(缺少)虚拟静态函数的任何替代方案? [英] any alternatives to (lack of) virtual static functions?

查看:95
本文介绍了(缺少)虚拟静态函数的任何替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我搜索了我的问题的答案并找到了类似的帖子,

但没有一个能解决我想解决的问题。

基本上,似乎我需要像虚拟静态

函数(我知道这是非法的),但是,有没有办法提供

相似的东西?作为我的查询目标的类是一个

模板类,它通过指向基类的指针连接到几个派生类之一



接口的特定派生类取决于提供的模板参数。但是,在一些情况下,当需要该类的对象可能不存在时,我需要调用与派生类相关的静态(类)函数

。我想出的两个

的可能性是1)将派对类的静态函数

作为回调函数传递给目标类或2)

将接口添加到抽象基类,删除派生类中函数的静态

指定,并使用一半 -

初始化对象(ughh !)通过指向

基类的指针来访问该函数。有更好,更优雅的方式吗?


谢谢,

Carl

下面的代码示例。

模板< typename T>

类基地

{

public:

//非法宣布虚拟静态函数

virtual void func1(T& a)= 0;

virtual void func2(T& b)= 0;

// ...


};

class Derived1:public Base< int>

{

public:

static void Derived1Func(int& x); //在这种情况下,类型T是int

void func1(int& a);

void func2(int& b);

/ / ...

};

class XyzType

{

XyzType(){}

~XyzType(){}

};

class Derived2:public Base< XyzType>

{

public:

static void Derived2Func(XyzType& x); //类型T是这里的XyzType

void func1(int& a);

void func2(int& b);

//。 ..

};

模板< typename T>

class目标

{

void aFunc(Base< T * ptr);

void bFunc(Base< T * ptr);

// ...

};

模板< typename T>

void Target< T> :: aFunc(Base< T * ptr)

{

T aT;


ptr-> func1(T& aT); //很好,工作正常


};

模板< typename T>

void Target< T> :: bFunc (基本< T * ptr)

{


//现在我需要处理Derived1Func或Derived2Func

// ...或DerivedNFunc取决于T参数,或者必要时其他一些

因为

//这个类主要使用一个接口来引用特定的

派生的

//类(并且不知道或关心它是哪个派生类)

}


谢谢,

Carl

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

推荐答案

carlm写道:
carlm wrote:

>
>



[...]

[...]


>

template< typename T>

void Target< T> :: bFunc(Base< T * ptr)

{

//现在我需要根据T参数获取Derived1Func或Derived2Func

// ...或DerivedNFunc的句柄,或者根据需要获取其他一些

因为

//这个类主要使用一个接口来引用特定的

派生的

//类(并且不知道或关心它是哪个派生类)

}
>
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)
}



专业化:


模板<>

void Target< int> :: bFunc(Base< int * ptr)

{

// ptr必须是Derived1,在你的具体情况下,施放它

Derived1 * p1 =(Derived1 *)ptr; //或使用dynamic_cast ...

p1-> Derived1Func(...); //调用静态函数

}


模板<>

void目标< XyzType> :: bFunc(Base< ; XyzType * ptr)

{

// ptr必须是Derived2,施放它

Derived2 * p2 =(Derived2 *)ptr;

p2-> Derived2Func(...);

}

Specialize it:

template <>
void Target<int>::bFunc(Base<int*ptr)
{
// ptr must be Derived1, in your specific case, cast it
Derived1 *p1 = (Derived1 *) ptr; // or use dynamic_cast...
p1->Derived1Func(...); // call the static function
}

template <>
void Target<XyzType>::bFunc(Base<XyzType*ptr)
{
// ptr must be Derived2, cast it
Derived2 *p2 = (Derived2*) ptr;
p2->Derived2Func(...);
}


template< typename T>
template <typename T>

void Target< T> :: bFunc(Base< T * ptr)

{


//现在我需要获取Derived1Func或Derived2Func

// ...或DerivedNFunc的处理,具体取决于T参数或其他一些

//参数如有必要。但是我不能直接打电话给他们任何一个

因为

//这个类主要使用一个接口来引用特定的

派生的

// class(并且不知道或关心它是哪个派生类)
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)



你的问题是T并不是唯一确定派生最多的class,

即允许有:


class Derived1:Base< int {};

class Derived2:Base< ; INT {}; //相同T


如果为真实类型添加模板参数,可以调用其静态

成员。


模板< typename T>

模板< typename D>

void目标< T> :: bFunc(D * dptr)

{

Base< T * ptr = static_cast< Base< T> *>(dptr);


...


D :: StaticFunction(); //调用的函数因D而异,不像

其他语言的泛型

}


你不能虚拟没有对象的调度,因为虚拟调度

取决于对象的运行时类型,没有对象......没有运行时类型。


另一种选择是函数指针的结构,每种类型填写为
。然后可以传递一个指向这个结构的指针,或者接口中的一个
虚函数可以返回它。这基本上是如何使用b
虚拟调用开始工作,除了C ++虚拟语法不是
让你引用v-table并将其传递给独立一个对象。

Your problem is that T doesn''t uniquely determine the most derived class,
i.e. it is permissible to have:

class Derived1 : Base<int{};
class Derived2 : Base<int{}; // same T

If you add a template parameter for the true type, you can call its static
members.

template <typename T>
template <typename D>
void Target<T>::bFunc(D* dptr)
{
Base<T*ptr = static_cast<Base<T>*>(dptr);

...

D::StaticFunction(); // function called varies with different D, unlike
generics in other languages
}

You can''t have virtual dispatch without an object because virtual dispatch
depends on the runtime type of the object, no object... no runtime type.

Another option is to have a structure of function pointers, filled in for
each type. Then a pointer to this structure can be passed around, or a
virtual function in the interface can return it. This is essentially how
virtual calls work to begin with, except that the C++ virtual syntax doesn''t
let you refer to the v-table and pass it around independent of an object.


>
>

专业化:


模板<>

void Target< int>: :bFunc(Base< int * ptr)

{

// ptr必须是Derived1,在你的具体情况下,施放它

Derived1 * p1 =(Derived1 *)ptr; //或使用dynamic_cast ...

p1-> Derived1Func(...); //调用静态函数
Specialize it:

template <>
void Target<int>::bFunc(Base<int*ptr)
{
// ptr must be Derived1, in your specific case, cast it
Derived1 *p1 = (Derived1 *) ptr; // or use dynamic_cast...
p1->Derived1Func(...); // call the static function



你不能(或不应该)使用对象实例调用静态函数,

它充其量是误导性的。


Type :: Member()是引用静态函数的正确方法。

You don''t (or shouldn''t) call static functions using an object instance,
it''s misleading at best.

Type::Member() is the correct way to refer to a static function.


}
}



这篇关于(缺少)虚拟静态函数的任何替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆