TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VC.NET

Notices

any alternatives to (lack of) virtual static functions?

VC.NET


Reply
 
Thread Tools Display Modes
Old 14-12-2007, 08:57 PM   #1
carlm
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
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 07:46 AM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional