constructor being ignored in visual studio 2010
I'm not used to using VS and when I realized my constructor was being ignored, you can understand why I would be confused
example:
VTXMENU::VTXMENU()
{
Submenu = NULL;
Next = NULL;
Previous = NULL;
Parent = NULL;
Base = NULL;
ItemType = 0;
ItemName[0] = 0; // (these two are character-array strings)
Command[0] = 0; //
cout<< "created\n";
}
and I'm creating one dynamically via:
VTXMENU *fish;
fish = new VTXMENU;
not only do my strings end up full of garbage, but the message "created" is never displayed
Why is this happening?
thx
Re: constructor being ignored in visual studio 2010
Quote:
Originally Posted by
invaderjolleyolleyman
and I'm creating one dynamically via:
VTXMENU *fish;
fish = new VTXMENU;
not only do my strings end up full of garbage, but the message "created" is never displayed
Why is this happening?
thx
Is VTXMENU a struct or a class? I'm not sure if I correctly interpreted the wording, but it seems like VC++ treats structs and classes differently when you create an object with new as you do.
http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
Quote:
Originally Posted by MSDN
If an object is of a class type and that class has constructors (as in the preceding example), the object can be initialized by the new operator only if one of these conditions is met:
- The arguments provided in the initializer agree with those of a constructor.
- The class has a default constructor (a constructor that can be called with no arguments).
It seems like "class type" does not include a struct and, therefore, your object is not initialized when you call new like this. To make sure the default constructor is called use this instead:
Code:
VTXMENU *fish = new VTXMENU();
Re: constructor being ignored in visual studio 2010
Hmmm.....that seems unlikely. There should be no appreciable difference between structs and classes on that level.
Re: constructor being ignored in visual studio 2010
Code:
#include <iostream>
using namespace std;
struct foo
{
int a;
foo()
{
std::cout << "hello\n";
}
};
int main()
{
foo* pFoo1 = new foo;
foo* pFoo2 = new foo();
}
Objects are properly constructed for both C::B+MinGW, as well as VS2008.
I think a short but complete code example could shed some light on this.
Re: constructor being ignored in visual studio 2010
Quote:
Originally Posted by
invaderjolleyolleyman
I'm not used to using VS and when I realized my constructor was being ignored, you can understand why I would be confused
If a compiler such as Visual C++ 2010 can't call a simple constructor, then thousands of programmers would have reported the problem.
So post a complete example, as the chance of constructors being ignored in one of the most used C++ compilers in the world is practically zero.
Regards,
Paul McKenzie
Re: constructor being ignored in visual studio 2010
Quote:
Originally Posted by
D_Drmmr
Is VTXMENU a struct or a class? I'm not sure if I correctly interpreted the wording, but it seems like VC++ treats structs and classes differently when you create an object with new as you do.
http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
It seems like "class type" does not include a struct and, therefore, your object is not initialized when you call new like this. To make sure the default constructor is called use this instead:
Code:
VTXMENU *fish = new VTXMENU();
I'm using a class (not a struct)
I thought I mentioned earlier that putting the parenthesis didn't help
I know this doesn't make sense or I wouldn't be asking about it ^_^
if a thorough example is what you guys want, then you shall receive it...
from main.cpp :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
VTXMENU* root;
root = new VTXMENU;
cout<< root->Command << endl;
delete root;
root = new VTXMENU();
cout<< root->Command << endl;
delete root;
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from VTXMENU.h :
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class VTXMENU
{
public:
VTXMENU* Submenu;
VTXMENU* Next;
VTXMENU* Previous;
VTXMENU* Parent;
VTXMENU* Base;
char ItemName[40];
char Command[40];
int ItemType;
VTXMENU();
VTXMENU(VTXMENU &old);
~VTXMENU();
VTXMENU operator = (VTXMENU &old);
void Release();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
constructor from VTXMENU.CPP :
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VTXMENU::VTXMENU()
{
Submenu = NULL;
Next = NULL;
Previous = NULL;
Parent = NULL;
Base = NULL;
ItemType = 0;
ItemName[0] = 0;
Command[0] = 0;
cout<< "initialized\n";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
both time that the contents of Command are displayed, it shows something similar to:
=========================
also, the word 'initialized' is never displayed
none of this seems rational at all
maybe my computer has a hole in its head or something
for some reason I've been having trouble getting this project to build well
I had this same problem a few days ago but it seemed to go away on it's own when I added an arbitrary include to one file, built, removed the include, then built again...
( I tried that again this morning but it didn't help XD )
Re: constructor being ignored in visual studio 2010
Quote:
Originally Posted by
invaderjolleyolleyman
both time that the contents of Command are displayed, it shows something similar to:
=========================
also, the word 'initialized' is never displayed
none of this seems rational at all
maybe my computer has a hole in its head or something
for some reason I've been having trouble getting this project to build well
I had this same problem a few days ago but it seemed to go away on it's own when I added an arbitrary include to one file, built, removed the include, then built again...
( I tried that again this morning but it didn't help XD )
This code, pasted into my test project (after I fixed a "missing destructor" error) works OK: I see two 'initialized'.
Could you try that? Does it work for you?
Something else must be wrong in your project.
Re: constructor being ignored in visual studio 2010
wow
I got it to work but it's tempting just to let you guys fight over it for a while before telling you what happened
Apparently, when using visual studio 2010, it is not only good practice but in fact required to say funcName(void) rather than just funcName() when declaring your functions
I'm not exactly upset about this but I can't help but wonder why no one in any of the books I have read or the college courses I have taken have ever mentioned this
I've only recently been inserting bits like that just for style purposes
I wonder it will stop working again tomorrow without me changing anything - lol
Re: constructor being ignored in visual studio 2010
That isn't required.
I'll bet what happened, though, was that your file's modification date got screwed up somehow, so that the version containing the constructor wasn't being recompiled. When you added the void, you forced it to be recompiled. That's my guess anyway.
Re: constructor being ignored in visual studio 2010
Code:
ItemName[0] = 0; // (these two are character-array strings)
Command[0] = 0; //
If these are character arrays of length X, then you are only initializing the first element. Therefore the elements (1 - (X-1)) won't be initialized. It doesn't matter that much but if you are looking at the memory in debug the uninitialized memory could look like what you might call "garbage".
Moreover you didn't flush the output stream so there is no guarantee that the message will be printed when the constructor executes. Use std::endl if you want to add that guarantee otherwise depending on the compiler you may or may not see it at all.