473,890 Members | 1,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy construction

i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy means a 'C' like method
of simply pushing the arguments into stack. accdng to me when fn 'f' is
over, it should simply mean that stack (local object that was made by
value)is popped out without calling its detructor.

i hope im not asking a stupid qn. but im confused abt this fundamental.

-----------------*heres the code*------------------------------

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCoun t = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany ()");
}
};
int HowMany::object Count = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print( "after construction of h");
HowMany h2 = f(h);
HowMany::print( "after call to f()");
} ///:~
Jul 22 '05 #1
14 2000
trying_to_learn wrote:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy means a 'C' like method
of simply pushing the arguments into stack. accdng to me when fn 'f' is
over, it should simply mean that stack (local object that was made by
value)is popped out without calling its detructor.

i hope im not asking a stupid qn. but im confused abt this fundamental.

-----------------*heres the code*------------------------------

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCoun t = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany ()");
}
};
int HowMany::object Count = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print( "after construction of h");
HowMany h2 = f(h);
HowMany::print( "after call to f()");
} ///:~

The reason is that, because you don't have a user defined copy
constructor for class HowMany, the compiler generated one for you (of
course you cannot see any sign of it being called). When h is passed by
value, the automatically augmented copy constructor, whose behavior in
this case is simply doing bitwise copy, is called, though it cannot see
it being called. So actually the symmetry still exists, with the copy
constructor invisible to you.
Jul 22 '05 #2
trying_to_learn wrote:
I am [reading] the chapter on copy construction in C++. In the code (see below),
the author says [that], if I pass the object by value as in HowMany h2 = f(h),
then a bitwise object is created w/o calling the constructor of the class.
However, he says [that], when [the thread of execution]
leaves scope of function f(HowMany), then the destructor is called.
To make a copy of h to "pass by value".
Why this inconsistency? According to me even the destructor should *not*
be called. I can understand that bit wise copy means a 'C' like method
of simply pushing the arguments into stack.

According to me, when f(HowMany) returns, it should simply mean that the stack
(local object that was made by value) is popped out without calling its destructor.
Actually, your C/C++ compiler emits code to do this automatically.
But it calls your "destructor " to "clean up" first.
If, for example, your constructor called new to allocate memory for a
pointer data member in one of your objects,
your destructor should call delete to deallocate that memory
*before* the object is "popped off of the stack".
I hope [that] I'm not asking a stupid question
but I'm confused about this fundamental. -----------------*heres the code*------------------------------

cat main.cc #include <iostream>

class HowMany {
private:
static int objectCount;
public:
static void print(const std::string& msg = "") {
if(0 != msg.size())
std::clog << msg << ": ";
std::clog << "objectCoun t = " << objectCount << std::endl;
}
HowMany(void) {
++objectCount;
print("HowMany: :HowMany(void)" );
}
HowMany(const HowMany& n) {
++objectCount;
print("HowMany: :HowMany(const HowMany&)");
}
~HowMany(void) {
--objectCount;
print("HowMany: :~HowMany(void) ");
}
};

int HowMany::object Count = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f(HowMany)");
return x;
}

int main(int argc, char* argv[]) {
HowMany h;
HowMany::print( "after construction of h");
HowMany h2 = f(h);
HowMany::print( "after call to f()");
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

HowMany::HowMan y(void): objectCount = 1
after construction of h: objectCount = 1
HowMany::HowMan y(const HowMany&): objectCount = 2
x argument inside f(HowMany): objectCount = 2
HowMany::HowMan y(const HowMany&): objectCount = 3
HowMany::~HowMa ny(void): objectCount = 2
after call to f(): objectCount = 2
HowMany::~HowMa ny(void): objectCount = 1
HowMany::~HowMa ny(void): objectCount = 0
Jul 22 '05 #3

"trying_to_lear n" <no****@no.no > wrote in message
news:cn******** **@gist.usc.edu ...
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value as
in HowMany h2 = f(h); ....then a bitwise object is created w/o calling
the constructor of the class.
That's rubbish. In C++ class objects are *never* created without calling a
class constructor. Probably the point the author is trying to make is that
in the example below it is the compiler generated copy constructor that is
called. But even then the author should appeciate that a compiler generated
copy constructor is *not* a bitwise copy and there is no such thing as a
bitwise object.

This sound like Bullschildt, who is the author?

If you add your own copy constructor to HowMany, then you will see that it
is called when you pass an object by value.

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }

// copy constructor
HowMany(const HowMany& rhs) { objectCount++; }
However he says when we leave scope of function HowMany f(HowMany x) then
the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not* be
called. i can understand that bit wise copy means a 'C' like method of
simply pushing the arguments into stack. accdng to me when fn 'f' is over,
it should simply mean that stack (local object that was made by value)is
popped out without calling its detructor.
Everytime a class object is created a constructor is called (even if it is a
compiler generated one). Every time a class object is destroyed a destructor
is called (even if it is a compiler generated one). There is no such thing
as a bitwise object or a bitwise copy in C++.

i hope im not asking a stupid qn. but im confused abt this fundamental.


No I think you have a stupid author who is trying to make the simple
complicated.

john
Jul 22 '05 #4
"trying_to_lear n" <no****@no.no > schrieb im Newsbeitrag news:cn******** **@gist.usc.edu ...
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class.


That is simply wrong. If you don't implement your own copy constructor, your compiler will do that for you, and it will call that constructor. However, this compiler generated copy constructor does not do a bitwise copy. It does a memberwise copy, that is, it calls the copy constructors for all data members.

HTH
Heinz
Jul 22 '05 #5
trying_to_learn wrote:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy means a 'C' like method
of simply pushing the arguments into stack. accdng to me when fn 'f' is
over, it should simply mean that stack (local object that was made by
value)is popped out without calling its detructor.

i hope im not asking a stupid qn. but im confused abt this fundamental.

-----------------*heres the code*------------------------------

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCoun t = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany ()");
}
};
int HowMany::object Count = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print( "after construction of h");
HowMany h2 = f(h);
HowMany::print( "after call to f()");
} ///:~

Thankyou all for ure replies
I am quoting directly from the book thinking in C++ and have put the
sentence in bold where the author says bitcopy is used and constructor
is not called.

Quote Start:
"Look at the point inside f( ), which occurs after the argument is
passed by value. This means the original object h exists outside the
function frame, and there’s an additional object inside the function
frame, which is the copy that has been passed by value. However,
the argument has been passed using C’s primitive notion of
bitcopying, whereas the C++ HowMany class requires true
initialization to maintain its integrity, so the default bitcopy fails to
produce the desired effect.
When the local object goes out of scope at the end of the call to f( ),
the destructor is called, which decrements objectCount, so outside
the function, objectCount is zero. *The creation of h2 is also
performed using a bitcopy, so the constructor isn’t called there
either*, and when h and h2 go out of scope, their destructors cause
the negative values of objectCount."

End Quote
Jul 22 '05 #6
trying_to_learn wrote:
Quote Start:
"Look at the point inside f( ), which occurs after the argument is
passed by value. This means the original object h exists outside the
function frame, and there’s an additional object inside the function
frame, which is the copy that has been passed by value. However,
the argument has been passed using C’s primitive notion of
bitcopying, whereas the C++ HowMany class requires true
initialization to maintain its integrity, so the default bitcopy fails to
produce the desired effect.
When the local object goes out of scope at the end of the call to f( ),
the destructor is called, which decrements objectCount, so outside
the function, objectCount is zero. *The creation of h2 is also
performed using a bitcopy, so the constructor isn’t called there
either*, and when h and h2 go out of scope, their destructors cause
the negative values of objectCount."

End Quote


I think it's also correct to say that the copy constructor is not called
in this case. Although *conceptually* an augmented copy constructor is
provided by the compiler, it also can just insert the something like
push instructions into the code, to have the effect of bitwise copy.
But for your destructor, since its defined by yourself, it must be
executed. However, if you don't define it, the compiler would also
augment one for you, which may not do anything and thus skipped.
Please note that the compiler-augmented copy constructor *conceptually*
exists, but may or may not be actually.
Jul 22 '05 #7

"trying_to_lear n" <no****@no.no > wrote in message
news:cn******** **@gist.usc.edu ...
trying_to_learn wrote:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy means a 'C' like method
of simply pushing the arguments into stack. accdng to me when fn 'f' is
over, it should simply mean that stack (local object that was made by
value)is popped out without calling its detructor.

i hope im not asking a stupid qn. but im confused abt this fundamental.

-----------------*heres the code*------------------------------

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCoun t = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany ()");
}
};
int HowMany::object Count = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print( "after construction of h");
HowMany h2 = f(h);
HowMany::print( "after call to f()");
} ///:~ Thankyou all for ure replies
I am quoting directly from the book thinking in C++ and have put the
sentence in bold where the author says bitcopy is used and constructor
is not called.

Quote Start:
"Look at the point inside f( ), which occurs after the argument is
passed by value. This means the original object h exists outside the
function frame, and there’s an additional object inside the function
frame, which is the copy that has been passed by value. However,
the argument has been passed using C’s primitive notion of
bitcopying,


Since HowMany contains no data at all, its hard to see why the author thinks
a bit copy is occuring.
whereas the C++ HowMany class requires true
initialization to maintain its integrity, so the default bitcopy fails to
produce the desired effect.
The author is also incorrect in his implication that if you fail to provide
a copy constructor then the default is bitwise copying. It perfectly
possible to write a class where a bitwise copy would occur, but its also
possible to write one where it wouldn't. If you fail to provide a copy
constructor the default is *memberwise* copying not bitwise copying.
Memberwise copying may be the same as bitwise copying but it may not, it all
depends on the class

When the local object goes out of scope at the end of the call to f( ),
the destructor is called, which decrements objectCount, so outside
the function, objectCount is zero. *The creation of h2 is also
performed using a bitcopy, so the constructor isn’t called there
either*, and when h and h2 go out of scope, their destructors cause
the negative values of objectCount."

End Quote


I think the bottom line is that the author has written a deliberately bad
piece of C++ code to illustrate a point, and confused you in the process,
presumably because you don't think it should be possible to write bad code
like that. Unfortunately it is.

john
Jul 22 '05 #8
angelo wrote:
. Although *conceptually* an augmented copy constructor is
provided by the compiler, it also can just insert the something like
push instructions into the code, to have the effect of bitwise copy.


I would like to go back in time and remove whatever reference started
all this "bitwise" copy non-sense. C++ does not in theory or practice
ever copy things "bitwise."

You can't even make the assumption "conceptual ly." The lack of a user
defined copy constructor means that there is a subobject by subobject copy
performed. While in the case of POD types the compiler may, in fact,
do a block memory copy of some sort, if the class is NOT POD, the compiler
generated copy has to copy each member by it's respective copy constructor.

For exmaple:
struct foo {
std::string s;
};

The compiler can't "bitwise" copy anything here. It has to generate a
copy constructor that is the equivelent of:
foo::foo(const foo& that) s(that.s) { }
Jul 22 '05 #9
Ron Natalie wrote:
I would like to go back in time and remove whatever reference started
all this "bitwise" copy non-sense. C++ does not in theory or practice
ever copy things "bitwise." I admit that the term "bitwise" is not used for copy constructor in the
Standard, and that's what we say is "in theory", right?
But in practice you can see too many places, formally or informally,
occur the worde "bitwise".
You can't even make the assumption "conceptual ly." See the Standard 12.8.1, the second sentence is:
*Conceptually*, these operations are implemented by a copy constructor
and a copy assignment operator.
For exmaple:
struct foo {
std::string s;
};

The compiler can't "bitwise" copy anything here. It has to generate a
copy constructor that is the equivelent of:
foo::foo(const foo& that) s(that.s) { }

Of course so. But I guess most of us do know about this. What we are
talking about is when the class *is* POD. See Inside the C++ Object
Model 2.2,
----------------------------------------------------------------------
How is this operation in practice carried out? The original ARM tells us:
Conceptually, for a class X [this operation is] implemented by…a copy
constructor.
The operative word here is conceptually. The commentary that follows
explains:
In practice, a good compiler can generate bitwise copies for most
class objects since they have bitwise copy semantics….
That is, a copy constructor is not automatically generated by the
compiler for each class that does not explicitly define one. Rather, as
the ARM tells us,
Default constructors and copy constructors…ar e generated (by the
compiler) where needed.
Needed in this instance means when the class does not exhibit bitwise
copy semantics.
Jul 22 '05 #10

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

Similar topics

42
5821
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
12
1517
by: Bryan Bullard | last post by:
i have a class that contains a private pointer to a system resource. i'm quite sure that i will need to have a user defined copy-constructor to do a deep copy. can someone put me in the right direction (that is, simplified material or examples)? -bryan ps: the private variable that needs attention points to a critical section semaphore (used to synch access to the object) that is dynamically allocated/deallocated in ctor/dtor...
14
1994
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
3
10290
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain how I could declate/override the two. Thanx
10
2589
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects, this bypasses the problem or it seems to me like that. Could any one give me some simple examples...
2
1631
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's written that "Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permitted to treat the original and the copy as two different ways of...
9
1688
by: janzon | last post by:
Consider the code below. The output is the following two lines: 0xbfc78090 0xbfc780a0 This proves that the variable m in main() is not the very same instance of MyClass as temp_m in hello(). Hence (?) m is created as copy of temp_m. But the copy constructor is not called. Contradiction. Where am I thinking incorrectly?
15
2416
by: Victor Bazarov | last post by:
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int);
7
2168
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I need deep copy and I believe I'm doing that in my constructors. Can someone help me see what I'm missing out? Here is my code. typedef boost::shared_ptr<FactorFactorPtr; enum FactorTypeT
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9821
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9630
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8016
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.