avatar
t*s
2
1。 Sunrise Over Potomac River
2010年最后一个早上拍的。祝大家新年万事如意!
2。 DC Sunset Rider
然后献上圣诞那天拍的,祝大家事业如日中天,摄影水平一骑绝尘,没有traffic。:)
avatar
b*d
3
class myc
{
...
public:
...
void donothing() {cout<};
int main()
{
myc *p=0;
p->donothing();
}
output:
I do nothing!
经我测试,default constructor, copy constructor, = assignment operator 都没
有被调用。而且指针为0,换成NULL也一样。但是却可以使用类的函数。
请问:p指向的object(这里其实都没有,因为是0)是怎么产生的?
avatar
b*y
4
近期要考REG的同学可以加QQ群 142602456 做广告搞推销的一律免进,进了踢。欢迎大
家一起学习讨论。我11月底考,大家互帮互助!
avatar
m*a
5
你不如问问组里的人
各个地方情况不一样
有些组给的条件就是很FIRM的
avatar
m*1
6
Very nice!!!
avatar
l*s
7
under the hood, member function is really a normal function with a hidden
parameter bound to "this".
you passed in an invalid pointer, but it does not cause problem because you
don't use instance data. o.w, you will trigger runtime error.
avatar
b*y
8
自己更新一下,希望更多同学看见。
avatar
l*b
9
我就哭穷,说我得养家糊口,你们这里消费又高。FYI,另一个教授(没写名字)开给
我多少多少钱。我不指望你Match,你给我xxxxx刀就行了。我提的这个数字是调查过的
,就是有人能拿这么高(虽然大部分人拿不到这么多)

【在 z*******g 的大作中提到】
: 她会不会很不爽?以后穿小鞋?
avatar
r*5
10
赞。那个大冷天,我在被窝睡懒觉,你在外面吹冷风。发觉自己越来越懒了。
avatar
g*e
11
只有access member variable的时候才用到p指针
avatar
v*0
12
lz
, qq number is not valid.
avatar
g*e
13
只有access member variable的时候才用到p指针
avatar
b*y
14
没错。是群,要加群。
avatar
b*d
15
谢谢!
我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
是p可以直接在member function列表里找需要的函数,虽然p=0?

you

【在 l*********s 的大作中提到】
: under the hood, member function is really a normal function with a hidden
: parameter bound to "this".
: you passed in an invalid pointer, but it does not cause problem because you
: don't use instance data. o.w, you will trigger runtime error.

avatar
l*s
16
2nd explanation is partially correct as object creation is not involved at
all. but there is no such process of member function lookup. Non-virtual
function address is determined at compiling time.

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

avatar
x*q
17
可以这么理解,编译器是指上把这个方程编译成了类似
blahblah7myc8donothing9Ev(myc*)的C方程。
C++是静态语言,编译时就已经通过p的namespace,declared type/class和function
name找到了上述方
程,然后把p做参数压栈,call blahblah7myc8donothing9Ev而已。
所以,并非通过p找到了member function,而是通过p的类型找到的该方程。非virtual
function,每个类的object并不保存member function的地址。

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

avatar
t*t
18
I would say this compiler sucks. it should catch such error instead of
letting it pass.
avatar
t*t
19
catching such error requires runtime check. most of these kind of error, are
"no diagnostic required" by standard.

【在 t***t 的大作中提到】
: I would say this compiler sucks. it should catch such error instead of
: letting it pass.

avatar
t*t
20
for this case, no runtime check is needed.
compiler should give warning whenever an integer is used as address and
assigned to a pointer. which means, pointer can only be get from new or from
another pointer. IMO, developer should never do it.
so such code will always get warning and alert the developer.
or, can set the compiler stricter, give error instead of warning.
i don't see the need to assign integer to pointer unless for very low level
programming.

are

【在 t****t 的大作中提到】
: catching such error requires runtime check. most of these kind of error, are
: "no diagnostic required" by standard.

avatar
t*t
21
you do know null pointer is traditionally expressed by number 0, don't you?
in fact, literal integer 0 is the only way to express null pointer. NULL is
simply a macro form of literal integer 0. you want a warning everytime null
pointer is used?
of course, in c++11, there is this new nullptr keyword.

from
level

【在 t***t 的大作中提到】
: for this case, no runtime check is needed.
: compiler should give warning whenever an integer is used as address and
: assigned to a pointer. which means, pointer can only be get from new or from
: another pointer. IMO, developer should never do it.
: so such code will always get warning and alert the developer.
: or, can set the compiler stricter, give error instead of warning.
: i don't see the need to assign integer to pointer unless for very low level
: programming.
:
: are

avatar
t*t
22
this can be enforced by compiler.
if assign integer 0, give warning.
if assign by macro null, pass.
and actually this can only be done by compiler, not run time, because
pointer is integer at runtime. only compiler knows that it's integer or
pointer.

?
is
null

【在 t****t 的大作中提到】
: you do know null pointer is traditionally expressed by number 0, don't you?
: in fact, literal integer 0 is the only way to express null pointer. NULL is
: simply a macro form of literal integer 0. you want a warning everytime null
: pointer is used?
: of course, in c++11, there is this new nullptr keyword.
:
: from
: level

avatar
t*t
23
basically you want to mix PP stage with compile stage.
i think this is generally a very bad idea, unless absolutely necessary. NULL
(not null) or 0 is simply the same token, i think there is more important t
hings to warn about, e.g. multi-value-change-between-sequence-point type of
error; this is implementable and worth the trouble.
distinguishing null or 0 is not worth it. besides there is this new nullptr
keyword now.

【在 t***t 的大作中提到】
: this can be enforced by compiler.
: if assign integer 0, give warning.
: if assign by macro null, pass.
: and actually this can only be done by compiler, not run time, because
: pointer is integer at runtime. only compiler knows that it's integer or
: pointer.
:
: ?
: is
: null

avatar
n*t
24
as long as you know how things work, there is no problem for this kind of
usage. And if you do not know how things work, your C++ will definitely suck.

【在 t***t 的大作中提到】
: I would say this compiler sucks. it should catch such error instead of
: letting it pass.

avatar
f*n
25
Technically according to the standard, it's undefined behavior. Undefined
behavior means it can do anything, crash or not, or blow up your computer.
Whatever.
In practice, non-virtual function calls are looked up at compile time in all
compilers. The pointer is not used in determining which function to call.

【在 b*****d 的大作中提到】
: class myc
: {
: ...
: public:
: ...
: void donothing() {cout<: };
: int main()
: {
: myc *p=0;

avatar
b*s
26
C++的类的内存布局就是这样的,和实例无关的单独有一块地方,是这个类共有的

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

avatar
d*n
27

前段时间看见CLANG的邮件列表里有人讨论,也许不用多久,会加入到clang的
undefined behavior sanitizer里了。

【在 t****t 的大作中提到】
: basically you want to mix PP stage with compile stage.
: i think this is generally a very bad idea, unless absolutely necessary. NULL
: (not null) or 0 is simply the same token, i think there is more important t
: hings to warn about, e.g. multi-value-change-between-sequence-point type of
: error; this is implementable and worth the trouble.
: distinguishing null or 0 is not worth it. besides there is this new nullptr
: keyword now.

avatar
t*t
28
gcc already has this warning option, although it is said not 100% accurate.

【在 d****n 的大作中提到】
:
: 前段时间看见CLANG的邮件列表里有人讨论,也许不用多久,会加入到clang的
: undefined behavior sanitizer里了。

avatar
x*u
29
我感觉clang的sanitizer是一种技术上很成功,但实际应用可能带来灾难的东西。
C++代码追求的不是发现的bug数多,而是残留的bug少,很多santizer实际上是鼓励危
险方式编程,并且给予程序员虚假的安全感。

【在 t****t 的大作中提到】
: gcc already has this warning option, although it is said not 100% accurate.
avatar
d*a
30
类似于你写了个如下的function和调用。
void donothing(myc *p)
{
cout << "I do nothing!" << endl;
}
int main()
{
myc *p = NULL;
donothing(p);
}

【在 b*****d 的大作中提到】
: 谢谢!
: 我的问题是p指针变量的值应该是个地址。一般情况是个object的地址。但这里是0. 那
: p是怎么找到member function的地址的?是系统产生了一个不完全的object交给p?还
: 是p可以直接在member function列表里找需要的函数,虽然p=0?
:
: you

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。