Skip to main content
gamedev.net
🔒 Locked

virtual static function?

Started by shaobohou Apr 14, 2004 at 4:44 AM 8 replies 700+ views
Original Post
shaobohou
shaobohou
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.
Just because it is not nice, doesn''t mean it is not miraculous.
foofightr
foofightr
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]
Qw3r7yU10p!
Qw3r7yU10p!
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;
}
iaretony
iaretony

Look up, "pointers to member functions" in stroustrup''s book... A function need not be static to call it with a function pointer, but you will need both a pointer to the object AND a function pointer to call a member function VIA a member pointer...

Tony
shaobohou
shaobohou
I have considered that options in the last two post, but i''d rather the the other function using the function pointer knows nothing about objects.
Just because it is not nice, doesn''t mean it is not miraculous.
SiCrane
SiCrane
What about using function objects (a.k.a. functors), like boost::function?
hplus0603
hplus0603
You can pass an interface.


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.
enum Bool { True, False, FileNotFound };
benstr
benstr
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 -
-Benny-
shaobohou
shaobohou
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.
Just because it is not nice, doesn''t mean it is not miraculous.
benstr
benstr
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]
-Benny-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.