|
Does anyone know if it's possible to pass arguments to the constructor of array elements?
For example: I have a class Cfoo
class Cfoo
{
public:
Cfoo( int arg1 = 0 );
private:
int m_arg1;
}
I want to create an array of 200 Cfoo's, and initialise them with Cfoo(-1). I've tried
aFoo = new Cfoo(-1)[200];
aFoo = new Cfoo[200](-1);
I've run out of ideas
Is it impossible to call ctors on members of dynamically declared arrays?
TIA,
Pete
|
|
|
|
|
If you were to use vector, then it would be easy. But what you're trying to do is not possible AFAIK.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
just to expand on what the previous poster said:
here's an example (from google):
class Foo() { ... Foo(int, long, char, char*) { } ... };
vector<Foo> myArray(10, Foo(1, 2, 3, "hello"));
unless you have a *very* specific reason for using "new []", std::vector is usually a better way to go.
-c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses. /. #3848917
|
|
|
|
|
well it should work
memset (aFoo, -1, 200);
i guess this should work...
bernhard
"I'm from the South Bronx, and I don't care what you say: those cows look dangerous." U.S. Secretary of State Colin Powell at George Bush's ranch in Texas
|
|
|
|
|
Thanks for the input guys.
It seems that most of my questions on CP these days lead to a new appreciation for the STL
Thanks again,
Pete
|
|
|
|
|
...does the following code hang and or lock up my app. I'm using the MSComm32 ocx for serial communications, and m_Comm is a member variable to my Control Class. If I trace out the "input" I'll see the correct data flowing through, and If I monitor the serial port it shows the connect but, my app just hangs there.
BOOL bConnected = FALSE;
CString sInput;
while(!bConnected)
{
if (m_Comm.GetInBufferCount() > 0)
{
sInput = m_Comm.GetInput();
sInput.MakeLower();
if (sInput.Find("connect") != -1)
bConnected = TRUE;
}
}
As far as I can tell, this should work. But it doesnt.
Thanks,
Frank
|
|
|
|
|
You've traced this through and sInput does indeed contain the word 'connect' ???
Why are you using a BOOL instead of a bool ?
Personally I'd also include a timeout in code like the above. What if m_Comm.GetInBufferCount() remains 0, or sInput.Find("connect") continues to be -1 ?
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
Why are you using a BOOL instead of a bool ?
This may sound stupid...but, um...what the difference?
Christian Graus wrote:
Personally I'd also include a timeout in code like the above. What if m_Comm.GetInBufferCount() remains 0, or sInput.Find("connect") continues to be -1 ?
Its in my TODO list.
Seriously, I've got 2 of these while loops. The only way I've even remotely been able to get them to exit properly is to include a 'break' after the if returns true.
Thanks Christian,
Frank
PS, whats the weather going to be like tomorrow?
|
|
|
|
|
Frank Deo wrote:
This may sound stupid...but, um...what the difference?
Fair enough. A BOOL is a typedef'd int, created for C programmers. C++ has an intrinsic bool type. It is more reliable, because it is only able to be set to true or false. An int can have any value, so checking for TRUE or FALSE may in fact not catch the value something is set to.
Frank Deo wrote:
The only way I've even remotely been able to get them to exit properly is to include a 'break' after the if returns true.
Have you tried setting a break point to see if any of the conditions you're checking for ever come about ?
Frank Deo wrote:
PS, whats the weather going to be like tomorrow?
*sigh* rain, rain, rain.....
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Christian Graus wrote:
A BOOL is a typedef'd int, created for C programmers. C++ has an intrinsic bool type. It is more reliable, because it is only able to be set to true or false. An int can have any value, so checking for TRUE or FALSE may in fact not catch the value something is set to.
Cool, thanks for the insight.
Christian Graus wrote:
Have you tried setting a break point to see if any of the conditions you're checking for ever come about ?
Yea. Everywhere. When the app locks, and I break it its always on the while statement (the top of it). Dunno why.
Thanks again Christian,
Frank
|
|
|
|
|
|
Hi,
i have 3 objects derived from CObject.
These objects are the data source for my tree control. I want to find out which object is selected so i tried to use RUNTIME_CLASS macro.
CObject *selectedItem = ( CObject * ) m_treeMain.GetItemData( m_treeMain.GetSelectedItem() );
if( selectedItem )
{
if( selectedItem->IsKindOf( RUNTIME_CLASS( CBand ) ) )
{
AfxMessageBox( "so far so good" );
}
}
This code causes two errors :
1) error C2039: 'classCBand' : is not a member of 'CBand'
2) error C2065: 'classCBand' : undeclared identifier
What is this classCBand anyway?
I included Band.h into this function's file. And when i replace CBand with another class name such as CObject or CTreeCtrl, it works. So what's wrong with my class? What should i do to retrieve the runtime class ?
Thanks in advance.
|
|
|
|
|
There is a macro you need to include in your class for this stuff to be declared. I was forced to use this crap by a former employer, it is at best a hack. You should instead use dynamic_cast, which works for anything, not just CObject derived stuff.
CBand *selectedItem = dynamic_cast<CBand*>( m_treeMain.GetItemData( m_treeMain.GetSelectedItem() ));
if( selectedItem )
{
AfxMessageBox( "so far so good" );
}
dynamic_cast returns NULL if the cast is invalid, otherwise the object requested. It also tells you at compile time if the cast you are attempting is unreasonable.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
If you use dynamic_cast, check up option "Enable Run-Time type info" in your project settings. It is switched off by default, AFAIK
|
|
|
|
|
That is true. I forgot that one, sorry.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Thank you both for your help. It's working fine now.
But i had to do a minor change in your code sample.
CBand *selectedItem = dynamic_cast<cband*>( (CObject*)
m_treeMain.GetItemData(
m_treeMain.GetSelectedItem() ));
if( selectedItem )
{
AfxMessageBox( selectedItem->GetBandName() );
}
I don't know why but it didn't directly cast from DWORD to CBand and gave the following message :
error C2681: 'unsigned long' : invalid expression type for dynamic_cast
So i decided to cast it to CObject* first. Frankly, i didn't quite understand why this change made it work, but it did, so i just moved on
Anyway, thanks again.
|
|
|
|
|
hi all,
I found an article about I have an excel automation dos based application . I take an article,
Automate excel from c++ without using mfc or #import (Q216686) as an example.
then based on another article Automate excel and then know the use closed it(Q192348)
I changed my code again. actually, i added,
<br />
<br />
HWND hWnd;<br />
char buf[1024];<br />
hWnd = ::FindWindow("XLMain",NULL);<br />
if(NULL==hWnd)<br />
{<br />
long lErr = GetLastError();<br />
sprintf(buf, "FindWindow Error code = %d",lErr);<br />
AfxMessageBox(buf);<br />
}<br />
<br />
DWORD pid;<br />
DWORD dThread;<br />
DWORD dwReason;<br />
dThread = ::GetWindowThreadProcessId(hWnd,&pid);<br />
HANDLE hProcess;<br />
hProcess = ::OpenProcess(SYNCHRONIZE|PROCESS_ALL_ACCESS,TRUE,pid);<br />
<br />
dwReason = ::WaitForSingleObject(hProcess,INFINITE);<br />
::CloseHandle(hProcess);<br />
everything is fine except
dwReason = ::WaitForSingleObject(hProcess,INFINITE);
is waiting forever because in windows task manager's processes tab, you will find EXCEL.EXE eventhough you have closed ms excel application.
if you command the line
dwReason = ::WaitForSingleObject(hProcess,INFINITE);
you will not find EXCEL.EXE in windows task manager's processes tab.
why is that?
|
|
|
|
|
can someone help me please
|
|
|
|
|
G'Day Win,
I use the same code successfully. What I discovered was that as I developed the application I would run it and terminate it prematurely with Excel open. This of course did not close down excel properly. So that when I started the application again I had 2 versions of excel running etc. Thus when I had a 'real' excel application running and I tested my program and shut excel down this code failed because I had closed down a different excel to the one being watched. I think this is what may be happening in your case. So terminate all excel processes with task manager, run excel and then your application and see if it works then.
Hope that is of some help,
Richard.
|
|
|
|
|
thank you Richard.
that's is a really helpful. Thanks
|
|
|
|
|
I need to handle the WM_MOUSEMOVE message for my tree control. I've been told that I have to derive a class from my tree control and handle mousemove int there but I can't figure out how to derive the class from my tree control.
-Raffi
|
|
|
|
|
It's simple. Launch the MFC wizard (CTRL+W) and create a new class selecting CTreeCtrl as the base class (call it CMyTreeCtrl , for instance.) Handle WM_MOUSEMOVE in the new class. Now, where you defined a variable of type CTreeCtrl , replace CTreeCtrl with CMyTreeCtrl (you'll need to include "mytreectrl.h" , also.) That's it.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I previously have all my tree control code in my view class. Do I have to move all my tree control code to this new class or will it work with just leaving it as is and doing what you said above?
-Raffi
|
|
|
|
|
It'll work by only replacing CTreeCtrl with the new class, you can leave all the other stuff where it was
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
worked perfectly, thanks a lot
-Raffi
|
|
|
|