Class(c++) virtual functions usage

Started by
6 comments, last by Namethatnobodyelsetook 19 years, 6 months ago
Here's my little problem :

class A
{
   int a;
public:
   virtual bool equal(A& rA){return a == rA.a;}
};

class B : public class A
{
   int b;
public:
   virtual bool equal(A& rA){return false;}
   virtual bool equal(B& rB){return b == rB.b;}
};

bool equal(A& pA1, A& pA2)
{
    return pA1.equal(pA2);
}

int main(void)
{
   A lA;
   B lB1, lB2;

   equal(lB1, lB2);

   return 1;
}



My question is how to achive that in "equal(lB1, lB2)" call B.equal() function ? I'd be glad for any help.
Advertisement
im thinking, if you know that the args passed to the non member
equal function ARE GUARANTEED to be of type B, you could (static_cast<B*>( &pA1 )) safely. if not i think you can use RTTI:

if( dynamic_cast<B*>( &pA1 ) != NULL )    // its of type B*else {    // failed}
Anyone, feel free to correct me!
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
You could use templates:

template<typename T1, typename T2>bool equal(const T1& lhs, const T2& rhs){    return lhs.equal(rhs);}


This will rely upon the object type being known at compile time. If you have a pointer to a base class but it really points to a derived class then it won't work as you want.

Probably what you need is DoubleDispatch and the VisitorPattern. I'll leave you to work out the details.
Quote:Original post by silvermace
im thinking, if you know that the args passed to the non member
equal function ARE GUARANTEED to be of type B, you could (static_cast<B*>( &pA1 )) safely. if not i think you can use RTTI:

if( dynamic_cast<B*>( &pA1 ) != NULL )    // its of type B*else {    // failed}
Anyone, feel free to correct me!


Casting usually isn't really a good design solution unless you're dealing with passing data into or out of your application such as network data, binary data from a file, etc.

In this case it's unnecssary and using it is not scalable. As more types are added you'll end up with a lot of conditional code.
Thanks petewood.
What about adding the function:

bool equal(B& pB1, B& pB2){   return pB1.equal(pB2);}
So, i couldn't solve my problem, i tried template. But not worked, and i don't know how to solve with double dispatch.

class A{public:virtual bool equal(A *) = 0;};class B: public A{public:virtual bool equal(A *){return false;}virtual bool equal(B *){return true;}};int main(void){B lB1, lB2;A* lBuff[2];lBuff[0] = &lB1;lBuff[1] = &lB2;bool b = lBuff[0]->equal(lBuff[1]);return 1;}


So again, b is false;
Could you use RTTI to check if class1 == class2. If not, return false. Else, check all members. Since the two classes are the same, the virtual function called will be the one of the correct type to check all elements of both classes.

If you don't want to rely on RTTI you could put a number in your baseclass and initialize it to a value per type, creating your own RTTI system, then following the steps above.

This also means you'll only need one equal function per class. Right now you've got two in B. Will C have 3? As you add more classes this will be a nightmare. My solution is pretty simple and clean.

This topic is closed to new replies.

Advertisement