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

Why overloaded copy constructor is not getting called here?

6
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. class Cents
  4. {
  5.  
  6. public:
  7.   int m_nCents;
  8.    Cents(int nCents=0):m_nCents(nCents)
  9.     {
  10.        cout<<"Calling normal constructor with value:";   m_nCents = nCents;
  11.        cout<<m_nCents<<endl;
  12.     }
  13.  
  14.     // Copy constructor
  15.     Cents(const Cents& cSource)
  16.     {
  17.         cout<<"Calling copy construct\n";
  18.         m_nCents = cSource.m_nCents;
  19.        std::cout<<m_nCents<<std::endl;
  20.     }
  21. };
  22.  
  23.  
  24. int main(){
  25.  
  26.  
  27. Cents obj2 = 37;
  28. cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents;
  29.  
  30. return 0;
  31. }

o/p is
Calling normal constructor with value:37
Value of m_nCents with sobject2:37

Question is :
Why is the overloaded copy constructor that I have written not getting called here?Internally default copy constructor is getting called.Thats why we get
value of obj2.m_nCents as 37.
~
~
Nov 19 '12 #1

✓ answered by weaknessforcats

You are not correct. There is this constructor:

Expand|Select|Wrap|Line Numbers
  1. Cents(int nCents=0):m_nCents(nCents)
This already has an int argument. The compiler will pick this one as the better fit.

6 1666
weaknessforcats
9,208 Expert Mod 8TB
In order for a copy constructor to be called, there must be an object to copy. In your example you are creating an object and initializing it with 37. That would be a call to a constructor that has an int argument rather than a Cents& argument.
Nov 19 '12 #2
If you want to call a copy constructor you need to write the below code in your main function.

Cents obj1=37;

Cents obj2=obj1;
(or)
Cents obj2(obj1);

cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents;


Output:

Value of m_nCents with sobject2:37


Copy Constructor will be called at the time of initialisation of object with another object of same class type.
Nov 20 '12 #3
subi09
6
Thanks for the reply .But as per my understanding,

Cents obj2 = 37;=> statement will create a temporary object of Cents and initialise it with 37(that is where constructor is called) and then it will copy the value of that object to obj2(at this time copy constructor should be called).



Please clarify this and correct me if am wrong.
Nov 20 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
You are not correct. There is this constructor:

Expand|Select|Wrap|Line Numbers
  1. Cents(int nCents=0):m_nCents(nCents)
This already has an int argument. The compiler will pick this one as the better fit.
Nov 20 '12 #5
subi09
6
Then if am adding 2 piece of code extra to the above like below

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. class Cents
  4. {
  5.  
  6. public:
  7. int m_nCents;
  8. Cents(int nCents=0):m_nCents(nCents)
  9. {
  10. cout<<"Calling normal constructor with value:"; m_nCents = nCents;
  11. cout<<m_nCents<<endl;
  12. }
  13.  
  14. // Copy constructor
  15. Cents(const Cents& cSource)
  16. {
  17. cout<<"Calling copy construct\n";
  18. m_nCents = cSource.m_nCents;
  19. std::cout<<m_nCents<<std::endl;
  20. }
  21. friend void display(Cents obj);-->newly added
  22. };
  23.  
  24. void display(Cents obj){}
  25.  
  26.  
  27. int main(){
  28.  
  29. //Cents obj2 = 37;
  30. display(37);---> newly added
  31. // cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents;
  32.  
  33. return 0;
  34. }

O/p>
Calling normal constructor with value:37
Calling copy construct
37

Question is

then why in this case. display(37),first calls constructor,then copy constructor.As per the above explanation,it should stop with call to constructor.This scenario actually is the root cause of my above doubt.It would be very helpful if you can clear this.
Nov 20 '12 #6
weaknessforcats
9,208 Expert Mod 8TB
I have no idea.

The display(37) should call Cents::Cents(int). That will initialize the Cents object created as the function argument. There will be no copy constructor call.

I did compile and link your code and ran it using Visual Studio.NET 2008 and there is just the Cents::Cents(int) call. The copy constructor is not called.
Nov 20 '12 #7

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

Similar topics

6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
2
by: puzzlecracker | last post by:
B.S. page 271: class complex { double re, im; ... public: ... }; comlex::comple(complex c):re(c.re), im(c.im){} //error
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(){
13
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
2
msinl
by: msinl | last post by:
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? class A { private: int value; ...
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...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.