换句话说,你的代码*非常粗略*类似于以下内容: 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 notexist in memory?. In the other hand, if you try to access to the othermethod like this:printf("%d",Object->GiveMeValue());it crash!, and obviusly it crash because the object not exist inmemory and when you attempt to access to it variable "value" it read''sthe 0x00000 direction.But my question is about the method, the method''s in C++ classesexists without produce any object in code?. I''m thinking about and ihave a possibly explanation but what do you think about?.ThanksGiancarlo Berenz 解决方案Because C++ is very efficient and very mechanical. It will not check manydetails of your output code, including whether a pointer is correctlyseated. That''s because C++ must compete with assembler (where you can getaway with much worse things), while compiling huge programs in reasonabletime.So, at -time, C++ inserted no opcodes into the output program that checkedwhere Object points. The previous line could have pointed it to a legitimateobject, 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.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!!!Because C++ is very efficient and very mechanical. It will not check manydetails of your output code, including whether a pointer is correctlyseated. That''s because C++ must compete with assembler (where you can getaway with much worse things), while compiling huge programs in reasonabletime.[snip]It has nothing to do with assembly.IMO the compiler has no way of knowing whether the pointer is correct ornot. It doesn''t know about register memory layout by example and I canperfectly design a plateform with memory starting at 0x0000000 in whichcase deferencing NULL is valid.The only thing the compiler can do is checking the type and that isalready a good thing C++ is strongly typed.MichaelSince you write a definition for your functions, they can be compiledand their code can exist in memory. The object they "belong to" is justa pointer passed as a hidden parameter, and if that parameter is notused, 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> 这篇关于一个简单的课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 13:00
查看更多