-1

I'm trying to understand the code below. More specifically, why is b.x in the main function 5?
As far as I understand, I have a constructor Someclass(int xx):x(xx){} in the class which sets my attribute x to xx. Therefore, a.x in the main function is 4.
But what do the lines
Someclass(const Someclass& a){x=a.x;x++;} and
void operator=(const Someclass& a){x=a.x;x--;} do?
Is the first one also a constructor? And I think the second one overrides the '=' operation. But with what? And what is this a in the class?

Since I am still quite new to programming, I would really appreciate your help!

 class Someclass{
    public:
    int x;
    public:
    Someclass(int xx):x(xx){}
    Someclass(const Someclass& a){x=a.x;x++;}
    void operator=(const Someclass& a){x=a.x;x--;}

};

int main()
{
 
    Someclass a(4);
    Someclass b=a;
    cout<<"a:"<<a.x<<endl; //prints 4
    cout<<"b:"<<b.x<<endl; //prints 5
   
    return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
sojaBean
  • 9
  • 3
  • 2
    Look for "copy constructor" in your tutorial. – 273K Jul 17 '22 at 18:44
  • "But what do the lines `Someclass(const Someclass& a){x=a.x;x++;}` and `void operator=(const Someclass& a){x=a.x;x--;}` do" -- shouldn't whatever textbook this code comes from have an explanation and a description of all of this? This is basic, core, C++ fundamental concepts so I'm assuming you're reading this in your C++ textbook, is there something ***specific*** in the textbook's explanation, here, that's unclear to you? – Sam Varshavchik Jul 17 '22 at 18:44
  • They are the copy constructor and the assigment operator for the class. And they do *really* odd things. Usually we assume that a copy has the same value as the original, but who knows... – BoP Jul 17 '22 at 18:44
  • 1
    Please learn the language in a structured way, e.g. from one of the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't look up random code and try to figure out what it does without having learned about the syntax it is using. In order to understand the shown code you should have learned about the topics of _references_, _copy constructors_ and _copy assignment operators_. All of these are fundamental concepts that should be explained in any introduction to the language. Trying to learn by guessing meaning of syntax is not going to work. – user17732522 Jul 17 '22 at 18:54

1 Answers1

1

When you instantiate a with the following line:

Someclass a(4);

You are calling the "normal" constructor, namely:

class Someclass {
  public:
    int x;
  public:
    Someclass(int xx):x(xx){} // <= This one
    Someclass(const Someclass& a){x=a.x;x++;}
    void operator=(const Someclass& a){x=a.x;x--;}
};

When you instantiate b with the following line:

Someclass b=a;

You are creating a new object (b) from an already existing one (a). This is what the copy constructor is for.

class Someclass {
  public:
    int x;
  public:
    Someclass(int xx):x(xx){}
    Someclass(const Someclass& a){x=a.x;x++;} // <= This one
    void operator=(const Someclass& a){x=a.x;x--;}
};

To put it simply, the equals operator is for the already instantiated object. This operator would be called if you wrote the following:

Someclass a(4);
Someclass b = a;
b = a;
std::cout << b.x << std::endl; // Prints 3
thomaoc
  • 94
  • 10