copy constructor question

I

Ilia Poliakov

#include <iostream>

using namespace std;

class B
{
public:
B() {};
B(const B&)
{
cout << "Constructor called for B" << endl;
};
~B() {};
};

struct A
{
B m_oB;

public:
A(B& rB)
{
cout << "test1" << endl;
m_oB = rB;

cout << "test2" << endl;

B oTestB = rB;
};

~A() {};
};

void main()
{
B b;
A a(b);
}

Output:

test1
test2
Constructor called for B

Why was not B::B(const B&) constructor called in test1 case?

PS. I work under MSVC7.0
 
R

Ron Natalie

Ilia Poliakov said:
{
cout << "test1" << endl;
m_oB = rB;

This isn't construction it's assignment!
void main()
{
Main must return int!

Why was not B::B(const B&) constructor called in test1 case?
Because you don't construct a B object from another B object.
You construct an A object from a B. That constructor does
an assigment of B to B which calls the implicitly-generated
copy-assignment operator after m_oB was derfault constructed.

If you want to copy construct the member in the other constructor
you need to do this.

A(B& rB) : m_oB(rB) {
}
 
K

Kevin Saff

Ilia Poliakov said:
struct A
{
B m_oB;

public:
A(B& rB)
{
cout << "test1" << endl;
m_oB = rB;

// This calls operator=, not a constructor.
cout << "test2" << endl;

B oTestB = rB;

// This calls the copy constructor, not operator=.
};

~A() {};
};

void main()
{
B b;
A a(b);
}

Output:

test1
test2
Constructor called for B

Why was not B::B(const B&) constructor called in test1 case?

T newT = oldT;

// actually calls:

T newT(oldT);

// if available. However,

T newT;
newT = oldT;

// will call operator=.

// You should define operator= to do what you want.
 
K

Kevin Goodsell

Ilia said:
#include <iostream>

using namespace std;

class B
{
public:
B() {};

Get rid of these semi-colons at the end of functions.
B(const B&)
{
cout << "Constructor called for B" << endl;
};
~B() {};
};

struct A
{
B m_oB;

public:
A(B& rB)
{
cout << "test1" << endl;
m_oB = rB;

Copy assignment. No problem.
cout << "test2" << endl;

B oTestB = rB;

Copy construction. No problem.
};

~A() {};
};

void main()

void is not and never has been an acceptable return type for main. main
has always been required to return int.
{
B b;
A a(b);
}

Output:

test1
test2
Constructor called for B

Why was not B::B(const B&) constructor called in test1 case?

It's not clear why you believe it should have been. No construction
occurred there. Only an assignment to an existing object.

-Kevin
 
L

Liu Jian

Ilia Poliakov said:
#include <iostream>

using namespace std;

class B
{
public:
B() {};
B(const B&)
{
cout << "Constructor called for B" << endl;
};
~B() {};
};

struct A
{
B m_oB;

public:
A(B& rB)
{
cout << "test1" << endl;
m_oB = rB;

// Change it like following calls copy constractor of B
// B oB = rB;
// or B oB(rB);
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top