C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about derived class and copy constructors....

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jeroen
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Question about derived class and copy constructors.... Reply with 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).

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





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Re: Question about derived class and copy constructors.... Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.