473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Jul 19 '05 #1
4 3166

"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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
3
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
1
by: Visame | last post by:
#include <iostream> using namespace std; class A { public: A() {cout << "constructor A" << endl;} A(A& a) {cout<<"copy A"<<endl;} virtual void Test(){cout <<"A test"...
6
by: suresh | last post by:
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.