473,396 Members | 1,996 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,396 software developers and data experts.

Interesting Copy Constructors!!

vj

Hi! I recently came across this intresting behaviour shown by Visual
C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
this the standard behaviour shown by all compilers or its limited only
to VC++.
Listing 1
==================
#include <iostream>
using namespace std;
class Class1
{
int i;
public:
Class1(int ii)
{i=ii;cout<<"Class1::Parameteized Constructor called with
"<<ii<<endl;}

int geti(){return i;}

Class1(Class1 & c)
{i=c.i;cout<<"Class1::Copy Constructor called with "<<i<<endl;}
};

ostream & operator<<(ostream & os,Class1 & c)
{return os<<c.geti();}

Class1 getNext(Class1 c)
{Class1 cc(c.geti()+1);
return cc;
}

void main()
{Class1 c(0);
cout<<getNext(c);
cin.get();
}

Output
===============
Class1::Parameteized Constructor called with 0
Class1::Copy Constructor called with 0
Class1::Parameteized Constructor called with 1
Class1::Copy Constructor called with 1
1
Listing 2
===============
/* same class but diffrent get getNext & main functions*/
Class1 getNext(Class1 c)
{return Class1(c.geti()+1);}

void main()
{cout<<getNext(Class1(0));
cin.get();
}

output
================
Class1::Parameteized Constructor called with 0
Class1::Parameteized Constructor called with 1 //!!Strange No Copy
Constructor
1

It seems that the compiler is bypassing the copy constructor when
creating a temporary object. It does seems sense as there no need to
call the copy constructor as the object is already created and
gauranteed to be unmodified, So it passes the object itself to the
function.

However I am not sure that this behaviour a standard behaviour of copy
constructors?
Thanks,
VJ

Oct 27 '05 #1
7 1585
On 26 Oct 2005 20:37:31 -0700, "vj" <va*******@hotmail.com> wrote:

Hi! I recently came across this intresting behaviour shown by Visual
C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
this the standard behaviour shown by all compilers or its limited only
to VC++.
Listing 1
==================
#include <iostream>
using namespace std;
class Class1
{
int i;
public:
Class1(int ii)
{i=ii;cout<<"Class1::Parameteized Constructor called with
"<<ii<<endl;}

int geti(){return i;}

Class1(Class1 & c)
{i=c.i;cout<<"Class1::Copy Constructor called with "<<i<<endl;}
};

ostream & operator<<(ostream & os,Class1 & c)
{return os<<c.geti();}

Class1 getNext(Class1 c)
{Class1 cc(c.geti()+1);
return cc;
}

void main()
{Class1 c(0);
cout<<getNext(c);
cin.get();
}

Output
===============
Class1::Parameteized Constructor called with 0
Class1::Copy Constructor called with 0
Class1::Parameteized Constructor called with 1
Class1::Copy Constructor called with 1
1
Listing 2
===============
/* same class but diffrent get getNext & main functions*/
Class1 getNext(Class1 c)
{return Class1(c.geti()+1);}

void main()
{cout<<getNext(Class1(0));
cin.get();
}

output
================
Class1::Parameteized Constructor called with 0
Class1::Parameteized Constructor called with 1 //!!Strange No Copy
Constructor
1

It seems that the compiler is bypassing the copy constructor when
creating a temporary object. It does seems sense as there no need to
call the copy constructor as the object is already created and
gauranteed to be unmodified, So it passes the object itself to the
function.

However I am not sure that this behaviour a standard behaviour of copy
constructors?
Thanks,
VJ


It is an opional optimization, and it is compiler dependent. Some
compilers may have optimized the first case to be exactly the same as
your second case.
Oct 27 '05 #2

"vj" <va*******@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
|
| Hi! I recently came across this intresting behaviour shown by Visual
| C++ 6.0 compiler regarding Copy Constructors. Please tell me that is
| this the standard behaviour shown by all compilers or its limited only
| to VC++.

Allow me to focus on your class design instead...

|
|
| Listing 1
| ==================
| #include <iostream>
| using namespace std;
| class Class1
| {
| int i;
| public:
| Class1(int ii)
| {i=ii;cout<<"Class1::Parameteized Constructor called with
| "<<ii<<endl;}
|
| int geti(){return i;}

int geti() const
{
return i;
}

see section 18.10 in the faq
http://www.parashift.com/c++-faq-lit...rrectness.html

|
| Class1(Class1 & c)

Class1(const Class1& c)

| {i=c.i;cout<<"Class1::Copy Constructor called with "<<i<<endl;}
|

friend std::ostream& operator<<(std::ostream& os, const Class1& c);

|
| };
|
| ostream & operator<<(ostream & os,Class1 & c)

std::ostream& operator<<(std::ostream& os, const Class1& c)
{
os << c.i;
return os;
}

gee, all of a sudden, you don't even need geti()

| {return os<<c.geti();}
|
| Class1 getNext(Class1 c)
| {Class1 cc(c.geti()+1);
| return cc;
| }

What if getNext(..) attempts to "get" a Class1 node that doesn't exist?
The creator of the class is responsible to supply the appropriate
robustness to cover that eventuality. This should not be left to the
user of the class.

You'ld have a more robust design if you relied on an STL container to
store the instances of Class1 and iterators to get next valid elements.
With iterators, one past the last element in either direction will
evaluate to null. This greatly simplifies your class's design (and
eventually: a container's design).

ie (not tested):

// test.cpp
#include "Class1.h" // without geti() or getNext(...)
#include <iostream>
#include <ostream>
#include <list> // or vector, queue, deque

int main()
{
std::list< Class1 > clist;
clist.push_back(0);
clist.push_back(1);
clist.push_back(2);

// std::copy would be useful here, but for the sake of
// explaining iterators and their benefits...

typedef std::list< Class1 >::iterator CIter;
CIter c_iter = clist.begin(); // where *c_iter is now the first
element

// the for loop remains unchanged regardless of clist's actual size
!
for( c_iter; c_iter != clist.end(); ++c_iter)
{
std::cout << *c_iter << " ";
}
std::endl;
}

<snip>
Oct 27 '05 #3
> However I am not sure that this behaviour a standard behaviour of copy
constructors?

Yes, it is. It is standard.

--
Michael Kochetkov
Oct 27 '05 #4
vj
Dear friends,

Thanks for your support, but i think that there is wierd scenario here.
On one side Zara says that this is not a standard but mearly an
optional optimization . However Michael Kochetkov says that it is the
standard behaviour. Please don't let me confused.... What i really need
is a definite answer.

Thanks,
VJ

Oct 27 '05 #5

Peter_Julian wrote:
...

You'ld have a more robust design if you relied on an STL container to
store the instances of Class1 and iterators to get next valid elements.
With iterators, one past the last element in either direction will
evaluate to null. This greatly simplifies your class's design (and
eventually: a container's design).
ie (not tested):

// test.cpp
#include "Class1.h" // without geti() or getNext(...)
#include <iostream>
#include <ostream>
#include <list> // or vector, queue, deque

int main()
{
std::list< Class1 > clist;
clist.push_back(0);
clist.push_back(1);
clist.push_back(2);

// std::copy would be useful here, but for the sake of
// explaining iterators and their benefits...

typedef std::list< Class1 >::iterator CIter;
CIter c_iter = clist.begin(); // where *c_iter is now the first
element

// the for loop remains unchanged regardless of clist's actual size
!
for( c_iter; c_iter != clist.end(); ++c_iter)
{
std::cout << *c_iter << " ";
}
std::endl;
}


An iterator advanced to the end of a sequence is not guaranteed (or
even be likely) to evaluate to NULL. Such an iterator cannot even be
safely dereferenced. It can be compared to another iterator in the same
container, usually the end() iterator as the example provided
demonstrates.

Greg

Oct 27 '05 #6
vj wrote:
Dear friends,

Thanks for your support, but i think that there is wierd scenario here.
On one side Zara says that this is not a standard but mearly an
optional optimization . However Michael Kochetkov says that it is the
standard behaviour. Please don't let me confused.... What i really need
is a definite answer.


The standard *allows* elimination of copy constructors in certain
circumstances but it does not *require* the copy constructor to be bypassed
in these case. Thus, the behavior you observed is blessed but not enforced
by the standard. The reason that the standard allows elimination of copy
constructors in some cases is indeed to ease optimization.
Best

Kai-Uwe Bux

Oct 27 '05 #7
> Dear friends,

Thanks for your support, but i think that there is wierd scenario
here. On one side Zara says that this is not a standard but mearly an
optional optimization . However Michael Kochetkov says that it is the
standard behaviour. Please don't let me confused.... What i really
need is a definite answer.

In addition to the answer above: though a compiler can eliminate objects
copying during, for example, return value optimization the publicly available
copy constructor (explicitly or implicitly declared) is a must in this case.
Even if it is never called at all.

--
Michael Kochetkov
Oct 27 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.