Why was copy constructor called in this example?

FelB

I am very new to C++ and I don't understand why the copy constructor was called in the following code:

 #include <iostream>

    using namespace std;

    class Line

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

   private:
      int *ptr;
};

// Member functions definitions including constructor
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);

   return 0;
}

And when it runs:

Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!

Why does it construct an other Line object after the first one with the simple constructor?

Thanks!

Jean-François Fabre

because you are passing Line as value

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

You should pass it as constant reference, which is faster (and display shoud be a constant method so constant objects can call it).

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

(provided that you change to int getLength( void ) const;)

And in your case, think of redefining assignment operator

Line &operator=(const Line &other);

or affecting a line in another will copy pointer data and will crash on your second object deletion since memory will be freed twice.

If you want to make sure that default copy constructor / assignment operator cannot be called, just declare them in your private part:

private:
  Line &operator=(const Line &other);
  Line(const Line &other);

code trying to use them won't compile.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Why copy constructor was called twice in following program?

来自分类Dev

Why move constructor does not called without std::move?

来自分类Dev

why doesn't my c# base constructor get called?

来自分类Dev

Variadic template constructor and copy constructor

来自分类Dev

Are copy constructor and operator= a must?

来自分类Dev

std::vector initialization move/copy constructor of the element

来自分类Dev

Does deleting a copy constructor or copy assignment operator count as "user declared"?

来自分类Dev

向量push_back多次调用copy_constructor吗?

来自分类Dev

Why does this CodeLab Example in Polymer not work?

来自分类Dev

Why will GCC copy word into the return register but not byte?

来自分类Dev

Why does this example from the myHDL manual give me different results?

来自分类Dev

Why is my simple Struts2 example not working?

来自分类Dev

Why view.onLayout() is called repetitively when changed=false?

来自分类Dev

Why does shouldChangeTextInRange get called multiple times using predictive input?

来自分类Dev

Why does a mule rollback exception propagate to the flow that called it?

来自分类Dev

Why do we need a user-provided constructor for a const object?

来自分类Dev

Why isn't my class constructor initializing its member variables?

来自分类Dev

Why do I need another constructor in an extended abstract class?

来自分类Dev

如何在不调用copy-constructor的情况下向vector添加元素?

来自分类Dev

如何在不调用copy-constructor的情况下向vector添加元素?

来自分类Dev

创建对象向量时,如何将参数传递给Default或Copy Constructor以初始化值?

来自分类Dev

cellForRowAtIndexPath not called but numberOfRowsInSection called

来自分类Dev

[email protected]”->“ example” PHP

来自分类Dev

XML <example>标记,带有:(<m:example>)

来自分类Dev

更改[] .__ proto __。constructor和[] .constructor的行为不同

来自分类Dev

std::copy raw data to cout in hex format using ostream_iterator<uint8_t> prints out unformatted data. Why?

来自分类Dev

Working maven obfuscate example

来自分类Dev

Is it safe to use == on FP in this example

来自分类Dev

Convert curl example to pycurl

Related 相关文章

  1. 1

    Why copy constructor was called twice in following program?

  2. 2

    Why move constructor does not called without std::move?

  3. 3

    why doesn't my c# base constructor get called?

  4. 4

    Variadic template constructor and copy constructor

  5. 5

    Are copy constructor and operator= a must?

  6. 6

    std::vector initialization move/copy constructor of the element

  7. 7

    Does deleting a copy constructor or copy assignment operator count as "user declared"?

  8. 8

    向量push_back多次调用copy_constructor吗?

  9. 9

    Why does this CodeLab Example in Polymer not work?

  10. 10

    Why will GCC copy word into the return register but not byte?

  11. 11

    Why does this example from the myHDL manual give me different results?

  12. 12

    Why is my simple Struts2 example not working?

  13. 13

    Why view.onLayout() is called repetitively when changed=false?

  14. 14

    Why does shouldChangeTextInRange get called multiple times using predictive input?

  15. 15

    Why does a mule rollback exception propagate to the flow that called it?

  16. 16

    Why do we need a user-provided constructor for a const object?

  17. 17

    Why isn't my class constructor initializing its member variables?

  18. 18

    Why do I need another constructor in an extended abstract class?

  19. 19

    如何在不调用copy-constructor的情况下向vector添加元素?

  20. 20

    如何在不调用copy-constructor的情况下向vector添加元素?

  21. 21

    创建对象向量时,如何将参数传递给Default或Copy Constructor以初始化值?

  22. 22

    cellForRowAtIndexPath not called but numberOfRowsInSection called

  23. 23

    [email protected]”->“ example” PHP

  24. 24

    XML <example>标记,带有:(<m:example>)

  25. 25

    更改[] .__ proto __。constructor和[] .constructor的行为不同

  26. 26

    std::copy raw data to cout in hex format using ostream_iterator<uint8_t> prints out unformatted data. Why?

  27. 27

    Working maven obfuscate example

  28. 28

    Is it safe to use == on FP in this example

  29. 29

    Convert curl example to pycurl

热门标签

归档