Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kyo

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

This topic is 5544 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

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.

Share this post


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

Share this post


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

Share this post


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


Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Thanks AP''s method worked fine, i didn''t get the template thing though would probably be a neater solution.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!