473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
May 21 '07 #1
2 1327
Jeroen wrote:
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.

--
Ian Collins.
May 21 '07 #2
Ian Collins schreef:
Jeroen wrote:
>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.
OK, thanks for the answer.
May 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
11
by: needin4mation | last post by:
I know this doesn't have much of a context, but, in this (take from Petzold's Programming Windows with C#) snippet: class DatePlus: Date { public DatePlus(){} public DatePlus(int year, int...
10
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now,...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
4
by: gw7rib | last post by:
Suppose I have the following code fragment: class base { private: int m; public: base(int x) { m = x; } };
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.