一个简单的课程 [英] A simple class

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

问题描述

嗨:


最近我写这段代码:


class简单

{

private:

int value;

public:

int GiveMeARandom(void);

int GiveMeValue(无效);

}


int简单:: GiveMeARandom(无效)

{

return rand()%100;

}


int Simple :: GiveMeValue(void)

{

返回此 - >值;

}


....


int main ()

{

Simple * Object = NULL;

printf("%d",Object-> GiveMeARandom());

返回0;

}

嗯,这段代码编译好了,当我尝试使用它们时......工作!

但我的问题是我如何能够访问内存中不存在
的对象的方法?另一方面,如果您尝试访问其他

方法,请执行以下操作:


printf("%d",Object-> GiveMeValue ());


它崩溃!并且很明显它崩溃了,因为该对象不存在于

内存中以及当你试图访问它时变量值"它读取了'$

0x00000方向。


但我的问题是关于方法,方法'在C ++类中

是否在代码中没有生成任何对象?我正在考虑和我

有一个可能的解释,但你怎么看?


谢谢

Giancarlo Berenz

解决方案
吉安卡洛Berenz写道:

简单*对象= NULL;

printf("%d",Object-> GiveMeARandom());


嗯,这段代码编译好了,当我试图使用它们时......工作!,

但我的问题是我如何能够访问内存中不存在
的对象的方法?



因为C ++非常高效且非常机械化。它不会检查输出代码的许多

细节,包括指针是否正确

就位。那是因为C ++必须与汇编程序竞争(在那里你可以用更糟糕的东西获得
),同时在合理的时间内编译大量的程序。


因此,在-time,C ++没有在输出程序中插入任何操作码,检查

,其中Object指向。上一行可能已经将它指向一个合法的

对象,或悬挂它,或者像你一样将它清空。


当你写一个破碎的程序像这样,你得到未定义的行为。

这意味着程序可以按照你期望的方式工作,或以另一种方式工作,

或崩溃,或程序可以爆炸最近的厕所。


printf("%d",Object-> GiveMeValue());


它会崩溃!并且很明显它崩溃了,因为该对象不存在于
内存中,当你试图访问它时,变量value它在0x00000的方向上读'$




另一个函数没有理由在它运行时触摸假定的对象,

所以它意外地看起来工作正常。 />

-

Phlip
http://www.greencheese.us/ZeekLand < - 不是博客!!!


Phlipaécrit:


Giancarlo Berenz写道:


> Simple * Object = NULL;
printf("%d",Object-> GiveMeARandom());


>好吧,这段代码编译好了,当我尝试使用它们时......工作!,
但是我的问题是我如何访问一个不存在于内存中的对象的方法?



因为C ++非常高效且非常机械化。它不会检查输出代码的许多

细节,包括指针是否正确

就位。那是因为C ++必须与汇编程序竞争(在那里你可以用更糟糕的东西获得
),同时在合理的时间内编译大量的程序。

[snip]



它与汇编无关。

IMO编译器无法知道指针是否是正确的还是

没有。它不知道寄存器内存布局的例子我可以完美地设计一个平台,内存从0x0000000开始,其中

大小写引用NULL是有效的。


编译器可以做的唯一事情就是检查类型,这是一个好东西C ++是强类型的。


迈克尔


勒2007年6月3日3时59,吉安卡洛Berenz一个ecrit:

您好:


最近我写这段代码:


class简单

{

private :

int value;

public:

int GiveMeARandom(void);

int GiveMeValue(void);

}


int简单:: GiveMeARandom(无效)

{

返回rand() %100;

}


int简单:: GiveMeValue(无效)

{

返回this-> value;

}


....


int main()

{

简单*对象=空;

printf("%) d",Object-> GiveMeARandom());

返回0;

}


嗯,这段代码编译'没关系,当我试图使用它们时......工作!,

但我的问题是我如何能够访问一个不存在
的对象的方法记忆?。另一方面,如果您尝试访问其他

方法,请执行以下操作:


printf("%d",Object-> GiveMeValue ());


它崩溃!并且很明显它崩溃了,因为该对象不存在于

内存中以及当你试图访问它时变量值"它读取了'$

0x00000方向。


但我的问题是关于方法,方法'在C ++类中

是否在代码中没有生成任何对象?我正在考虑和我的b $ b有一个可能的解释,但你怎么看?



既然您为函数编写了一个定义,它们就可以编译成
,它们的代码可以存在于内存中。他们所属的对象是只是

a指针作为隐藏参数传递,如果该参数不是

使用,它可能适用于某些实现,如你的。

换句话说,你的代码*非常粗略*类似于以下内容:


class简单

{

private:

int value;


friend int Simple_GiveMeARandom(Simple *);

friend int Simple_GiveMeValue(Simple *) ;

}


int Simple_GiveMeARandom(简单* / *未使用* /)

{

return rand()%100;

}


int Simple_GiveMeValue(简单*那个)

{

返回 - >值;

}


.....


int main( )

{

Simple * Object = NULL;

printf("%d",Simple_GiveMeARandom(Object));

返回0;

}


-

Serge Paccalin

< se ************ @ easyvisio.net>

Hi:

Recently i write this code:

class Simple
{
private:
int value;
public:
int GiveMeARandom(void);
int GiveMeValue(void);
}

int Simple::GiveMeARandom(void)
{
return rand()%100;
}

int Simple::GiveMeValue(void)
{
return this->value;
}

....

int main()
{
Simple * Object = NULL;
printf("%d",Object->GiveMeARandom());
return 0;
}
Well, this code compile''s ok and when i tried to use them... works!,
but my question is how i can access to a method of an object that not
exist in memory?. In the other hand, if you try to access to the other
method like this:

printf("%d",Object->GiveMeValue());

it crash!, and obviusly it crash because the object not exist in
memory and when you attempt to access to it variable "value" it read''s
the 0x00000 direction.

But my question is about the method, the method''s in C++ classes
exists without produce any object in code?. I''m thinking about and i
have a possibly explanation but what do you think about?.

Thanks
Giancarlo Berenz

解决方案

Giancarlo Berenz wrote:

Simple * Object = NULL;
printf("%d",Object->GiveMeARandom());

Well, this code compile''s ok and when i tried to use them... works!,
but my question is how i can access to a method of an object that not
exist in memory?

Because C++ is very efficient and very mechanical. It will not check many
details of your output code, including whether a pointer is correctly
seated. That''s because C++ must compete with assembler (where you can get
away with much worse things), while compiling huge programs in reasonable
time.

So, at -time, C++ inserted no opcodes into the output program that checked
where Object points. The previous line could have pointed it to a legitimate
object, or dangled it, or NULLed it, like you did.

When you write a broken program like that, you get "undefined behavior".
That means the program could work the way you expect, or work another way,
or crash, or the program could explode the nearest toilet.

printf("%d",Object->GiveMeValue());

it crash!, and obviusly it crash because the object not exist in
memory and when you attempt to access to it variable "value" it read''s
the 0x00000 direction.

And the other function had no reason to touch the presumed object as it ran,
so it accidentally appeared to work correctly.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


Phlip a écrit :

Giancarlo Berenz wrote:

> Simple * Object = NULL;
printf("%d",Object->GiveMeARandom());

>Well, this code compile''s ok and when i tried to use them... works!,
but my question is how i can access to a method of an object that not
exist in memory?


Because C++ is very efficient and very mechanical. It will not check many
details of your output code, including whether a pointer is correctly
seated. That''s because C++ must compete with assembler (where you can get
away with much worse things), while compiling huge programs in reasonable
time.

[snip]

It has nothing to do with assembly.
IMO the compiler has no way of knowing whether the pointer is correct or
not. It doesn''t know about register memory layout by example and I can
perfectly design a plateform with memory starting at 0x0000000 in which
case deferencing NULL is valid.

The only thing the compiler can do is checking the type and that is
already a good thing C++ is strongly typed.

Michael


Le 06.03.2007 03:59, Giancarlo Berenz a ecrit:

Hi:

Recently i write this code:

class Simple
{
private:
int value;
public:
int GiveMeARandom(void);
int GiveMeValue(void);
}

int Simple::GiveMeARandom(void)
{
return rand()%100;
}

int Simple::GiveMeValue(void)
{
return this->value;
}

....

int main()
{
Simple * Object = NULL;
printf("%d",Object->GiveMeARandom());
return 0;
}
Well, this code compile''s ok and when i tried to use them... works!,
but my question is how i can access to a method of an object that not
exist in memory?. In the other hand, if you try to access to the other
method like this:

printf("%d",Object->GiveMeValue());

it crash!, and obviusly it crash because the object not exist in
memory and when you attempt to access to it variable "value" it read''s
the 0x00000 direction.

But my question is about the method, the method''s in C++ classes
exists without produce any object in code?. I''m thinking about and i
have a possibly explanation but what do you think about?.

Since you write a definition for your functions, they can be compiled
and their code can exist in memory. The object they "belong to" is just
a pointer passed as a hidden parameter, and if that parameter is not
used, it may work on some implementations, like yours.

In other words, your code is *very roughly* similar to the following:

class Simple
{
private:
int value;

friend int Simple_GiveMeARandom(Simple *);
friend int Simple_GiveMeValue(Simple *);
}

int Simple_GiveMeARandom(Simple * /*unused*/)
{
return rand() % 100;
}

int Simple_GiveMeValue(Simple *that)
{
return that->value;
}

.....

int main()
{
Simple * Object = NULL;
printf("%d",Simple_GiveMeARandom(Object));
return 0;
}


--
Serge Paccalin
<se************@easyvisio.net>


这篇关于一个简单的课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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