Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can a constructor call a method from inside it or can a method call constructor from within a class.
Posted
Updated 31-Aug-13 11:35am
v3

A constructor can call methods, yes.
A method can only call a constructor in the same way anything else can: by creating a new instance. Be aware that if a method constructs a new object of the same type, then calling that method from a constructor may result in an infinite loop...
 
Share this answer
 
Actually, the other three solutions have covered your question pretty good. There are just a few things I like to emphasize.

Be very careful when calling a virtual function from a constructor. Doing so does not call the method of your actual class object, i.e. the most derived version, but the method of the class as far as it has been constructed up to the point in this constructor. Example: You have a class A, and derive B and from B your derive C. And class A declares a virtual function f that is overridden in B and C. If you now create an object of type C and the constructor of B calls f, not C::f but B::f is called, because at the time the constructor of B runs, the object is still in the state of being a B and not a C. That's a bit tricky.

The advice of not making your constructor too complex is a good one. The reason for it is that the only way of signaling an error to the outside world is by throwing an exception. And correct exception handling can sometimes be a real challange. So you are well advised to make your constructor relatively simple and do the things that can go wrong (like opening files, creating windows etc) in a normal member function, which may report the error by return value.

And finally, you cannot call a constructor of a class from another constructor. If you have common code that you want to share between various constructors, then you have to put it into a separate member function (typically called Init).
 
Share this answer
 
Comments
Stefan_Lang 2-Sep-13 6:07am    
"Be very careful when calling"

Make that "Don't call"! It's an unneccesary risk for an extremely difficult to pin down bug. While there are cases where it works as intended, class hierarchies may change in the future, and thus cause the constructor code to break. Better not risk it.

"... not C::f but B::f is called, because at the time the constructor of B runs, the object is still in the state of being a B and not a C"
Are you sure every compiler treats it like that? It makes sense to me, but that doesn't mean it does to a compiler - and I doubt the C++ standard makes any demands about this kind of behaviour. Or does it?
Stefan_Lang 2-Sep-13 6:09am    
P.S.: 5ed for being the most complete solution.
nv3 2-Sep-13 6:47am    
Thanks for your amendments, Stefan. Yes, in fact I can't think of a situation where I'd want to call a virtual function from a constructor, so "don't call" appears to be a good advice.

Regarding the constructor logic: That's what I always thought as being the definition in the standard. And some other CP articles support that. I have never taken the time to look it up in the standard, though.
hi,
yes a method call can be made from within a constructor.
You might do this when you have multiple constructors in a class and you want all of them to call some common member variable initialization method.
Special care must be taken when calling virtual functions from constructor . You might end up with an access-violation exception if the method being called from the constructor is pure virtual.
As a good practice the methods you are calling from the constructor should not be very complex.
For other methods calling a constructor, see the OriginalGriff's answer .
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-Aug-13 22:32pm    
5ed.
—SA
chandanadhikari 1-Sep-13 0:57am    
Thanks Sergey
Stefan_Lang 2-Sep-13 5:51am    
Actually you may get a runtime error upon calling a virtual function even if it is not pure virtual, unless the version of that function you want to call is the one inside the same class as the constructor: the problem is that the constructor of a base class will be called before the constructor of the derived class, so calling a virtual function defined in the derived class would reference an uninitialized object!

Therefore you shouldn't call any virtual function inside a constructor at all.
chandanadhikari 2-Sep-13 6:48am    
Thanks for the explanation Stefan_Lang. The best practice is indeed to avoid virtual methods in constructors.
The answer to both of your questions is yes. You can call a method by writing it inside constructor; and you can call a constructor by writing it inside a method but usually we don't call a constructor by writing inside a method instead we simply create a new instance which calls the constructor automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900