开发者

Assignment vs Initialization in C++

开发者 https://www.devze.com 2022-12-20 22:55 出处:网络
I thought that constructors control initialization and operator= functions control assignment in C++. So why does this code work?

I thought that constructors control initialization and operator= functions control assignment in C++. So why does this code work?

#include <iostream>
#include <cmath>
using namespace std;

class Deg {
    public:
        Deg() {}
        Deg(int a) : d(a) {}        
        开发者_Python百科void operator()(double a)
        {
            cout << pow(a,d) << endl;
        }

    private:
        int d;
};

int
main(int argc, char **argv) 
{
    Deg d = 2;
    d(5);
    d = 3; /* this shouldn't work, Deg doesn't have an operator= that takes an int */
    d(5);
    return 0;
}

On the third line of the main function, I am assigning an int to an object of class Deg. Since I don't have an operator=(int) function, I thought that this would certainly fail...but instead it calls the Deg(int a) constructor. So do constructors control assignment as well?


This is what's called implicit type conversion. The compiler will look to see if there's a constructor to directly change from the type you're assigning to the type you're trying to assign, and call it. You can stop it from happening by adding the explicit keyword in front of the constructor you wouldn't like to be implicitly called, like this:

explicit Deg(int a) : d(a) {}


Just to clarify JonM's answer:

For the line d = 3, an assignment operator is involved. 3 is being implicitly converted to a Deg, as JonM said, and then that Deg is assigned to d using the compiler-generated assignment operator (which by default does a member-wise assignment). If you want to prevent assignment, you must declare a private assignment operator (and do not implement it):

//...
private:
    Deg& operator=(const Deg&);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消