• Advertisement

Archived

This topic is now archived and is closed to further replies.

virtual static function?

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

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.

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 -

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement