using copy constructor

T

Tony Johansson

Hello Experts!

I have this constructor for class Flight.

Flight::Flight(string flight_no, Klocka dep_t, Klocka arr_t)
: no(flight_no), dep(dep_t), arr(arr_t) {}

Both dep and arr are objects of the Klocka class in the definition of the
Flight class.

The call to instansiate a Flight object is.
Flight f3("AA7089", ka, kb);

In this constructor for class Flight I wonder how many times will the copy
constructor for class Klocka be called.

As I have learned you may correct me if I'm wrong when an actual argument is
passed by value to a function will the copy constructor be called. In this
case will the copy constructor for class Klocka be called for copy object
ka to object dep_t and copy object kb to object arr_t.

But now to my question will the copy constructor for class Klocka be called
also for copying object dep_t to object dep and copying object arr_t to
object arr in the initialize list?

Many thank!
 
B

benben

Tony Johansson said:
Hello Experts!

I have this constructor for class Flight.

Flight::Flight(string flight_no, Klocka dep_t, Klocka arr_t)
: no(flight_no), dep(dep_t), arr(arr_t) {}

Both dep and arr are objects of the Klocka class in the definition of the
Flight class.

The call to instansiate a Flight object is.
Flight f3("AA7089", ka, kb);

In this constructor for class Flight I wonder how many times will the copy
constructor for class Klocka be called.
2


As I have learned you may correct me if I'm wrong when an actual argument is
passed by value to a function will the copy constructor be called. In this
case will the copy constructor for class Klocka be called for copy object
ka to object dep_t and copy object kb to object arr_t.

....
ka and kb are arguments, dep_t and arr_t are parameters, and it the call the
parameters ARE the arguments, i.e. dep_t IS ka and arr_t IS kb.
But now to my question will the copy constructor for class Klocka be called
also for copying object dep_t to object dep and copying object arr_t to
object arr in the initialize list?

Yes, so its one copy constructor for each in-class Klocka object.
Many thank!

You are welcome!

Regards,
Ben
 
T

Tony Johansson

Hello again!


In this constructor for class Flight I wonder how many times will the copy
You mentioned

but isn't that right when I read your answer I think that the answer is 4
instead.

//Tony
 
B

benben

Tony Johansson said:
Hello again!


In this constructor for class Flight I wonder how many times will the copy

You mentioned

but isn't that right when I read your answer I think that the answer is 4
instead.

Sorry I oversighted your code...

There are 4 copy constructions in the invocation to the constructor:

1. from ka to dep_t;
2. from kb to arr_t;
3. from dep_t to dep; and
4. from arr_t to arr.

In my previous post I thought the constructor was taking reference, which
isn't. Sorry for that.

However, if a parameter is only used to construct a class member variable,
then a reference is indeed commonly used as opposed to taking values.
Obviously, taking references reduces the number of constructor invocation.

Regards,
Ben
 
C

Christian Gyrling

As a side note. Why are you passing Klocka and the 'string' to the
constructor by value?
If the only thing you are doing is making a copy of them you should pass
them by const reference to avoid the extra copy constructions.

Christian
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top