![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
any alternatives to (lack of) virtual static functions?
Hello,
I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The specific derived class that is interfaced to depends on the template parameter provided. However, in a few cases, I need to call a static (class) function associated with the derived class when an objects of that class may not exist. Two possibilities I have come up with are to 1) pass the static function of the dervied class to the target class as a callback function or 2) add the interface to the abstract base class, remove the static designation of the function in the derived class, and use a half- initialized object (ughh!) to access the function through a pointer to the base class. Is there a better, more elegant way to do this? Thanks, Carl Code example below. template <typename T> class Base { public: // illegal to declare a virtual static function here virtual void func1(T& a) = 0; virtual void func2(T& b) = 0; // ... }; class Derived1 : public Base<int> { public: static void Derived1Func(int& x); // type T is int in this case void func1(int& a); void func2(int& b); // ... }; class XyzType { XyzType() {} ~XyzType() {} }; class Derived2 : public Base<XyzType> { public: static void Derived2Func(XyzType& x); // type T is XyzType here void func1(int& a); void func2(int& b); // ... }; template <typename T> class Target { void aFunc(Base<T> *ptr); void bFunc(Base<T> *ptr); // ... }; template <typename T> void Target<T>::aFunc(Base<T> *ptr) { T aT; ptr->func1(T& aT); // great, works fine }; template <typename T> void Target<T>::bFunc(Base<T> *ptr) { // now here I need to get a handle on Derived1Func or Derived2Func // ... or DerivedNFunc depending on the T parameter, or some other // parameter if necessary. But I cannot call any of them directly because // this class primarily uses an interface to reference the specific derived // class (and does not know or care which derived class it is) } Thanks, Carl |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |