Jump to content
  • Advertisement
Sign in to follow this  
deadimp

Class Comparisons - Third Party to Second Party to First

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

I have yet another question... Is there some way to compare classes of certain objects? Can it be done using 'template'? My Psuedo Code: (an example)
template <class objType>
bool isInt(objType &var) {
 if (objType==int) return true; //Remember, Psuedo
 else return false;
}
I found something about the function 'same_type_p', but it doesn't work on Dev-C++ (4.9.9.1). I don't know where the header is for it. (RESOLVED) One more question... (RESOLVED) New question! I now have the typeid operator, and a method to pass classes to functions, but now I need to figure out if a certain class (of a pointer) is either the same or a descendant of another class (using polymorphism and inheritance). [Edited by - deadimp on December 31, 2004 5:34:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
This can easily be achieved using template specialization.

template <typename T>
bool is_int(T & x)
{
return false;
}

template <>
bool is_int(int & x)
{
return true;
}

Share this post


Link to post
Share on other sites
You gotta overload the == operator

bool operator==(int);

edit - oh nm, I misunderstood your question.

Share this post


Link to post
Share on other sites
Thanks! That helped alot.
One more question: How do you pass classes to functions, without using templates?
Psuedo Code:
void func(class X) {
//Use class X and typeid
}

Share this post


Link to post
Share on other sites
You can't pass a class to a function without using templates, only instances (or pointers to instances, such as pointers to abstract base classes). Template functions are how C++ lets you pass classes.

Share this post


Link to post
Share on other sites
It wasn't too imperative. Though I found you could do this:
template <class T>
bool func() {
//Code
}
//Calling func()
func<int>();

Looks kinda messy, but it works. Thanks for your help.

Share this post


Link to post
Share on other sites
God that made my head hurt. I think I'm gonna take the easy way and make an array of 'parent' pointers inside of the class (I'm lazy). However, I shall come back to that article once I feel I think I can understand it. Thanks.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!