Jump to content
  • Advertisement
Sign in to follow this  
EverIce

Class(c++) virtual functions usage

This topic is 5390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!