Basic C++ question
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
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.)
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.)
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
Last question: What really is the difference between, ClassType var; and, ClassType var();?
- xeddiex
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.
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.
Quote:Original post by Kuladusint i();
.. declares a function.
He means somthing like int i = int();
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.
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.
Quote:Original post by jflangloisQuote:Original post by Kuladusint 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();?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement