# how to use functions of base class

M

#### m0shbear

Given
struct A {
int x;
int y;
bool operator==(const A& a) const {
return (x==a.x)&&(y==a.y);
}
};
struct B : public A {
int s;
int t;
B(int u = 0, int v = 0, int w = 0, int z = 0) : A(u,v), s(w), t(z)
{ }
bool operator==(const B& b) const {
return (dynamic_cast<A&>(*this)).operator==(dynamic_cast<const
A&>(b)) &&
(s==b.s)&&(t==b.t);
}
};

Will B:perator==(const B&) behave as intended, according to the
spec?
I'm using composition inheritance to remove duplicate code and want to
know if it's for naught.

P

#### Paul

m0shbear said:
Given
struct A {
int x;
int y;
bool operator==(const A& a) const {
return (x==a.x)&&(y==a.y);
}
};
struct B : public A {
int s;
int t;
B(int u = 0, int v = 0, int w = 0, int z = 0) : A(u,v), s(w), t(z)
{ }
bool operator==(const B& b) const {
return (dynamic_cast<A&>(*this)).operator==(dynamic_cast<const
A&>(b)) &&
(s==b.s)&&(t==b.t);
}
};

Will B:perator==(const B&) behave as intended, according to the
spec?
I'm using composition inheritance to remove duplicate code and want to
know if it's for naught.

Does this not work?
bool operator==(const B& b) const {
return (A:perator==(b))&& s==b.s && t==b.t;
}