Calling a constructor

Hi,

Is there a way I can call a parent constructor of a class without using the : notation:

myClass::myClass():parentClass(stuffhere){
}

Can I do anything similar inside the method body rather than the above?

Thanks,
Uni
LVL 3
Unimatrix_001Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Infinity08Connect With a Mentor Commented:
No. If you need to pass arguments to the base class constructor, you have to call it in the derived class's initialization list (like you showed).

You can call other base class methods by explicitly mentioning the base class namespace, eg. :

        MyClass::fun(int a) {
            ParentClass::fun(a);
        }

But constructors are a special case, since they're used for constructing objects ... ie. the base class has to be constructed before constructing the derived class.
0
 
Unimatrix_001Author Commented:
Ok, thank you. :)
0
 
Infinity08Commented:
Is there any specific reason you wanted to call it in the body of the constructor ? Maybe there's another way to do things ?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Unimatrix_001Author Commented:
Nah, just out of preference really - I like all code within the body... Nothing more than a preference really. :)
0
 
Infinity08Commented:
In that case, you should know, that using the initializer list is recommended, since it allows the compiler to optimize the code more than if you'd do it in the body.
0
 
Unimatrix_001Author Commented:
I shall do that then...
0
 
Infinity08Commented:
;)
0
All Courses

From novice to tech pro — start learning today.