🔒 Locked
virtual static function?
Advertisement
Original Post
this is gonna sound like a stupid question, is there anyway of making a static class function "virtual" in a sense.
Let me explain, I need to define a static function for a class so that it can be passed as function pointer to someone else. (think derivative function in numerical integrator)
but subclass of the this class will need to define their own static function with the same interface. How can I let the subclass know that they will have to implement a static class of a certain interface.
Static and virtual functions are mutually exclusive from a logical standpoint. Static member functions don't go on the vtable, and they must be defined (i.e. no pure virtuals) so you cannot resolve different versions of them at runtime. Best you can do is have identically named static functions inside each class.
[edited by - foofightr on April 14, 2004 5:59:11 AM]
[edited by - foofightr on April 14, 2004 5:59:11 AM]
pass the object to the static function and then have it call the virtual function on the object.
#include <iostream>
class Base {
public:
static void somethingStatic(Base* object, int argument) {
if(object)
object->somethingVirtual(argument);
}
virtual void somethingVirtual(int argument) {
std::cout << "Base somethingVirtual with argument " << argument << "\n";
}
};
class Derived : public Base {
public:
void somethingVirtual(int argument) {
std::cout << "Derived somethingVirtual with argument " << argument << "\n";
}
};
typedef void (*FunctionPointer)(Base*, int);//pointer to function taking Base* and integer and returning void
int main() {
FunctionPointer fp = &Base::somethingStatic;
Base b;
Derived d;
fp(&b, 6);
fp(&d, 2);
return 0;
}
What about using function objects (a.k.a. functors), like boost::function?
You can pass an interface.
Have your function needing to derive stuff take one of these as argument:
Then, implement IMyDerivative in each concrete class that needs to provide derivatives, and pass the class instance to the function needing to integrate.
That way, you can answer the "derive()" question using actual class memebers, rather than having to rely on some data being passed externally to your class.
class IMyDerivative {
public:
virtual void derive( float inX, float inY, float * outDx, float * outDy ) = 0;
};
Have your function needing to derive stuff take one of these as argument:
void MyIntegrate( IMyDerivative * dxdy, ... )
{
float slopeX, slopeY;
dxdy->derive( 2, 3, &slopeX, &slopeY );
}
Then, implement IMyDerivative in each concrete class that needs to provide derivatives, and pass the class instance to the function needing to integrate.
That way, you can answer the "derive()" question using actual class memebers, rather than having to rely on some data being passed externally to your class.
Don''t know if this will help or not. But you could so something with templates, like this. Hope it helps.
class X
{
public:
static void DoSomething();
};
class Y
{
public:
static void DoSomething();
};
class Z
{
public:
template <class W> static void DoSomething()
{
W::DoSomething();
}
};
Z::DoSomething<X>();
Z::DoSomething<Y>();
- Benny -
class X
{
public:
static void DoSomething();
};
class Y
{
public:
static void DoSomething();
};
class Z
{
public:
template <class W> static void DoSomething()
{
W::DoSomething();
}
};
Z::DoSomething<X>();
Z::DoSomething<Y>();
- Benny -
the template idea sounds interesting but doesn''t template means that all classes are treated the same so each class can''t do something different.
i am basically looking for way to tell the subclasses that they need to define a static function of certain interface.
I am going have to look into the functor idea, don''t quite understand properly after a brief search on google.
i am basically looking for way to tell the subclasses that they need to define a static function of certain interface.
I am going have to look into the functor idea, don''t quite understand properly after a brief search on google.
No, each class is not treated the same (assuming I did not misunderstand what you mean by that)
If class X and Y looks like below nad Z as above...
class X
{
public:
static void DoSomething()
{
std::cout << "Hello X" std::endl;
}
};
class Y
{
public:
static void DoSomething()
{
std::cout << "Hello Y" std::endl;
}
};
... then this:
Z::DoSomething();
Z::DoSomething();
Will output:
Hello X
Helly Y
About functors, they are basically classes made to work like a function by overloading the function call operator.
class Functor
{
public:
Functor() {}
virtual ~Functor() {}
void operator () ( const std::string& str )
{
std::cout << str << std::endl;
}
};
Functor x;
x( "Hello Functor" );
would output: Hello Functor
You can have the functor as complex ot simple as you want, pass arguments to the constructor and store them in some member variables or not or do whatever, it's just another class with the function call operator overloaded. They can be really usefull but I must admit I have not taken the time to understand what your problem is so I can't say if they are for you or not. Anyway, you could have a functor base class and let your static function take that as an argument and call it from inside your static function. Then inherit the functor class and pass the functor you need. It all depends on your needs I guess.
Hrm... I wish there was a preview, I have no idea what I just said
Edit: As I said, there should be a preview :D
- Benny -
[edited by - benstr on April 16, 2004 2:14:35 AM]
If class X and Y looks like below nad Z as above...
class X
{
public:
static void DoSomething()
{
std::cout << "Hello X" std::endl;
}
};
class Y
{
public:
static void DoSomething()
{
std::cout << "Hello Y" std::endl;
}
};
... then this:
Z::DoSomething
Z::DoSomething
Will output:
Hello X
Helly Y
About functors, they are basically classes made to work like a function by overloading the function call operator.
class Functor
{
public:
Functor() {}
virtual ~Functor() {}
void operator () ( const std::string& str )
{
std::cout << str << std::endl;
}
};
Functor x;
x( "Hello Functor" );
would output: Hello Functor
You can have the functor as complex ot simple as you want, pass arguments to the constructor and store them in some member variables or not or do whatever, it's just another class with the function call operator overloaded. They can be really usefull but I must admit I have not taken the time to understand what your problem is so I can't say if they are for you or not. Anyway, you could have a functor base class and let your static function take that as an argument and call it from inside your static function. Then inherit the functor class and pass the functor you need. It all depends on your needs I guess.
Hrm... I wish there was a preview, I have no idea what I just said
Edit: As I said, there should be a preview :D
- Benny -
[edited by - benstr on April 16, 2004 2:14:35 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement