if object has function call it

Started by
11 comments, last by the_edd 16 years, 3 months ago
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
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.
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.
[size="2"]I like the Walrus best.
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...
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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.
[size="2"]I like the Walrus best.
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();
[size=2]Darwinbots - [size=2]Artificial life simulation
ingenius!

i can work with that....

thx!
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.


if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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,...);

This topic is closed to new replies.

Advertisement