Constructor

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

I've a class that contains a private member object
that in turn is a reference:

class A
{
public:
A( classB & );

private:
classB &mB;
};

A::A( classB &B )
: mB( B )
{
}

What happens in the constructor?
Will the copy constructor of classB be invoked?
Or will just the reference of B be assigned to
mB without calling any constructors?

Is it reasonable to add a copy constructor and
an assignment operator for class A? Or will this
be handled by the default constructors and operators?

Regards,
Chris
 
B

Bart

Christian said:
Hi,

I've a class that contains a private member object
that in turn is a reference:

class A
{
public:
A( classB & );

private:
classB &mB;
};

A::A( classB &B )
: mB( B )
{
}

What happens in the constructor?
Will the copy constructor of classB be invoked?
Or will just the reference of B be assigned to
mB without calling any constructors?

No copy. If a copy was required for references then copy constructors
would suffer from infinite recurence.
Is it reasonable to add a copy constructor and
an assignment operator for class A? Or will this
be handled by the default constructors and operators?

Adding a copy constructor and assignment operator is always reasonable,
but not required in this case.

Regards,
Bart.
 
J

Jim Langston

Christian Christmann said:
Hi,

I've a class that contains a private member object
that in turn is a reference:

class A
{
public:
A( classB & );

private:
classB &mB;
};

A::A( classB &B )
: mB( B )
{
}

What happens in the constructor?
Will the copy constructor of classB be invoked?
Or will just the reference of B be assigned to
mB without calling any constructors?

Is it reasonable to add a copy constructor and
an assignment operator for class A? Or will this
be handled by the default constructors and operators?

Regards,
Chris

If you think of a reference as a pointer, for this particular case, it
should be clearer. Your constructor accepts the address of classB, and
stores it in mB (as a reference). At this point mB points to ( as a
reference ) some external classB instance.

The instance of classB is never touched, not the constructor, the copy
constructor or the assignment constructor. Just the address is stored (the
reference is stored, your OS/Compiler may not actually store references as
pointers).

A default copy constructor and assignment operator won't be generated if
there is a reference in the class (I just tested this and was told by my
compiler that an assignment operator could not be generated). I'm still a
little hazy on when the copy constructor is called instead of the assignment
operator, so couldn't test that, but I'm fairly sure it's the same.

So you will either need to create your own copy constructor and assignment
operators, or do without.
 
B

Bart

Jim Langston wrote:
A default copy constructor and assignment operator won't be generated if
there is a reference in the class (I just tested this and was told by my
compiler that an assignment operator could not be generated). I'm still a
little hazy on when the copy constructor is called instead of the assignment
operator, so couldn't test that, but I'm fairly sure it's the same.
<snip>

You may be surprised but they aren't the same in this particular case.
It makes sense to make a copy constructor when there are reference
members, because the copy constructor initializes the references in the
newly created instance. On the other hand, an assignment operator does
not make sense because references cannot be made to reference a
different object. I'm a little rusty on what the standard says but I'm
pretty sure that implicitly-defined copy constructors ARE well formed
for classes that contain non-static reference members, while
implicitly-defined assignment operators are not.

That's as far as C++ legalese goes. Now, I realize I may have replied a
little to quickly in my previous post. In fact, if your class contains
a non-static reference then it is highly likely that it needs to
perform a deep copy and so a copy constructor and assignment operator
will be needed for that.

Regards,
Bart.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top