Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calling a constructor

Posted on 2009-04-08
7
164 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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