2

I have this code. I'm not that proficient in C++, so I'm going to ask several questions:

#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;
   cout<<&obj<<endl;
   *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() {

   int a = 10;
   cout<<&a<<endl;
   Line line(a);

   display(line);

   return 0;
}
  1. Here I don't see where we call copy contructor.
  2. Destructor is being called twice. Where is the second object is being created?
  3. Line::Line(const Line &obj) receives address of a as a parameter? I guess not. But why? a by itself is not an instance of Line, so why function accepts that?
  4. What would be the difference between Line::Line(const Line &obj) and Line::Line(const Line obj)
  5. And could you please explain this *ptr = *obj.ptr; . From here i know only lhs, which dereferences ptr (aka sets value of an object). But I didn't get what's in rhs?

I'd appreciate if you explain with less technical terms and more examples, for clarity

Above code outputs this:

0x7fff692196a4
Normal constructor allocating ptr
Copy constructor allocating ptr.
0x7fff69219698
Length of line : 10
Freeing memory!
Freeing memory!
Yura
  • 85
  • 6
  • 1
    So what is the output of this program? How did you figure out that default constructor is called if it is not implemented here? Destructor called for `line` `main` function local variable and for `obj` `display` function argument (which is copy-constructed). – user7860670 May 07 '18 at 18:09
  • You don't have a default constructor. Are you talking about `Line(int)`? – R Sahu May 07 '18 at 18:11
  • Yes, sorry. Editing – Yura May 07 '18 at 18:12
  • `Line line(a)` is calling the `int` constructor, that should be obvious. – Mark Ransom May 07 '18 at 18:13
  • You still haven't explained where you see a call to a default constructor. In any case, what you need is a [book for beginners](https://stackoverflow.com/q/388242/241631). – Praetorian May 07 '18 at 18:13
  • No more default constructor in question. Updated it – Yura May 07 '18 at 18:14
  • 1
    @Yura Do you know what pass by value is? – NathanOliver May 07 '18 at 18:15
  • There is also a thing called debugger that allows you to execute code line-by line and figure out what is called where. – user7860670 May 07 '18 at 18:15
  • @NathanOliver , yes, I know. But I still don't feel confident about it – Yura May 07 '18 at 18:16
  • @Yura `void display(Line obj)` accepts it's argument by copy. `obj` is a *copy* of the object given as the argument (`line` in this case). – François Andrieux May 07 '18 at 18:18
  • @VTT , no all people smart enough to figure out things from debugger. If everyone was, number of questions here would reduce by 80% – Yura May 07 '18 at 18:21
  • @NathanOliver I would have closed this as "too broad". There are 5 separate questions here. Only one of them is a duplicate. – melpomene May 07 '18 at 18:22
  • 1
    @melpomene I felt all the questions stemmed from them not knowing that pasing by value causes a copy. – NathanOliver May 07 '18 at 18:26

1 Answers1

1

Here for some reason default constructor is called. I don't see any place where we create object without parameter (aka that goes to default constructor)

I don't see any place either! How can you be so sure the default constructor is called? In fact, your class is not constructible using a default constructor because there isn't one. If you try this:

Line l;

It won't compile since there is no default constructor.

Destructor is being called twice. Where is the second object is being created?

It this line:

display(line);

You function receives a value. To have a value both in your main and your display function, you have to copy it somehow. Passing line will copy it from your main to your function.

Line::Line(const Line &obj) receives address of a as a parameter? I guess not. But why? a by itself is not an instance of Line, so why function accepts that?

No. This is a reference. The copy constructor receive a reference from other context to copy it.

What would be the difference between Line::Line(const Line &obj) and Line::Line(const Line obj)

One is a reference, the other is passing by value (a copy, move or other construction). If the copy constructor had to receive a value, how would you copy the value into the copy constructor? The copy constructor must receive a reference.

And could you please explain this *ptr = *obj.ptr; . From here i know only lhs, which dereferences ptr (aka sets value of an object). But I didn't get what's in rhs?

I don't know what you mean by rhs and lhs, is not in the code you posted. But indeed, *ptr = *obj.ptr will set the value from the integer pointed by obj.ptr into the variable pointed by this->ptr.

I recommend getting a good C++ book and follow a tutorial that will teach you the basics.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141