By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

copy constructor question

P: n/a
#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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Ilia Poliakov" <ip*****@allesklar.de> wrote in message news:bi************@ID-120622.news.uni-berlin.de...
A(B& rB) {
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) {
}
Jul 19 '05 #2

P: n/a

"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bi************@ID-120622.news.uni-berlin.de...
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.

Jul 19 '05 #3

P: n/a

"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bi************@ID-120622.news.uni-berlin.de...
#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);


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

Jul 19 '05 #4

P: n/a
Ilia Poliakov wrote:
#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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.