how do I get an ADT virtual function to take the derived class as a parameter?

Started by
5 comments, last by Kyo 21 years ago
This is my abstract data type:
  
class data
{
public:
	data() {}
	virtual ~data() {}

	virtual int compare(const data &otherData) = 0;
	virtual void show() = 0;
	virtual int getValue() = 0;
};
  
This is the base data type that''s being held in a list, and any derived classes must implement compare, show and getValue. The problem is how do I implement the compare function in the derived class? The derived class has to take an object of itself as a parameter, not a data class type.
Advertisement
In the implementation of ''compare'', you chould use RTTI to make sure ''otherData'' is of the same type, and if it''s not than act like if the objects are completely different.

int deriveddata::compare( const data& otherData ){    const derived* pDerived = dynamic_cast< const derived* >( &otherData );    if( pDerived )    {        // compare ''this'' and ''pDerived''    }    else    {        // objects are not equals        return 1; // this could also be -1, I guess    }} 


----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
Blastersoft - http://www.blastersoft.com
Templates. Using Templates as a dirived type.

class template TType{
TType();
virtural ~TType();
T* compare(T* cls) = 0;
}

and derive all classes from this and you will be forced to build the compare function in them. and does not matter about the type they are drived from.
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.
Hi!

I think I''ve got the solution to your problem.

Lets say that we have an abstract class data and a class obj that derives from data and wants to have an object of class obj as a parameter in a function we can do like this :

class data{
public:
data() {}
virtual ~data() {}
virtual int compare(const data *otherData) = 0;
virtual void show() = 0;
virtual int getValue() = 0;
};

class obj
{
public:
...
int compare(const data *otherData);
...
};

now if you want to send an instance of obj to obj''s compare tou just fo like this (myobj and yourobj are instances of obj)

myobj.compare((data*)&yourobj); // converting yourobj to its
// base class data.

and in the implementation of compare you just do this:
int obj::compare(const data *otherdata)
{
obj *newobject = (obj*)otherdata; // converting back to obj
// type
....
}

So basicly you just convert to the baseclass during the functioncall and convert back to the class you want in that function.

Hope this helps.


quote:Original post by Anonymous Poster
Hi!

I think I''ve got the solution to your problem.

Lets say that we have an abstract class data and a class obj that derives from data and wants to have an object of class obj as a parameter in a function we can do like this :

class data{
public:
data() {}
virtual ~data() {}
virtual int compare(const data *otherData) = 0;
virtual void show() = 0;
virtual int getValue() = 0;
};

class obj
{
public:
...
int compare(const data *otherData);
...
};

now if you want to send an instance of obj to obj''s compare tou just fo like this (myobj and yourobj are instances of obj)

myobj.compare((data*)&yourobj); // converting yourobj to its
// base class data.

and in the implementation of compare you just do this:
int obj::compare(const data *otherdata)
{
obj *newobject = (obj*)otherdata; // converting back to obj
// type
....
}

So basicly you just convert to the baseclass during the functioncall and convert back to the class you want in that function.

Hope this helps.




This is not type-safe at all. And if you use templates you can''t use the benefits of polymophy. I''d say Blaster''s suggestion is the way to go.
If you look at the recent articles you will see more information on a template manager. This will also tell you how to directly handle what you are looking for.

As for the last guys post I do not see how that will help. Firstoff nothing is a pointer and means you can only copy it into the class..... Also that the class is not drived from any base class. So those are the flaws I can see off hand without checking deeper.
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.
Thanks AP''s method worked fine, i didn''t get the template thing though would probably be a neater solution.

This topic is closed to new replies.

Advertisement