Solved

Calling a constructor

Posted on 2009-04-08
7
166 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
ID: 24104456
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
ID: 31568381
Ok, thank you. :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24104609
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24104640
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
ID: 24104650
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
ID: 24104667
I shall do that then...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24104671
;)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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