-
July 20th, 2010, 08:34 AM
#1
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
-
July 20th, 2010, 08:48 AM
#2
Re: constructor being ignored in visual studio 2010
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
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();
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 20th, 2010, 09:20 AM
#3
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.
-
July 20th, 2010, 09:26 AM
#4
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 20th, 2010, 09:59 AM
#5
Re: constructor being ignored in visual studio 2010
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
-
July 20th, 2010, 05:12 PM
#6
Re: constructor being ignored in visual studio 2010
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 )
-
July 20th, 2010, 05:21 PM
#7
Re: constructor being ignored in visual studio 2010
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.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 20th, 2010, 05:27 PM
#8
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
-
July 20th, 2010, 05:29 PM
#9
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.
-
July 20th, 2010, 07:25 PM
#10
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|