Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

if object has function call it

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

is it possible to have an if statement that says if object has function x call function x if (Object->Function) Object->Function(); shouldnt this be possible in compiler-time? just have the compiler check if the function name is defined...? im working in MS C++ visual studio 2005 PS. This is a possible solution to a problem i posted in another forum, but i dont think/hope it is counted as a cross post

Share this post


Link to post
Share on other sites
Advertisement
It's almost possible. You can create a standalone function which calls the function if it exists, using boost::enable_if, and then call that standalone function.

Share this post


Link to post
Share on other sites
It is not pòssible to do that. To be able to write:

if (Object->Function)

Object has to have Function in order for the program to compile.

Explain to us what are you trying to accomplish. I'm sure there is a proper way to handle it.

Share this post


Link to post
Share on other sites
if (Object->Function)

was an example illustrating just about what im trying to do...

what i want to do is to create a class wrapper that takes a boost::any
and this wrapper has a function that calls a function that most of (but not all) the objects sent to boost::any has...

Share this post


Link to post
Share on other sites
Well, wouldn't you need to "cast" the boost::any to a specific type before calling the function, anyway? If that's the case, then you already know, at compile time, whether the type has the function or not. And if you're using templates, the compiler will break loudly at compile time if the function isn't found in the given type specified in the template.

Share this post


Link to post
Share on other sites
He wants to be able to do it at run time.

I personally think it's a bad idea, at least in c++.

In my mind, if I wanted to iterate a bunch of objects and call some function from them, they should have that function or inherit from a class that has it.

Share this post


Link to post
Share on other sites
Try this: Say you define a base class with the function you want to call as a pure virtual. Classes derive from this base class and define the function. I don't know about boost::any, but suppose you're passing a void pointer:

void CallingFunc(void *incoming)

use dynamic_cast to convert the void pointer to your base class:

BaseClass *ptr = dynamic_cast<BaseClass *>(incoming)

now you just need to check if ptr != NULL. If it's equal to NULL, then the incoming ptr wasn't derived from the BaseClass, and you can't call the function.

if(ptr) ptr->Func();

Share this post


Link to post
Share on other sites
Quote:
Original post by Numsgil
Try this: Say you define a base class with the function you want to call as a pure virtual. Classes derive from this base class and define the function. I don't know about boost::any, but suppose you're passing a void pointer:

void CallingFunc(void *incoming)

use dynamic_cast to convert the void pointer to your base class:

BaseClass *ptr = dynamic_cast<BaseClass *>(incoming)

now you just need to check if ptr != NULL. If it's equal to NULL, then the incoming ptr wasn't derived from the BaseClass, and you can't call the function.

if(ptr) ptr->Func();


I could be wrong, but I'm pretty sure that you can't dynamic_cast from a void* like that.

Quote:
C++ Standard 5.2.7.1
...
dynamic_cast<T>(v)
...
If T is a pointer type, v shall be an rvalue of a pointer to complete class type, and the result is an rvalue of type T.


Share this post


Link to post
Share on other sites
You can't dynamic cast. You could do this:

void CallIt(void* someObject, param...)
{
// don't call the method
}

void CallIt(Base* someObject, param...)
{
someObject->MyMethod(param...);
}

...
int a;
Derived b;
CallIt(&a,...);
CallIt(&b,...);

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!