 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jeroen Guest
|
Posted: Mon May 21, 2007 9:11 am Post subject: Question about derived class and copy constructors.... |
|
|
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 |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Mon May 21, 2007 9:11 am Post subject: Re: Question about derived class and copy constructors.... |
|
|
Jeroen wrote:
| Quote: | 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.
| Quote: | 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. |
| Quote: | 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. |
--
Ian Collins. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|