Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to use functions of base class (http://www.velocityreviews.com/forums/t745366-how-to-use-functions-of-base-class.html)

 m0shbear 03-18-2011 10:14 PM

how to use functions of base class

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::operator==(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.

 Paul 03-18-2011 10:49 PM

Re: how to use functions of base class

"m0shbear" <andrey.vul@gmail.com> wrote in message
> 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::operator==(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::operator==(b))&& s==b.s && t==b.t;
}

 All times are GMT. The time now is 07:31 AM.