Solved

Calling a constructor

Posted on 2009-04-08
7
160 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:Unimatrix_001
  • 4
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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
 
LVL 3

Author Closing Comment

by:Unimatrix_001
Comment Utility
Ok, thank you. :)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
Nah, just out of preference really - I like all code within the body... Nothing more than a preference really. :)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 
LVL 3

Author Comment

by:Unimatrix_001
Comment Utility
I shall do that then...
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now