Solved

Calling a constructor

Posted on 2009-04-08
7
161 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

930 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

12 Experts available now in Live!

Get 1:1 Help Now