Jump to content
  • Advertisement
Sign in to follow this  
Grellin

A little help with a class problem.

This topic is 5457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my question. Why does a class set up similar to this not call the constructor?
 class Test
{
public:
Test();
~Test();
int Test;
};

Test::Test()
{
printf("constructor");
}
Test::~Test()
{
printf("destructor");
}

Test   ObjectName;

int main()
{
return 0;
}

But making the class object inside main() does?

Share this post


Link to post
Share on other sites
Advertisement
your question could use some more details but i assume your thinging of a class which has no con/de structor im pretty sure that some classes you can get away without declaring either and the compiler will provide you with the default one but if you declare one you MUST declare the other

If your reffering to the fact that you may not always need to call a constructor thats because unless the object is beinging dynamicly created or you are intializing variables then you dont need to call the constructor (especialy if the constructor dosnt do anything any way)
correct me if im wrong

Share this post


Link to post
Share on other sites
The way I understand it, when a class object is created the default constructor for that class is called. In my example when the object is created inside the main() the constructor works, verified by the printf statement. When the object is created outside of the main() as a global object, the constructor is not called, also verified by the printf statment not being called. In my example the code is fairly useless because I didn't want to put the whole class up there when the part that matters is there. I am just trying to figure out why, when the object is global the constructor I defined isn't called. I know the class still works becuase the rest of the methods and data memebers I have in my real class are accessible.

Share this post


Link to post
Share on other sites
If the object is created outside of main()...hmm, this is an interesting one.

I'd say that when you create the object outside of main the object is never created, because it never enters the program execution [loop]. Which would mean that until you initialize the variable with Test blank = new Test, or use it inside of main, the constructor is never called. This would raise a question or two for me as to whether or not objects are created in global scope or not.

...Whoa, I lost myself, and probably said a few things that were wrong. Aks questions where you pleeze.

Share this post


Link to post
Share on other sites
umm yeah when i run and compile that test program, with renaming the integer in it since it conflicts with the class name, it prints the constructor/destructor statements... although this is using VC++ 6.0 and probably isn't the standard?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I too can compile this (after renaming the int test) and it runs just fine. I am using mingw. The object is created at the time main is first called.

Share this post


Link to post
Share on other sites
Yeah it worked fine for me under gcc 2.95.4. What compiler were you using, Grellin? It's possible that it was working, but perhaps stdout was not initialised, so the output was going into the bit bucket..

cheers
sam

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you dont actually use Test somewhere the compiler will just optimize it out.

Try setting Test.Test to something somewhere and see if it constructs it then.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
If you dont actually use Test somewhere the compiler will just optimize it out.

Try setting Test.Test to something somewhere and see if it constructs it then.


But in this case, Test is being used. The compiler should (and does) call the constructor and desctructor. Any object declared outside of a function (i.e. non-local objects) is garaunteed to be constructed as stated in the C++ standard 3.6.2

However the order in which those objects are constructed is entirely independant for each translation unit, and it is not garaunteed that it'll happen after the standard output is initialized or not.

A more reliable way to test this out is to get the constructor and destructor to increment or alter some static variable, and get it to tprint out that value inside main.

- Phil

Share this post


Link to post
Share on other sites
Thanks for all the responses guys. Still can't figure it out. I tested my code on my Linux box with KDevelope and it worked fine. I guess MSVC++ 6 just hates me. If I figure out what it is, I'll post it. The strange thing is the member functions and data memebers are all working as they should. Just the dang constructor and destructor are not getting called. Oh well. Thanks again for all the ideas.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!