Jump to content
  • Advertisement
Sign in to follow this  
xeddiex

Basic C++ question

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

Back from a month-long break and just refreshing up on my C++ beginner knowledge. Question: Is, ClassType var; the same as, ClassType var = ClassType();? Thanks, - xeddiex

Share this post


Link to post
Share on other sites
Advertisement
Yes.

Note that neither is possible if there is no no-arg constructor (i.e., a constructor is defined that requires at least one argument, and there is *not* a constructor defined that could take no arguments. Without any constructors, of course, the language will generate a no-arg constructor for you.)

Share this post


Link to post
Share on other sites
Thanks for the prompt reply, Zahlman. Another question: Does invoking the class name itself (.. ClassType() ..) make the compiler emit code to create a temp object and pass it as a pointer/ref to the ctor for initialization?

Last question: What really is the difference between, ClassType var; and, ClassType var();?

- xeddiex

Share this post


Link to post
Share on other sites
for custom types there is NO difference. For POD types (int, short, etc) then the second version I believe initializes the type to the default value (zero) ... but I'm not 100% of that cause I never use it that way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
For POD types (int, short, etc) then the second version I believe initializes the type to the default value (zero) ... but I'm not 100% of that cause I never use it that way.



int i();


.. declares a function.

Share this post


Link to post
Share on other sites
ClassType var = ClassType();

Will require that the copy constructor be accesible (e.g. not private if being called outside the class' code). Also, a(n extremely) dumb compiler can legitimately create a temporary with the default constructor and copy construct var from that temporary. Most compilers are not that dumb and will just default initialize var.

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
Quote:
Original post by Kuladus

int i();


.. declares a function.

He means somthing like int i = int();


I was answering this question:

Quote:

Last question: What really is the difference between, ClassType var; and, ClassType var();?

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.

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!