A
anongroupaccount
What measures should be taken to avoid this sort of thing?
class Base
{
};
class Derived1 : public Base
{
private:
int i, j, k;
};
class Derived2 : public Base
{
private:
double l, m, n;
};
void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}
int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;
BaseAssign(d1, d2);
}
// End code snippet
The only way I can see around preventing this sort of thing is
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.
Do people generally just not worry about this sort of thing? I'm
absolutely paranoid about stuff like this.
class Base
{
};
class Derived1 : public Base
{
private:
int i, j, k;
};
class Derived2 : public Base
{
private:
double l, m, n;
};
void BaseAssign(Base& lhs, Base& rhs)
{
lhs = rhs;
}
int main(int argc, char *argv[])
{
Derived1 d1;
Derived2 d2;
BaseAssign(d1, d2);
}
// End code snippet
The only way I can see around preventing this sort of thing is
declaring the assignment operators and copy constructors of non-leaf
classes protected, and providing clone/create methods instead of
letting the operators/constructors be used.
Do people generally just not worry about this sort of thing? I'm
absolutely paranoid about stuff like this.