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

Copy constructor being called after one argument constructor

msinl
1
Hi guys,
Am a newbie in C++. Can you guys please tell why the copy constructor is being called after the supplied one argument constructor is called?

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   private:
  4.    int value;
  5.  
  6.   public:
  7.     A(int val) { value = val; std::cout << "\nOne argument constructor called";}
  8.     A() { value = 0; std::cout << "\n0 argument constructor called";}
  9.     A(A& b) { *this = b; std::cout << "\nCopy constructor called " << this;}
  10.  
  11.   };
  12.  
  13.   int main()
  14.   {
  15.     int hi;
  16.         A b = A(2);  //why is copy constructor also called after one argument constructor
  17.     std::cin >> hi;
  18.     return 0;
  19.   }
  20.  
Oct 6 '07 #1
2 1886
RRick
463 Expert 256MB
In your code, you are really invoking two compiler calls.
Expand|Select|Wrap|Line Numbers
  1. A b = A(2);  //why is copy
First, you create a temporary object with A(2) which calls A( int) and then you assign the temporary object to another object b, which invokes the copy constructor. If you want to only call A( int) then use:
Expand|Select|Wrap|Line Numbers
  1. A b(2);  //  only A( int) called
You can delete the copy constructor and get the same results because the compiler will supply a default shallow copy constructor. Also, you need to add a const to your copy constructor, because my compiler (GNU g++) produces an odd error without it. The copy constructor should be:
Expand|Select|Wrap|Line Numbers
  1. A( const A & b) ...
Oct 6 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Almose correct:
A b = A(2); //why is copy

First, you create a temporary object with A(2) which calls A( int) and then you assign the temporary object to another object b, which invokes the copy constructor.
There is not assignment here as there is no LVAL. The object b is being created and assignment cannot occur until after the object has been created.

Here A(2) calls the A(int) coinstructor to create a temp object. This temp object is used to call A(A) to intialize b.

There are just the two constructor calls.
Oct 6 '07 #3

Sign in to post your reply or Sign up for a free account.

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...
15
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...
11
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not...
14
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...
3
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(){
4
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...
2
by: pragtideep | last post by:
Kindly help me explain the behaviour of defult copy constructor . Why the destructor is freeing the SAME memory twice , though it was allocated just once . #include<iostream> using namespace...
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.