为什么没有静态虚函数? [英] Why are there no static virtual functions?

查看:61
本文介绍了为什么没有静态虚函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我不太清楚为什么没有静态虚函数。我好b $ b会喜欢这样的东西:


class Base {

static virtual std :: string getName(){

返回" Base";

}

}


class派生:public Base {

static std :: string getName(){

return" Derived";

}

}


所以我可以比较一些指针给我的东西。例如。


Base * pointer = getSomeBasePointer();

if(pointer-> getName == Derived :: getName()){

doSomething();

}


有人可以解释一下为什么会这样吗?

谢谢Phil

Hello
I don''t exactly understand why there are no static virtual functions. I
would have liked something like this:

class Base{
static virtual std::string getName(){
return "Base";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil

推荐答案

" Philipp" < si ******* @ freesurf.chwrote in message

news:11 ************* @ sicinfo3.epfl.ch ...
"Philipp" <si*******@freesurf.chwrote in message
news:11*************@sicinfo3.epfl.ch...

你好

我不完全理解为什么没有静态虚函数。我好b $ b会喜欢这样的东西:


class Base {

static virtual std :: string getName(){

返回" Base";

}

}


class派生:public Base {

static std :: string getName(){

return" Derived";

}

}


所以我可以比较一些指针给我的东西。例如。


Base * pointer = getSomeBasePointer();

if(pointer-> getName == Derived :: getName()){

doSomething();

}


有人可以解释一下为什么会这样吗?

谢谢Phil
Hello
I don''t exactly understand why there are no static virtual functions. I
would have liked something like this:

class Base{
static virtual std::string getName(){
return "Base";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil



为什么要让它静止?只需摆脱静态关键字,

就可以实现你想要的。


你希望通过向
方法?

Why would you want to make it static? Just get rid of the static keyword,
and it does what you want.

What are you hoping to accomplish by adding the static keyword to the
method?


Philipp schrieb:
Philipp schrieb:

你好

我不完全理解为什么没有静态虚函数。我
Hello
I don''t exactly understand why there are no static virtual functions. I



因为虚函数依赖于实际对象,所以你用函数调用函数

。但静态函数不需要对象。

Because virtual functions depend on the actual object you call the function
with. But static functions don''t need objects.


会喜欢这样的东西:
would have liked something like this:



[ ...]

[...]


所以我可以比较一些指针给我的东西。例如。


Base * pointer = getSomeBasePointer();

if(pointer-> getName == Derived :: getName()){

doSomething();

}
so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}



这可以用typeid完成:


if(typeid(* pointer)== typeid(Derived))

// ...


-

Thomas
http://www.netmeister.org/news /learn2quote.html


Thomas J. Gritzan写道:
Thomas J. Gritzan wrote:

Philipp schrieb:
Philipp schrieb:

> Hello
我不完全理解为什么没有静态虚函数。我
>Hello
I don''t exactly understand why there are no static virtual functions. I



因为虚函数依赖于实际对象,所以你用函数调用函数

。但静态函数不需要对象。


Because virtual functions depend on the actual object you call the function
with. But static functions don''t need objects.



我想到的是虚拟如果被派生类覆盖,这是错的吗?


从这个意义上讲,我期待静态函数可以覆盖

by a派生类(即:函数是虚拟_和_静态)。


从你的解释我明白这是一个编译器/

优化问题。

I think of "virtual" as "gets overriden by derived classes", is this wrong?

In that sense, I was expecting that a static function could be overriden
by a derived class (ie: function is virtual _and_ static).

From your explaination I understand that this is a compiler /
optimization issue.


这可以用typeid完成:


if(typeid(* pointer)== typeid(Derived))< br $>
// ...
This can be done with typeid:

if (typeid(*pointer) == typeid(Derived))
// ...



是的,谢谢(我是用动态演员做的,同样的事情)。


Phil

Yes thanks (I did it with dynamic cast, same thing).

Phil


这篇关于为什么没有静态虚函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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