static关键字 [英] static keyword

查看:86
本文介绍了static关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A级

{

int dCol;

public:

const char * Display(void) const {return this == NULL? " A" :B ; }


};


int main(无效)

{


静态A * a;

printf(a是",a-> Display());


返回0; < br $>

}


打印A


我不明白为什么指针值是必要的NULL?

解决方案

Frank-O写道:


class A

{

int dCol;

public:

const char * Display(void)const {return this == NULL? " A" :B ; }


};


int main(无效)

{


静态A * a;

printf(a是",a-> Display());


返回0; < br $>

}


打印A


我不明白为什么指针值是必要的NULL?



因为指针的默认初始化为零初始化

它。由于这是一个静态的sorage对象,它会默认初始化。


但是,你的程序有UNDEFINED行为。当你取消引用空指针时,你无法保证




Frank-O写道:
< blockquote class =post_quotes>
class A

{

int dCol;

public:

const char *显示(void)const {return this == NULL? " A" :B ; }


};


int main(无效)

{


静态A * a;

printf(a是",a-> Display());


返回0; < br $>

}


它打印A



它可以打印任何东西: A->显示()"取消引用未初始化的指针

变量。程序有不确定的行为。


我不明白为什么指针值必须为NULL?



没有什么可以理解的:你的程序有不确定的行为;

任何事情都可能发生。

Best


Kai-Uwe Bux




" Kai-Uwe Bux" < jk ******** @ gmx.net写信息

新闻:eo ********** @ murdoch.acc.Virginia.EDU ...


Frank-O写道:


> class A
{
int dCol;
public:
const char *显示(void)const {return this == NULL? " A" :B ; }

};

int main(void)


静态A * a;
printf(" a是,a->显示());

返回0;

}
它打印A



它可以打印任何内容:a-> Display()取消引用未初始化的

指针

变量。该程序具有未定义的行为。



我不相信这是真的。 a-> Display()实际上并非解除引用

指针。无论是否创建了'A'的任何

实例,Display()方法都存在,并且无论
$ b $如何,它都将通过a->进行调用b''a''的值。也就是说,根据具有有效的非NULL指针,调用类中方法的能力不是
。当然,如果

该方法试图利用数据成员(例如示例中的''dCol')

,那么会发生什么?这个''指针未初始化(或者这个

情况下为NULL),完全是未知的。


例如,如果这样写了Display(): br />

const void *显示(void)const {return this; }


然后:


A * a =(A *)0x12345678;

printf(" a)是%p",a->显示());


....应该打印:a is 12345678"没有任何困难,即使分配给''a''的地址的内存很可能包含垃圾,因为

它没有使用''这个''指针(默认传递给

函数)。但是,以下情况可能会崩溃:


const void *显示(void)const {dCol = 6;归还这个;
因为它试图修改它很可能不拥有的内存,

这可能会导致访问冲突或核心转储。


我认为通过无效指针调用方法可能会导致某些实现中的未定义行为,但是每一个

我使用过的编译器,类方法总是可用并且可以通过未初始化和NULL指针来调用
- 你只是无法知道

如果你试图访问将会发生什么记忆通过''this''指针。


- 丹尼斯


class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

I don'' t understand why the pointer value is necessary NULL ?

解决方案

Frank-O wrote:

class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

I don'' t understand why the pointer value is necessary NULL ?

because the default initialization of a pointer is to zero intialize
it. Since this is a static sorage object, it does get default initialized.

However, your program has UNDEFINED behavior. You are not guaranteed
of anything when you dereference a null pointer.


Frank-O wrote:

class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

It may print anything: "a->Display()" dereferences an uninitialized pointer
variable. The program has undefined behavior.

I don'' t understand why the pointer value is necessary NULL ?

There is nothing to understand: your program has undefined behavior;
anything can happen.
Best

Kai-Uwe Bux



"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...

Frank-O wrote:

>class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A


It may print anything: "a->Display()" dereferences an uninitialized
pointer
variable. The program has undefined behavior.

I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer. The Display() method exists regardless of whether or not any
instances of ''A'' are created, and it will get invoked via a->, regardless of
the value of ''a''. That is, the ability to call methods in the class is not
dependent on having a valid, non-NULL pointer. Of course, what happens if
the method attempts to make use of data members (like ''dCol'' in the example)
within the class, since the ''this'' pointer is uninitialized (or NULL in this
case), is completely unknown.

For example, if Display() were written thus:

const void * Display(void) const { return this; }

Then:

A *a = (A *)0x12345678;
printf("a is %p", a->Display());

....should print: "a is 12345678" without any difficulty, even though the
memory at the address assigned to ''a'' is likely to contain garbage, because
it makes no use of the ''this'' pointer (which is silently passed to the
function). However, the following would probably crash:

const void * Display(void) const { dCol = 6; return this; }

because it is attempting to modify memory that it very likely does not own,
which would probably cause an access violation or core dump.

I suppose it is possible that calling a method through an invalid pointer
may result in undefined behavior in some implementations, but on every
compiler I have used, class methods are always available and callable
through uninitialized and NULL pointers -- you just have no way of knowing
what will happen if you try to access memory through the ''this'' pointer.

- Dennis


这篇关于static关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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