0
Can I call derived class constructor before base class constructor in c++?
I have tried myself by making virtual constructor but compiler say that you cant make virtual cobstructors...I am literally confused..Kindly tell me..
12 Answers
+ 2
XXX yes it's been edited. But that's not even the point, there was no attempt at code until after. If he would've started with the attempt, then the answer to the question would reveal itself. And it would've came with the added bonus of understanding at a deeper level than someone who just takes it from the next guy. There's nothing wrong with asking at all, but i don't think it's cool to basically task others with what you have the same ability to do
+ 2
This sounds like the typical XY problem. ( https://en.wikipedia.org/wiki/XY_problem )
Like others have mentioned, no idea what you're trying to achieve with this.
If you're more specific we can give a better answer.
Also, you can have virtual constructors with a bit of a workaround:
https://en.m.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor
Whether you need it, I doubt it.
( still can't get away from the fact that base is constructed before derived )
+ 1
Well, the question is if we can do something in some language, so it doesn't depend on points of view, but on the language rules themselves.
That said, finding answers is always great for learning. So I propose a two step exercise:
A constructor is called when one creates an object from its class. So, your question equals "Can we instance a derived class before base class?".
1. Try to answer this question instead, from your knowledge of class inheritance.
2. Write some code doing that and see what happens.
+ 1
Slick
"Try it yourself and come up with your own point of view"
I disagree. I strongly believe that most of the times in programming, there is a "correct" way of doing something. Apart from language choices and naming conventions, I can't think of many things which are based on someone's POV and I think it's a good thing when someone asks others' POV (although, I would say that SL is a pretty bad place for asking such questions).
That said, I don't see any mention of "point of view" in the question, so I don't even know why we're having this discussion (unless OP did mention it and edited it later).
+ 1
proGAMBLER
No. You cannot call the constructor of a derived class before the constructor of the base class.
Why do you want to do that though? I don't see any reason for calling the constructor of the base class after.
You can however, explicitly call the constructor of the base class from the derived class. This allows you to have control over which base class constructor is called
This answer on StackOverflow shows an example
https://stackoverflow.com/a/15777735
+ 1
Slick
I see. I agree with all your points.
But I think in the context of this question, it was probably just bad wording from OP's side, since the question is related to the C++ language itself, and not something where they could've "attempted" the problem themselves before posting the question.
+ 1
proGAMBLER I understood you needed to know about the order you can call the constructors. But it is, in fact, about the order the constructors are executed.
I don't think you can reverse the execution order. But I don't think you need it either. Do you have any use case?
0
Let me share code what I want to do
0
Thanks Dennis..You're right guy who understood my question while others just took it as a joke..Sad to see..btw, thanks a lot
- 1
đ„șWhy sololearn community is so salty?
- 2
New