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 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