N
Nephi Immortal
Please explain why operator= cannot be inherited into class B from
class A. Is it because the left reference before operator= is type A,
but not type B? Then, do I have to define a copy of operator= and
replace left reference to type B in class B body?
It is annoying to redefine operator= every time I create several new
classes that are derived from class A.
class A {
public:
A() : m_data( 0 ) {}
~A() {}
A &operator=( int byte ) {
m_data = byte;
return *this;
}
int m_data;
};
class B : public A {
public:
B() : A() {}
~B() {}
};
int main () {
A a;
a = 5; // ok
B b;
b = 7; // error
return 0;
}
class A. Is it because the left reference before operator= is type A,
but not type B? Then, do I have to define a copy of operator= and
replace left reference to type B in class B body?
It is annoying to redefine operator= every time I create several new
classes that are derived from class A.
class A {
public:
A() : m_data( 0 ) {}
~A() {}
A &operator=( int byte ) {
m_data = byte;
return *this;
}
int m_data;
};
class B : public A {
public:
B() : A() {}
~B() {}
};
int main () {
A a;
a = 5; // ok
B b;
b = 7; // error
return 0;
}