jeudi 28 mai 2015

C++11 Uniform initialization for constructor and operator=

I have T class and Table that uses T for construction parameter.

struct T {
    string name;
    long value;
};

class Table {

public:
    T a, b, c;

    Table(T a, T b, T c) {
        cout << "From constructor: " << a.name << endl;
        this->a = a; this->b = b; this->c = c;
    }
    Table& operator=(const Table& a) {
        cout << "In op=: " << a.a.name << endl;
        return *this;
    }
};

I also have an example function trying to invoke constructor and = operator.

int main(int argc, char *argv[]) {   
    Table phone_numbers {
        { "Donald Duck", 2015551234 },
        { "Mike Doonesbury", 9794566089 }, 
        { "Kell Dewclaw", 1123581321 }
        };
    Table phone_numbers2 = {
        { "Donald Dog", 2015551234 },
        { "Mike Doonesbury", 9794566089 }, 
        { "Kell Dewclaw", 1123581321 }
        };

}

However both of them invokes only the constructor.

From constructor: Donald Duck
From constructor: Donald Dog

What might be wrong? How to invoke = operator in this case?

Aucun commentaire:

Enregistrer un commentaire