Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kenjin

I got a question for a basic C++ question.

This topic is 5478 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

Ok I thought I knew C++ but this stumped me, When you instantiate an object such as FunnySTuff hahah; this automatically calls the constructor right? But I put all these initialtion functions in a constructor. some of these were function calls to load Images and such but it didn''t apparently run them correctly. So does this mean that if when you do FunnyStuff haha; your just allocatino resources for it but you also have to call new FunnyStuff to call the constructor can someone clear this basic issue

Share this post


Link to post
Share on other sites
Advertisement
No. When you instantiate FunnyStuff in that manner the constructor will be called. Any other problems are either with your code, or a strange incompatibility in the compiler.

Post code if you''d like a more direct answer.

Share this post


Link to post
Share on other sites
I had the same problem before. I can''t remember what was up but it had something todo with either class arrays "myclass a[20];" or DLLs. Maybe you should make your own constructor function like "a.Initialize();".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Produce a small example that reproduces the problem for us. It is likely that while you are doing that you will realise what you are doing wrong.

Share this post


Link to post
Share on other sites
If you really want to see if the functions in the constructor are called, just place a breakpoint in there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In general it''s a bad idea to put resource allocation functions inside a constructor. I always create an explicit Setup() method which can return a failure code if something''s gone wrong. Especially for globally declared objects, whose construction order is difficult to determine (and may not be totally standard).

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
In general it''s a bad idea to put resource allocation functions inside a constructor. I always create an explicit Setup() method which can return a failure code if something''s gone wrong.
Yuck. constructors should allocate whatever resources are necessary to initialize objects. Constructor failure should be signalled by throwing an exception.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Ok, I don''t know C++, but I do know OOP in Java and I was pretty sure you would have to do hahah = new FunnySTuff(); to call the constructor. Is it just different in C++, or am I wrong about it in Java too? (it''s been awhile since I did any serious Java programming).

"If I only had 6 months left to live I''d get back together with my first wife,
because 6 months with her would be like an eternity."
-Bobby Hall

Share this post


Link to post
Share on other sites
quote:
Original post by kenjin
Ok I thought I knew C++ but this stumped me,

When you instantiate an object such as

FunnySTuff hahah;

this automatically calls the constructor right?

Right.
quote:

But I put all these initialtion functions in a constructor.
some of these were function calls to load Images and such
but it didn't apparently run them correctly.

Post the exact code you are having a problem with.
quote:
Original post by Anonymous Poster
In general it's a bad idea to put resource allocation functions inside a constructor.

Why?
quote:

I always create an explicit Setup() method which can return a failure code if something's gone wrong.

Why?

[edited by - SabreMan on June 18, 2003 5:57:33 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!