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::eek: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.
 
Ad

Advertisements

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::eek: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::eek:perator==(b))&& s==b.s && t==b.t;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top