Base clase constructor

F

Flzw

Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?
 
B

Bob Hairgrove

Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?

In the initialization list of the derived class' constructor, e.g.:

class Base
{
int x;
public:
Base(int a) : x(a) {}
};

class Derived
{
int y;
public:
Derived(int b, int c) : Base(b), y(c) {}
};
 
H

Howard

Flzw said:
Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?

I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};

-Howard
 
K

Karl Heinz Buchegger

Flzw said:
Probably a stupid question but, how do I pass arguments to the base class
constructor when I create an Instance of a derived class ?

Does your textbook not cover this?
(You cannot learn a language as complex as C++ with a textbook)

To answer the question:
By specifying an initializer list

class Base
{
public:
Base( int i );
};

class Derived : public Base
{
public:
Derived();
};

Derived::Derived() : Base( 5 ) // initializer list to initialize the
// Base part by passing 5 to the ctor
{
}
 
H

Howard

Howard said:
I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};

-Howard

I meant this:

Derived( int x ) : Base( x ) { };

(I think the initializer has to go with the *definition* of the constructor,
and my first post just had a declaration, not a definition.)

-Howard
 
K

Karl Heinz Buchegger

Howard said:
I believe this is what you're looking for:

class Derived : public Base
{
Derived( int x ) : Base( x );
};

That's a syntax error.

Either you define the ctor inline, then the initializer list is also
defined 'inline'.
Or you don't, then the initializer list is also not 'inline'.

In simpler words: The intializer list goes always with the constructors
implementation.
 
H

Howard

Karl Heinz Buchegger said:
That's a syntax error.

Yeah, I saw that the instant I hit Send. :) (I also corrected myself.
Hopefully I didn't screw up my correction as well. Some days it just don't
pay to open my big fat mouth.)

-Howard
 
K

Karl Heinz Buchegger

Howard said:
[snip]

Yeah, I saw that the instant I hit Send. :)

Don't you hate it when that happens? I do!

That's really interesting: I can proofread as long
as I want and don't see the simplest mistakes. 1/10
second after hitting send, I immediatly know that I
made a mistake.
Same thing with programming. I can spend literally hours
in proofreading a code section and don't see the problem.
But as soon as I fire up the debugger and the breakpoint
at the start of the section is reached, I know immediatly
what's wrong. I vote for editors with builtin debuggers :)
 
M

Marcelo Pinto

Howard said:
....
I meant this:

Derived( int x ) : Base( x ) { };
^
just a small remark: this semicolon is not necessary.
(I think the initializer has to go with the *definition* of the constructor,
and my first post just had a declaration, not a definition.)

-Howard

Best regards,

Marcelo Pinto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top