模拟“静态纯虚拟”方法 [英] simulating "static pure virtual" methods

查看:52
本文介绍了模拟“静态纯虚拟”方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道在C ++中使用静态纯虚方法是违法的,

但似乎这样的东西会很有用当以下2

条件成立时:


1.您知道每个Derived类都需要实现
一些方法,但以不同的方式实现,并且基础

类无法实现它。这就是纯虚拟的用武之地。


2.您知道,对于每个Derived类,方法

从不使用或更改成员数据。因此它应该是静态的。


给出一个具体的例子。


假设你有一个抽象的Document类,它有一个名为<的方法br />
processDocument:


void processDocument(){

if(isApproved(_documentAuthor))

callSomeNonVirtualFunction ();

其他

callAnotherNonVirtualFunction();

}


只是为了轻松例如,让我们说我们知道isApproved()是

作者姓名的一些愚蠢的功能,它作为

争论传递。也许对于派生的KinkosDocument,isApproved()返回

如果作者的名字以A开头,则为true,否则为false。

对于其他一些派生的阶级规则会有所不同,但总是一个

函数的作者姓名本身。


所以我们有以下内容:


1. isApproved()不应该在基础Document类中实现。


2. isApproved()必须在任何派生的具体类中实现。


3. isApproved()必须是字符串的简单函数。


对于这样的情况,什么是正确的设计? />

谢谢,

cpp

Hi,

I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:

1. You know that every one of your Derived classes will need to
implement some method, but implement it differently, and that the base
class cannot implement it. This is where pure virtual comes in.

2. You know that for every one of your Derived classes, the method
never uses or alters member data. Thus it should be static.

To give a concrete example.

Say you have an abstract Document class, which has a method called
processDocument:

void processDocument() {
if (isApproved(_documentAuthor))
callSomeNonVirtualFunction();
else
callAnotherNonVirtualFunction();
}

Just to take an easy example, let''s say that we know isApproved() is
some silly function of the author''s name, which is passed in as an
arguement. Maybe for a derived KinkosDocument, isApproved() returns
true if the author''s name begins with "A", and is otherwise false.
For some other derived class the rule would be different, but always a
function of the author''s name itself.

So we have the following:

1. isApproved() should not be implemented in the base Document class.

2. isApproved() must be implemented in any derived concrete class.

3. isApproved() must be a simple function of a string.

What is the right design for a situation like this?

Thanks,
cpp

推荐答案



" cppaddict" <他*** @ hello.com>在消息中写道

news:ck ******************************** @ 4ax.com ...

"cppaddict" <he***@hello.com> wrote in message
news:ck********************************@4ax.com...


我知道在C ++中使用静态纯虚方法是违法的,
但似乎这样的东西会很有用当以下2个条件成立时:
Hi,

I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:



也许你需要这样的东西 -


struct A {

virtual void foo()= 0;

};


struct B:A {

void foo() {

staticB();

}

static void staticB(){}

};


struct C:A {

void foo(){

staticC();

}


static void staticC(){}

};


int main()

{

}


-Sharad


Perhaps you need something like this -

struct A{
virtual void foo() =0;
};

struct B : A{
void foo(){
staticB();
}
static void staticB(){}
};

struct C : A{
void foo(){
staticC();
}

static void staticC(){}
};

int main()
{
}

-Sharad


cppaddict写道:
cppaddict wrote:



我知道在C ++中使用静态纯虚方法是违法的,
但似乎如此当满足以下2个条件时,这样的方法会很有用:

1.你知道每个Derived类都需要实现某些方法,但是实现它不同的是,基础类无法实现它。这就是纯虚拟的用武之地。

2.您知道,对于您的每个Derived类,该方法从不使用或更改成员数据。因此它应该是静态的。

给出一个具体的例子。

假设你有一个抽象的Document类,它有一个名为
processDocument的方法:
void processDocument(){
if(isApproved(_documentAuthor))
callSomeNonVirtualFunction();

callAnotherNonVirtualFunction();
}

只是举一个简单的例子,让我们说我们知道isApproved()是作者姓名的一些愚蠢的功能,这是作为
争论传递的。也许对于派生的KinkosDocument,isApproved()返回
如果作者的名字以A开头,则返回true,否则为false。
对于某些其他派生类,规则会有所不同,但是总是作者姓名本身的功能。

Hi,

I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:

1. You know that every one of your Derived classes will need to
implement some method, but implement it differently, and that the base
class cannot implement it. This is where pure virtual comes in.

2. You know that for every one of your Derived classes, the method
never uses or alters member data. Thus it should be static.

To give a concrete example.

Say you have an abstract Document class, which has a method called
processDocument:

void processDocument() {
if (isApproved(_documentAuthor))
callSomeNonVirtualFunction();
else
callAnotherNonVirtualFunction();
}

Just to take an easy example, let''s say that we know isApproved() is
some silly function of the author''s name, which is passed in as an
arguement. Maybe for a derived KinkosDocument, isApproved() returns
true if the author''s name begins with "A", and is otherwise false.
For some other derived class the rule would be different, but always a
function of the author''s name itself.




plus - 这很重要 - 文件的类型应该是

检查名称。所以在上面你根本就不能打电话给

到isApproved。它需要是一些班级成员。但是然后

你需要该类的一个对象来确定Derived类的动态类型,因此isApproved可以是

a普通的虚拟成员该类的功能。

在这种情况下,静态方法无法做得更好或更差

。您仍然需要一个对象来调用方法

来识别应该进行调用的上下文。


如果你没有,静态函数是有用的我想指定

任何上下文,但使用该类作为一种''命名空间'',以避免

a自由站立功能。在你的例子中,函数不能是独立的,因此静态对这个问题没有任何用处。

-

Karl Heinz Buchegger
kb******@gascad.at


cppaddict写道:
cppaddict wrote:


我知道在C ++中使用静态纯虚方法是违法的,
但似乎是当以下2个条件成立时,这样会很有用:

1.您知道每个Derived类都需要实现某些方法,但实现方式不同,而且基类
类无法实现它。这就是纯虚拟的用武之地。

2.您知道,对于您的每个Derived类,该方法从不使用或更改成员数据。因此它应该是静态的。

给出一个具体的例子。

假设你有一个抽象的Document类,它有一个名为
processDocument的方法:
void processDocument(){
if(isApproved(_documentAuthor))
callSomeNonVirtualFunction();

callAnotherNonVirtualFunction();
}

只是举一个简单的例子,让我们说我们知道isApproved()是作者姓名的一些愚蠢的功能,这是作为
争论传递的。也许对于派生的KinkosDocument,isApproved()返回
如果作者的名字以A开头,则返回true,否则为false。
对于某些其他派生类,规则会有所不同,但是总是作者名字本身的功能。

所以我们有以下内容:

1. isApproved()不应该在基础上实现文档类。

2. isApproved()必须在任何派生的具体类中实现。

3. isApproved()必须是字符串的简单函数。

对于这种情况,什么是正确的设计?
Hi,

I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:

1. You know that every one of your Derived classes will need to
implement some method, but implement it differently, and that the base
class cannot implement it. This is where pure virtual comes in.

2. You know that for every one of your Derived classes, the method
never uses or alters member data. Thus it should be static.

To give a concrete example.

Say you have an abstract Document class, which has a method called
processDocument:

void processDocument() {
if (isApproved(_documentAuthor))
callSomeNonVirtualFunction();
else
callAnotherNonVirtualFunction();
}

Just to take an easy example, let''s say that we know isApproved() is
some silly function of the author''s name, which is passed in as an
arguement. Maybe for a derived KinkosDocument, isApproved() returns
true if the author''s name begins with "A", and is otherwise false.
For some other derived class the rule would be different, but always a
function of the author''s name itself.

So we have the following:

1. isApproved() should not be implemented in the base Document class.

2. isApproved() must be implemented in any derived concrete class.

3. isApproved() must be a simple function of a string.

What is the right design for a situation like this?




基类不需要声明方法。

虚拟方法的要点是通过指针允许子类特定的行为

(或引用)到基类型的对象。你不可能有一个整个班级指向

,所以没有必要使用静态虚拟方法。


如果客户端代码期望一个类的一些功能,并且该功能(例如具有特定名称的

静态方法)丢失,您将得到一个

编译时错误。 />

如果你想确保一组类型中的每一种都能以特定的方式使用

的先验,你可以写一个约束。上课要表达

的要求。您必须为每个要明确检查的类型实例化类。如果您觉得这种方法很有用,请查看

Boost Concept Checking库。


struct A

{

//每个子类都应该有一个默认的构造函数,以及一个名为foo的静态$ / b $ b方法。可以在没有arg'的情况下调用。

//

//为A / $
派生的每种类型实例化A :: Check_constraints //检查这些要求。


模板< typename T>

struct Check_constraints

{

static void check_for_default_constructor()

{

T t = T();

}


static void check_for_static_method_foo()

{

T :: foo();

}

};

};


struct B:A

{

static void foo(){}

B(){}

};


struct C:A

{

C(){}

};


/ *显式实例化验证B和C符合A'的要求。

* /

模板类A: :CHECK_CONSTRAINTS< B> ;;

模板类A :: Check_constraints< C> ;;



There''s no need for the base class to declare the method. The point of
virtual methods is to allow subclass-specific behavior through pointers
(or references) to objects of a base type. You can''t have a pointer to
an entire class, so there''s no point in having a static virtual method.

If client code expects some feature of a class, and that feature (e.g. a
static method with a particular name) is missing, you will get a
compile-time error.

If you want to ensure a priori that each of a group of types can be used
in particular ways, you can write a "constraints" class to express the
requirements. You do have to instantiate the class for each type you
want to check explicitly. If you find this approach useful, check out
the Boost Concept Checking library.

struct A
{
// Each subclass should have a default constructor, and a static
// method called "foo" that can be called with no arg''s.
//
// Instantiate A::Check_constraints for each type derived from A
// to check these requirements.

template< typename T >
struct Check_constraints
{
static void check_for_default_constructor ( )
{
T t = T( );
}

static void check_for_static_method_foo( )
{
T::foo( );
}
};
};

struct B: A
{
static void foo( ) { }
B ( ) { }
};

struct C: A
{
C ( ) { }
};

/* Explicit instantiations verify that B and C meet A''s requirements.
*/
template class A::Check_constraints< B >;
template class A::Check_constraints< C >;


这篇关于模拟“静态纯虚拟”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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