Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to use functions of base class

Reply
Thread Tools

how to use functions of base class

 
 
m0shbear
Guest
Posts: n/a
 
      03-18-2011
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.
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      03-18-2011

"m0shbear" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
How to access data or functions of the derived class if it was declared as the base class? Titan C++ 1 06-30-2003 03:04 PM



Advertisments