ONLY if CFoo has a well formed assignment operator :eek:Quote:
foo = CFoo(); //init foo with brand new CFoo, almost by calling ctor second time
Printable View
ONLY if CFoo has a well formed assignment operator :eek:Quote:
foo = CFoo(); //init foo with brand new CFoo, almost by calling ctor second time
It makes a difference.Quote:
I can't see for the life of me see the purpose of that! All it's doing is making another function do what the constructor does... and once! What I asked was if you can call a constructor twice.
A constructor is NOT a normal member function. It's specially handled by the compiler, and implicitly does a lot of things under the hood, especially when inheritance is involved, moreover it has specific semantics so that when new() is called, the storage gets a new effective type.
Do you know the difference between initialization and assignment?Quote:
Two people posted the same code but all it does is chuck a function into a constructor that does the constructor's job... I just don't see the point in it.
You're going to get into troubles if you do that!
For example, if your class contains a std::string member, I can claim that with most compilers, the second initialization won't free the memory of the first string, and it'll do a memory leak!
If you're lazy to such an extent, I guess you don't write many "secondary functions", but write huge functions... That would be a very bad programming style.Quote:
It's not that important that I find out the answer to this question, but I was just curious because it is easier than creating another function to initiliase a class's
No, behavior is defined, but this is evil, and may certainly create memory leaks!Quote:
Originally Posted by exterminator
Quote:
Originally Posted by basic.life
You can indirectly, through placement operator new, but this is evil and will produce memory leak. Bad idea.Quote:
Originally Posted by Mybowlcut
Actually, this is "re-using" the memory of the previous object. The old object dies (and memory is likely to leak) while a new object takes its place.
Very good.Quote:
Originally Posted by Hobson
This basic principle of procedural programming must not be forgotten in OOP, because OOP is an extension of the procedural programming paradigm and must keep the good principles.
I would like to tell, so that it becomes clear to everybody, that even when not using the initializer list, constructors of members (and base classes) are implicitly called before the "body" of the constructor is entered.Quote:
Originally Posted by exterminator
Exactly! And this is the reason why we cannot do explicit instantiation of constructor templates! Here - http://learningcppisfun.blogspot.com...-explicit.htmlQuote:
Originally Posted by Philip Nicoletti
It's like saying somebody can be born twice and be the same (like in my example) or may be qite different (like in your extension).Quote:
Originally Posted by Philip Nicoletti
Now returning to good programming common sense, each of us would be happy to have an Init() method when the destructor is approaching (like the OP purpose and the solution offered here)... :)
Ovidiu
PS. I have heard that every cat has nine constructors. :D ;)
not really. ONLY if there is dynamic allocation in the assigned class AND if CFoo has a assignment operator. there is std::string used so its safe to let the compiler generate a assignment operatorQuote:
Originally Posted by TheCPUWizard
so if he dies from age he can live 9 times his age? or will it create a gaia leak in the planet?Quote:
Originally Posted by ovidiucucu
Mitsukai,Quote:
Originally Posted by Mitsukai
If there is no dynamic allocation, and all member variables have fell formed assignment operators, then the default assignment operator will be a well formed assignment operator. Therefore I stand by my original statement.