Jump to content
  • Advertisement

Archived

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

sab3156

contemplating constructor calling

This topic is 5732 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, let''s say i have an class called Wheel. now, i have no default constructor defined, yet i have an overloaded constructor that takes 2 floats as arguments. if i instanciate the object and call that constructor like this:
Wheel bike_wheel;
...
...
bike_wheel(5.0, 9.0);
 
is the default constructor ever called? if so, what if i do it like this:
Wheel bike_wheel(5.0, 9.0);
 
is there a difference?

Share this post


Link to post
Share on other sites
Advertisement
First of all, the compiler doesn''t produce a default constructor if you provide a constructor.

Secondly, if you''ve provided a constructor at all, and haven''t provided a default constructor, then the compiler won''t let you use the first way.

Share this post


Link to post
Share on other sites
so if i use the first choice without providing a default constructor, i''ll have errors?

also, i understand in java the object is instanciated like this:


ClassName reference_var = new ClassName(args);


can you do this exact syntax in C++?

Share this post


Link to post
Share on other sites
No, you need:
ClassName *reference_var = new ClassName(args);
or
ClassName &reference_var = *new ClassName(args);

Skizz

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
[quote]Original post by Skizz
ClassName &reference_var = *new ClassName(args);



I smell a memory leak.






why? what''s wrong with it? is it better to use the other one?

Share this post


Link to post
Share on other sites
Because it won''t have its destructor called when it goes out of scope. You could add a ''
delete &reference_var; 
'' at the end of the function, but you would still have a leak if an exception was ever thrown because that code wouldn''t be executed.

Share this post


Link to post
Share on other sites
No, there isn't a way around it. Don't do that. Ever. Use the pointer example instead.


Don't listen to me. I've had too much coffee.

[edited by - sneftel on February 7, 2003 11:26:35 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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!