C++, copy constructor

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

#include <iostream>

using namespace std;

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

      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

    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

Question by:panJames
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 22

Accepted Solution

ambience earned 500 total points
ID: 38788153

void display(Line obj)

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

Expert Comment

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;
LVL 22

Expert Comment

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

739 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