Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   copy constructor question (http://www.velocityreviews.com/forums/t277060-copy-constructor-question.html)

Ilia Poliakov 08-25-2003 04:23 PM

copy constructor question
 
#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



Ron Natalie 08-25-2003 04:28 PM

Re: copy constructor question
 

"Ilia Poliakov" <ipoliak@allesklar.de> wrote in message news:bidd4d$7kq1h$1@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) {
}



Kevin Saff 08-25-2003 07:28 PM

Re: copy constructor question
 

"Ilia Poliakov" <ipoliak@allesklar.de> wrote in message
news:bidd4d$7kq1h$1@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.




Kevin Goodsell 08-25-2003 11:00 PM

Re: copy constructor question
 
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.


Liu Jian 08-25-2003 11:29 PM

Re: copy constructor question
 

"Ilia Poliakov" <ipoliak@allesklar.de> wrote in message
news:bidd4d$7kq1h$1@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
>
>





All times are GMT. The time now is 04:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.