Solved

C++, copy constructor

Posted on 2013-01-17
3
430 Views
Last Modified: 2013-01-18
Hello experts, please have a look at the following code:


#include <iostream>

using namespace std;

class Line
{
   public:
      int getLength( void );
      Line( int len );             // simple constructor, headers only
      Line( const Line &obj);      // copy constructor, headers only
      ~Line();                      // destructor

   private:
      int *ptr;
};

Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    // allocate memory for the pointer;
    ptr = new int;
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << endl;
    ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

Line::~Line(void)
{
    cout << "Freeing memory!" << endl;
    delete ptr;
}
int Line::getLength( void )
{
    return *ptr;
}

void display(Line obj)
{
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main( )
{
   Line line(10);

   display(line);//****** why does it call copy constructor here? ******

   return 0;
}

I do not understand why calling 'display' with 'line' as argument makes a call to constructor here...

Any help?

Thank you

panJames
0
Comment
Question by:panJames
  • 3
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 38788153
because

void display(Line obj)

does not take Line by reference. It takes it by value so a copy happens
0
 
LVL 22

Expert Comment

by:ambience
ID: 38788160
void display(Line& obj)

should not invoke the constructor.

Looking at the purpose of display its better to have it like

void display(const Line& obj)

but that wont compile unless you also change

int getLength( void ) const;

int Line::getLength( void ) const
{
    return *ptr;
}
0
 
LVL 22

Expert Comment

by:ambience
ID: 38788176
As a general rule, always consider passing objects as const references unless there is a compelling reason not to do so. Passing it by reference ensures there is no overhead of copying and const ensure there are no side-affects because you can only access const methods and data on the object.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now