由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++里的函数可不可以是virtual+static
相关主题
How to check the virtual function table size?请问static variable init的问题?
copy constructor问题。请问如何把初始化一个const 的vector (or array) in a class?
C++小插曲static vector 怎么 initialize ?
问个虚函数的作用c++ 弱问题:static const char* const 这两个const 分别是什么意思?
问题:vptr/vtable for virtual function & vptr/vtable for纯虚函数问题
A C++ compiler related interview question为什么derived object没有vptr?
问一个C++问题:default parameter and overriding/inheritanc (转载)我有个很傻的问题,关于function call via pointer
Two questions on virtual destructorstatic variable存在heap还是stack?
相关话题的讨论汇总
话题: static话题: virtual话题: c++话题: method话题: sense
进入Programming版参与讨论
1 (共1页)
j*p
发帖数: 115
1
网上那个有好多讨论 大部分说不行
但是有人说行,只不过c++ standard里没有
是这么回事么?
t****t
发帖数: 6806
2
稍微想一想就知道不行了, 不make sense

【在 j*p 的大作中提到】
: 网上那个有好多讨论 大部分说不行
: 但是有人说行,只不过c++ standard里没有
: 是这么回事么?

g*********s
发帖数: 1782
3
base and derived both have a virtual static method with the same
signature. looks ok ah.

【在 t****t 的大作中提到】
: 稍微想一想就知道不行了, 不make sense
t****t
发帖数: 6806
4
if you can't think properly, try to learn to do some experiment properly.

【在 g*********s 的大作中提到】
: base and derived both have a virtual static method with the same
: signature. looks ok ah.

g*********s
发帖数: 1782
5
static_virtual.cpp:3: error: member ‘s’ cannot be declared both virtual
and static
but is this compiler dependent, or explicitly forbidden in standard?

【在 t****t 的大作中提到】
: if you can't think properly, try to learn to do some experiment properly.
e******0
发帖数: 211
6
From C++ primer
static 不属于对象,是属于累
virtual 是 base, dirived基于对象的绑定, 不适用于static
同理, static不能设置为const

【在 j*p 的大作中提到】
: 网上那个有好多讨论 大部分说不行
: 但是有人说行,只不过c++ standard里没有
: 是这么回事么?

g*********s
发帖数: 1782
7
不适用和不能是两回事吧。

【在 e******0 的大作中提到】
: From C++ primer
: static 不属于对象,是属于累
: virtual 是 base, dirived基于对象的绑定, 不适用于static
: 同理, static不能设置为const

e******0
发帖数: 211
8
不make sense

【在 g*********s 的大作中提到】
: 不适用和不能是两回事吧。
g*********s
发帖数: 1782
9
x = x;
it doesn't make sense but it's allowed.

【在 e******0 的大作中提到】
: 不make sense
z****e
发帖数: 2024
10
static只是借用了class作为namespace而已。
本质上和一个静态绑定的全局函数没有任何区别,显然不能virtual啊。

【在 e******0 的大作中提到】
: From C++ primer
: static 不属于对象,是属于累
: virtual 是 base, dirived基于对象的绑定, 不适用于static
: 同理, static不能设置为const

相关主题
A C++ compiler related interview question请问static variable init的问题?
问一个C++问题:default parameter and overriding/inheritanc (转载)请问如何把初始化一个const 的vector (or array) in a class?
Two questions on virtual destructorstatic vector 怎么 initialize ?
进入Programming版参与讨论
j*p
发帖数: 115
11
virtual是说函数由object的类决定的, static是说函数和属于某个类,和具体的
object无关 理论上两个没冲突啊
我看到的最make sense的解释就是 理论上可以 只不过当时设计c++的时候 没有做 因
为没有什么需求

【在 e******0 的大作中提到】
: 不make sense
t****t
发帖数: 6806
12
virtual call depends on object. but in static call there is no object. how
does that make sense?
of course, you can call static method in virtual method. that's very fine.
but that doesn't means virtual-static make sense.

【在 j*p 的大作中提到】
: virtual是说函数由object的类决定的, static是说函数和属于某个类,和具体的
: object无关 理论上两个没冲突啊
: 我看到的最make sense的解释就是 理论上可以 只不过当时设计c++的时候 没有做 因
: 为没有什么需求

e******0
发帖数: 211
13
都是高手

【在 t****t 的大作中提到】
: virtual call depends on object. but in static call there is no object. how
: does that make sense?
: of course, you can call static method in virtual method. that's very fine.
: but that doesn't means virtual-static make sense.

S****z
发帖数: 666
14
那请各大高手来解答一下我的问题
为什么C++的默认构造函数不初始化它的成员变量?
比如一个pointer int *,它默认初始化一下为NULL不是很好吗?

how
fine.

【在 e******0 的大作中提到】
: 都是高手
e******0
发帖数: 211
15
类类型调用默认构造函数
内置复合类型,在类的局域域,不初始化

【在 S****z 的大作中提到】
: 那请各大高手来解答一下我的问题
: 为什么C++的默认构造函数不初始化它的成员变量?
: 比如一个pointer int *,它默认初始化一下为NULL不是很好吗?
:
: how
: fine.

S****z
发帖数: 666
16
不初始化是基于啥出发点的考虑?

【在 e******0 的大作中提到】
: 类类型调用默认构造函数
: 内置复合类型,在类的局域域,不初始化

t****t
发帖数: 6806
17
快呗.

【在 S****z 的大作中提到】
: 不初始化是基于啥出发点的考虑?
S****z
发帖数: 666
18
就不怕我这种3流的C++程序员就把没初始化的数据或者指针拿去用?

【在 t****t 的大作中提到】
: 快呗.
t****t
发帖数: 6806
19
you may use a sanitizing tool for that specific purpose, such as valgrind.
but frankly, you better use it correctly, or don't use it at all.

【在 S****z 的大作中提到】
: 就不怕我这种3流的C++程序员就把没初始化的数据或者指针拿去用?
g*********s
发帖数: 1782
20
virtual call depends on the type of the object, right?
object can call static function, right?
what i'm thinking is:
class B {
virtual static void x();
};
class D: public B {
virtual static void x();
}
B* b = new D;
b->x(); // dynamic binding
B::x(); // static binding
D::x(); // static binding
it may not make sense. but i'm curious why it's disallowed. is there any
side effect by doing so?
back to my example, x = x; doesn't make sense but it's allowed, isn't
it?

how
fine.

【在 t****t 的大作中提到】
: virtual call depends on object. but in static call there is no object. how
: does that make sense?
: of course, you can call static method in virtual method. that's very fine.
: but that doesn't means virtual-static make sense.

相关主题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?我有个很傻的问题,关于function call via pointer
纯虚函数问题static variable存在heap还是stack?
为什么derived object没有vptr?c++ 一问
进入Programming版参与讨论
t****t
发帖数: 6806
21
look, virtual and static essentially contradicts each other. a method is
either non-virtual (if it is static), or non-static (if it is virtual). it
can't be both. this is very different from x=x, where x=x is probably
redundant but still makes sense.
now why it's disallowed? because virtual method calls with `this' pointer,
where static method calls without it. however c/c++ function prototype is
unique, so you can't do this.

【在 g*********s 的大作中提到】
: virtual call depends on the type of the object, right?
: object can call static function, right?
: what i'm thinking is:
: class B {
: virtual static void x();
: };
: class D: public B {
: virtual static void x();
: }
: B* b = new D;

P********e
发帖数: 2610
22
中文名字就比较好理解,一个叫多太,一个叫静态.
我觉得,static function可以互相call each other
但如果你的class B里面还有
virtual static void y();
x, y之间不能互相call, 就违背了static character

【在 g*********s 的大作中提到】
: virtual call depends on the type of the object, right?
: object can call static function, right?
: what i'm thinking is:
: class B {
: virtual static void x();
: };
: class D: public B {
: virtual static void x();
: }
: B* b = new D;

g*********s
发帖数: 1782
23

is
virtual). it
pointer,
is
assume such an implementation to support static virtual:
for b->x(), we first know "this" has dynamic type D, then we lookup
class D's static method list to find x().
here i don't see any problem on prototype uniqueness...
of course, the principle of programming board is "trust is always right
on c++" and i honor it. :) but i just want to have a deeper
understanding through the discussion.

【在 t****t 的大作中提到】
: look, virtual and static essentially contradicts each other. a method is
: either non-virtual (if it is static), or non-static (if it is virtual). it
: can't be both. this is very different from x=x, where x=x is probably
: redundant but still makes sense.
: now why it's disallowed? because virtual method calls with `this' pointer,
: where static method calls without it. however c/c++ function prototype is
: unique, so you can't do this.

e****d
发帖数: 895
24
Calling a static member function doesn't pass in "this" of
the dynamic type, which results in not being able to lookup
the correct vptr to the vtable.

【在 g*********s 的大作中提到】
:
: is
: virtual). it
: pointer,
: is
: assume such an implementation to support static virtual:
: for b->x(), we first know "this" has dynamic type D, then we lookup
: class D's static method list to find x().
: here i don't see any problem on prototype uniqueness...
: of course, the principle of programming board is "trust is always right

j*p
发帖数: 115
25
I found this on the jobhunting board where someone posted his/her interview
process...
第三轮,又一个senior,请问什是多态?我答曰:其概念加C++实现的例子,虚函数若干
,此人追问什么是虚函数,我答曰,Vpr,Vt,等等。此人继续追问,解释vpr,vt的细
节实现,我努力解释。他还算满意,在问,什么是static,class里的static成员函数
和成员变量如何工作,我继续努力解释。他长叹了一口气,说:根据你以上解释,可不
可以有 static virtual function这个功能。我那个吐啊。。。我说这个不行的,一个
静态绑定,一个动态绑定,怎么玩。他说,这个可以玩,你看。。。扒拉扒拉(他开始
解释)。我真是觉得很无
语,难到我的那些解释不管用么?算了,看你是面试官,不计较了,我招了,我说这个
可以有。面试官再长叹一口气:这个真没有。我日啊。我问为什么,他说:这个就是没有
。然后继续一道,singleton pattern的编程题。
haha

【在 t****t 的大作中提到】
: look, virtual and static essentially contradicts each other. a method is
: either non-virtual (if it is static), or non-static (if it is virtual). it
: can't be both. this is very different from x=x, where x=x is probably
: redundant but still makes sense.
: now why it's disallowed? because virtual method calls with `this' pointer,
: where static method calls without it. however c/c++ function prototype is
: unique, so you can't do this.

z***e
发帖数: 5393
26
如果可以,你把static method的vptr放哪里?放在class的virtual pointer table里
面?

【在 j*p 的大作中提到】
: virtual是说函数由object的类决定的, static是说函数和属于某个类,和具体的
: object无关 理论上两个没冲突啊
: 我看到的最make sense的解释就是 理论上可以 只不过当时设计c++的时候 没有做 因
: 为没有什么需求

g*********s
发帖数: 1782
27
why not? static or not, methods are based on class, not object.

【在 z***e 的大作中提到】
: 如果可以,你把static method的vptr放哪里?放在class的virtual pointer table里
: 面?

g**w
发帖数: 969
28
http://c2.com/cgi/wiki?VirtualStaticIdiom
class Foo
{
public:
Foo();
protected:
// Virtual static idiom <-- StaticCallback?()
virtual bool Callback();
private:
// Virtual static idiom --> Callback()
static bool StaticCallback?( void *pThis )
{
assert( pThis );
return ((Foo *)pThis)->Callback();
}
};
1 (共1页)
进入Programming版参与讨论
相关主题
static variable存在heap还是stack?问题:vptr/vtable for virtual function & vptr/vtable for
c++ 一问A C++ compiler related interview question
问一个java基础的初始化的问题,一直搞不明白问一个C++问题:default parameter and overriding/inheritanc (转载)
一个c++问题 (转载)Two questions on virtual destructor
How to check the virtual function table size?请问static variable init的问题?
copy constructor问题。请问如何把初始化一个const 的vector (or array) in a class?
C++小插曲static vector 怎么 initialize ?
问个虚函数的作用c++ 弱问题:static const char* const 这两个const 分别是什么意思?
相关话题的讨论汇总
话题: static话题: virtual话题: c++话题: method话题: sense