dynamic binding on parameters, how ??

V

Victor Bogado

I want to create a class hierarchy for geometric elements, each
element must know how to report if he intersect with other element.
The interface is the following :

class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};

class SquareElement : public Element
{
public:
bool intersect(SquareElement &e)
{
...
}
};

class CircleElement
{
public:
bool intersect(SquareElement &e)
{
...
}

bool intersect(CircleElement &e)
{
...
}
};


The problem is that when I execute squareRef.intersect(circleRef) the C
++ will call the Element::intersect(Element &) because it don't do
dynamic binding on the parameters. So I thought on the scheme :

class Element

class Element
{
public:
...
// Here we could have a naive implementation
// (using bounding boxes for instance)
bool intersect(Element &e)
{
...
}
...
};

class SquareElement : public Element
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}

protected:
bool doIntersect(SquareElement &e)
{
...
}
};

class CircleElement
{
public:
bool intersect(Element &e)
{
return e.intersect(*this);
}

protected:

bool intersect(SquareElement &e)
{
...
}

bool intersect(CircleElement &e)
{
...
}
};

this solves some problems, but not all of them, unfortunately all
classes must know about all other classes in this case, otherwise the
compiler will use the generic form. Also there is a unbalance where
squareRef.intersect(circleRef) is potentially different from
circleRef.intersect(squareRef).
 
V

Victor Bazarov

Victor said:
I want to create a class hierarchy for geometric elements, each
element must know how to report if he intersect with other element.
[..]

Did you read [carefully] all suggestions to your previous question?
I am lazy to track down the exact author, but there was a suggestion
to implement a "register-an-interaction" mechanism in some kind of
registry of interactions. Every class when instantiated would
register what other classes it can intersect with, and then when the
element needs to intersect with other element, the registry should
find [the function pointer for the pair] and call it.

Implementing that registry would probably require some kind of type
identification system. 'typeid' looks like as good a solution as
any other.

V
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top