FilOle
FilOle

Reputation: 53

Order of constructor and copy constructor

Could someone tell me why does in such case copy constructor and destructor is used by program?

#include <iostream>

#include <iomanip>

using namespace std;

class Object
{
public:
        Object(){}
        Object(const Object &kk) {cout<<"kk"<<endl;}
        bool operator==(Object c)
        {
                cout<<"o=="<<endl; return false;
        }
        ~Object()
        {
                cout<<"des"<<endl;
        }

};

int main()
{
        Object o1,o2;
        bool result;
        result = (o1==o2);
}

The result is:

kk
o==
des

Thanks in advance for answer.

Upvotes: 0

Views: 100

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

Because your operator== takes its argument by value.

Taking by value implies a copy.

Make it take a const-reference instead, like you did for your copy constructor.

Upvotes: 2

Related Questions