are any of these code blocks equivalent?Code:class c { int x,y; };Code:class c { int x,y; c() {}; };edit: I almost forgot, the same question for destructors too.Code:class c { int x,y; c(); }; c::c(){}
are any of these code blocks equivalent?Code:class c { int x,y; };Code:class c { int x,y; c() {}; };edit: I almost forgot, the same question for destructors too.Code:class c { int x,y; c(); }; c::c(){}
Last edited by robwhit; 06-30-2007 at 12:29 PM.
No. Well, sort of.
The first one uses the built-in default constructor. I'm not sure how it's implemented, but it's probably the same as the second version.
The second one declares and implements the constructor inline. (Because the function body is inside the class.) When a function is inline, this suggests to the compiler (it can ignore the request) that the function is so simple that where ever it is used, the code in the function is put there instead of a function call to this trivial function. It's a good idea to make set and get functions as well as empty ones inline like that.
The last one makes the constructor like a normal function. But the compiler is free to optimise and make any functions inline that it thinks should be inline, and in fact most compilers ignore the inline suggestions by the programmer and make their own decisions. They're much better at it than we are.
So they're probably the same, in the end, with optimisations and all.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
The the second onethis ; is not required.Code:c() {};
Simple Answer Nope
cause They Dont need.
I believe the answer is no.Are constructors/destructors supposed to have return types?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so it's impossible to have a function pointer to a constructor or a destructor?
I would reason yes, since constructors do not have names. Without a name, how would one point to a function?so it's impossible to have a function pointer to a constructor or a destructor?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok I tried thisCode:class c { public: int x,y; c(); }; c::c(){} int main() { typeof(c::c)*var; var=c::c; return 0; }what does that mean?Code:main.cc: In function `int main()': main.cc:14: error: expected primary-expression before ';' token
Isn't it the same name as the class?constructors do not have names
From what I understand, no. The syntax just happens to involve the class name, but it is really different from normal function declaration, even if it looks similiar.Isn't it the same name as the class?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)