Question about derived class and copy constructors....

J

Jeroen

Hi all,

I wrote a little bit of code to see the behaviour of (copy) constructors
of base and derived classes. But I have a question about it. Let me
explain by the following (incomplete/illustrative) code:

class A {
// (copy) ctors goe here...
};

class B : public A {
// (copy) ctors goe here...
};

void foo(A a)
{
// function body here...
}

I can call 'foo' with an object of type B:

{
B b;
foo(b);
}

What I saw in my code is that when calling 'foo' this way, an object of
type A is created and the copy constructor for type A is called (which
apparently gets 'b' passed as parameter).

My question: is it possible that a compiler creates an object of type B
and calls the copy constructor for type B? In that case a type B object
is passed to the the function 'foo'.

I'm not sure about this because if I have a function:

void foo2(A& a){}

I can call that also with foo(b) and only a reference of 'b' is passed,
and the function 'foo' is fine with processing the object of type B...

On the other hand, I can imagine that 'foo' depends on a complete object
A on the stack so that passing an object of type B is not possible.

Or maybe I completely missed the point.

Thanx for comments and clarification,

Jeroen
 
I

Ian Collins

Jeroen said:
Hi all,

I wrote a little bit of code to see the behaviour of (copy) constructors
of base and derived classes. But I have a question about it. Let me
explain by the following (incomplete/illustrative) code:

class A {
// (copy) ctors goe here...
};

class B : public A {
// (copy) ctors goe here...
};

void foo(A a)
{
// function body here...
}

I can call 'foo' with an object of type B:

{
B b;
foo(b);
}

What I saw in my code is that when calling 'foo' this way, an object of
type A is created and the copy constructor for type A is called (which
apparently gets 'b' passed as parameter).
Correct, polymorphism doesn't work when passing by value, you get the A
part of b. This is known as slicing.
My question: is it possible that a compiler creates an object of type B
and calls the copy constructor for type B? In that case a type B object
is passed to the the function 'foo'.
No.

I'm not sure about this because if I have a function:

void foo2(A& a){}

I can call that also with foo(b) and only a reference of 'b' is passed,
and the function 'foo' is fine with processing the object of type B...
That's how it is supposed to work.
 
J

Jeroen

Ian Collins schreef:
Correct, polymorphism doesn't work when passing by value, you get the A
part of b. This is known as slicing.

That's how it is supposed to work.

OK, thanks for the answer.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top