Can I Call virutal function from a default Constructor in c++
-
Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)
-
Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)
The short answer is yes, but I would advise against it. A virtual method will propegate down to the derived class. But when you call the derived class' c'tor it will initialize the base class first. So, in the derived method, if you are relying on any members being initialized, they won't be. This may lead to hard to spot problems later in your code. Pseudo code:
call derived c'tor call base c'tor initialize base members call virtual method use **un**-initialized derived member initialize derived members
-
Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)
-
The short answer is yes, but I would advise against it. A virtual method will propegate down to the derived class. But when you call the derived class' c'tor it will initialize the base class first. So, in the derived method, if you are relying on any members being initialized, they won't be. This may lead to hard to spot problems later in your code. Pseudo code:
call derived c'tor call base c'tor initialize base members call virtual method use **un**-initialized derived member initialize derived members
Unless it is qualifid with the current class name, correct? You can 'force' the virtual function of a specific, known baser class, to be invoked as long as you are at least derived from said baser class. That would be safe.
-
Unless it is qualifid with the current class name, correct? You can 'force' the virtual function of a specific, known baser class, to be invoked as long as you are at least derived from said baser class. That would be safe.
Anything is safe as long as you know what you are doing. In the case of the poster though, it's not wise to baffle him with technicalities while he is just starting on the basics. Later, after he has got the basics under his thumb, is when he will start breaking the rules and pushing his code as far as it will give :rolleyes: