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 <iostream>
using namespace std;
class base
{
char s;
public:
base( ){cout<< "constructi on" << endl;}
base(const char a ) {cout << "constructi on with arg" << endl;}
base(const base & a){cout <<"copy constructor" << endl;}
base & operator=(const base & a){cout << "assignment operator" <<
endl;}
~base( ) {cout << "destructio n" << endl;}
};
main ( )
{
base b1 = 'x'; //why copy constructor is NOT called here as given in
books?
base b2 = b1; //copy constructor is called
} 6 1813
On Apr 15, 1:15*pm, suresh <suresh.amritap ...@gmail.comwr ote:
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 <iostream>
using namespace std;
class base
{
* char s;
public:
* base( ){cout<< "constructi on" << endl;}
* base(const char a ) {cout << "constructi on with arg" << endl;}
* base(const base & a){cout <<"copy constructor" << endl;}
* base & operator=(const base & a){cout << "assignment operator" <<
endl;}
* ~base( ) {cout << "destructio n" << endl;}
};
main ( )
{
* base b1 = 'x'; //why copy constructor is NOT called here as given in
books?
* base b2 = b1; //copy constructor is called
}- Hide quoted text -
Hi.
For this particular case your constructor that takes a char as an
argument is actually the one supposed to be called. Why should your
copy constructor be called? You're not creating a copy...
--
Leandro T. C. Melo
On 2008-04-15 12:23:16 -0400, ltcmelo <lt*****@gmail. comsaid:
On Apr 15, 1:15*pm, suresh <suresh.amritap ...@gmail.comwr ote:
>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 <iostream> using namespace std; class base { * char s; public: * base( ){cout<< "constructi on" << endl;} * base(const char a ) {cout << "constructi on with arg" << endl;} * base(const base & a){cout <<"copy constructor" << endl;} * base & operator=(const base & a){cout << "assignment operator" << endl;} * ~base( ) {cout << "destructio n" << endl;}
};
main ( ) { * base b1 = 'x'; //why copy constructor is NOT called here as given in
>books? * base b2 = b1; //copy constructor is called
For this particular case your constructor that takes a char as an
argument is actually the one supposed to be called. Why should your
copy constructor be called? You're not creating a copy...
In fact, it is creating a copy. For
base b1 = 'x';
the compiler should construct a temporary object of type base from the
argument 'x', then copy that temporary into b1. However, the answer to
the original question is that the compiler is permitted to elide the
copy if the copy constructor has no side effects, provided that this
does not turn invalid code into valid code. For example, if the copy
constructor is private (try it!), then this initialization is illegal,
regardless of whether the compiler elides the copy constructor.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
Pete Becker wrote:
the compiler should construct a temporary object of type base from the
argument 'x', then copy that temporary into b1. However, the answer to
the original question is that the compiler is permitted to elide the
copy if the copy constructor has no side effects,
Actually, the compiler is also allowed to do this if the constructor does
have side effects. Printing to cout is a side effect.
On Apr 15, 1:32*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-04-15 12:23:16 -0400, ltcmelo <ltcm...@gmail. comsaid:
On Apr 15, 1:15*pm, suresh <suresh.amritap ...@gmail.comwr ote:
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 <iostream>
using namespace std;
class base
{
* char s;
public:
* base( ){cout<< "constructi on" << endl;}
* base(const char a ) {cout << "constructi on with arg" << endl;}
* base(const base & a){cout <<"copy constructor" << endl;}
* base & operator=(const base & a){cout << "assignment operator" <<
endl;}
* ~base( ) {cout << "destructio n" << endl;}
};
main ( )
{
* base b1 = 'x'; //why copy constructor is NOT called here as givenin
books?
* base b2 = b1; //copy constructor is called
For this particular case your constructor that takes a char as an
argument is actually the one supposed to be called. Why should your
copy constructor be called? You're not creating a copy...
In fact, it is creating a copy. For
base b1 = 'x';
the compiler should construct a temporary object of type base from the
argument 'x', then copy that temporary into b1. However, the answer to
the original question is that the compiler is permitted to elide the
copy if the copy constructor has no side effects, provided that this
does not turn invalid code into valid code. For example, if the copy
constructor is private (try it!), then this initialization is illegal,
regardless of whether the compiler elides the copy constructor.
Yes, you're correct (thanks for the note). In fact I think I had a
problem with g++ a few years ago because of that. Cant't remember
exactly what it was...
--
Leandro T. C. Melo
On 2008-04-15 12:38:56 -0400, Rolf Magnus <ra******@t-online.desaid:
Pete Becker wrote:
>the compiler should construct a temporary object of type base from the argument 'x', then copy that temporary into b1. However, the answer to the original question is that the compiler is permitted to elide the copy if the copy constructor has no side effects,
Actually, the compiler is also allowed to do this if the constructor does
have side effects. Printing to cout is a side effect.
Yup.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
suresh wrote:
...
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
...
Because the language specification explicitly allows the compiler to
optimize away the copying of temporary objects, even if the
copy-constructor has any side-effects.
In other words, the constructor might get called. Or it might not get
called. It might depend on the compiler, on the compiler settings and
even on the concrete context in the code. Just don't rely on it.
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: A |
last post by:
Hi,
A default copy constructor is created for you when you don't specify one
yourself. In such case, the default copy constructor will simply do a
bitwise copy for primitives (including pointers) and for objects types call
their default constructor.
Any others points i should know?
|
by: Kench |
last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
hour ago still it does not show up there so posting this here.
Hi,
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?
|
by: trying_to_learn |
last post by:
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...
|
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 or
the default constructor is automatically called instead"
Why cant the compiler do this on its own. if we are making an object
through copr construction for an inherited class , then why not simply
call the corresponding copy constructors for...
| |
by: bipod.rafique |
last post by:
Hello all,
Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:
class aclass{
public:
aclass(){
|
by: Tony Johansson |
last post by:
Hello Experts!
I have this constructor for class Flight.
Flight::Flight(string flight_no, Klocka dep_t, Klocka arr_t)
: no(flight_no), dep(dep_t), arr(arr_t) {}
Both dep and arr are objects of the Klocka class in the definition of the
Flight class.
|
by: vj |
last post by:
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;
|
by: skishorev |
last post by:
Hi,
I know What is the copy constructor. I don't know where and why we have
to use copy constructor. If You know please give to me with a situation
where we have to use or with a small example.
I am waiting for your reply.
Thanks & Regards,
Sai Kishore
|
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...
|
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,...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |