本文介绍了课堂提问 - 请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 谁能告诉我这两者有什么区别? class nodeType { int data; nodeType * link; }; 这是第一个: main() { nodeType * test; test-> data = 10; cout << nodeType->数据; // 10 } 这是第二个: main() { nodeType * test = new nodeType; test-> data = 10; cout<<测试 - >数据; // 10 } 我有点困惑,因为第一个是由我的导师给出的。 怎么样在第一种情况下分配的内存??有没有使用动态内存分配的好处或缺点? 提前谢谢。Can anyone tell me what is the difference between these two?class nodeType{int data;nodeType *link;};This is the first one:main(){nodeType *test;test->data=10;cout << nodeType->data; // 10}and this is the second one:main(){nodeType *test=new nodeType;test->data=10;cout << test->data; // 10}I am kind of confused because the first one was given by my instructor.How is the memory allocated in the first situation?? Is there anybenefit or drawback of not using dynamic memory allocation??Thanks in advance.推荐答案 " Roger" < GN **** @ hotmail.com>在消息中写道 news:xg ******************* @ nwrddc02.gnilink.net ..."Roger" <gn****@hotmail.com> wrote in messagenews:xg*******************@nwrddc02.gnilink.net...谁能告诉我这两个有什么区别? 类nodeType {int int data; nodeType * link; }; 这是第一个: main() {node> * test; test-> data = 10; cout<< nodeType->数据; // 10 } 这是第二个: main() {node> * node = new nodeType; test-> data = 10; cout<<测试 - >数据; // 10 我有点困惑,因为第一个是由我的导师给出的。在第一种情况下如何分配内存?有没有使用动态内存分配的好处或缺点? 提前致谢。 Can anyone tell me what is the difference between these two? class nodeType { int data; nodeType *link; }; This is the first one: main() { nodeType *test; test->data=10; cout << nodeType->data; // 10 } and this is the second one: main() { nodeType *test=new nodeType; test->data=10; cout << test->data; // 10 } I am kind of confused because the first one was given by my instructor. How is the memory allocated in the first situation?? Is there any benefit or drawback of not using dynamic memory allocation?? Thanks in advance. 好​​吧,看起来像你的导师在第一种情况下犯了大错!直到 指针指向某个东西,它才能被*使用!Well, looks like your instructor made a blunder in the first case! Until apointer points at something, it *can''t* be used! Dave写道:Dave wrote:" Roger" < GN **** @ hotmail.com>在消息中写道新闻:xg ******************* @ nwrddc02.gnilink.net ... "Roger" <gn****@hotmail.com> wrote in message news:xg*******************@nwrddc02.gnilink.net...谁能告诉我这两个有什么区别? 类nodeType {int int data; nodeType * link; }; 这是第一个: main() {node> * test; test-> data = 10; cout<< nodeType->数据; // 10 } 这是第二个: main() {node> * node = new nodeType; test-> data = 10; cout<<测试 - >数据; // 10 我有点困惑,因为第一个是由我的导师给出的。在第一种情况下如何分配内存?有没有使用动态内存分配的好处或缺点? 提前致谢。Can anyone tell me what is the difference between these two?class nodeType{ int data; nodeType *link;};This is the first one:main(){ nodeType *test; test->data=10; cout << nodeType->data; // 10}and this is the second one:main(){ nodeType *test=new nodeType; test->data=10; cout << test->data; // 10}I am kind of confused because the first one was given by my instructor.How is the memory allocated in the first situation?? Is there anybenefit or drawback of not using dynamic memory allocation??Thanks in advance. 好​​吧,看起来你的导师做了一个在第一种情况下犯错!直到指针指向某个东西,它才能被*使用! Well, looks like your instructor made a blunder in the first case! Until a pointer points at something, it *can''t* be used! 起初我也是这么想的。但是在我编译了 程序后,没有发现任何错误,并且成功打印出来。 我想知道为什么会这样???At first I also thought about that way. But after I compiled theprogram, no error was found and the reasult print out successfully.I am wondering why this happened?? 2003年10月20日星期一21:16:46 -0700,Dave其中p是*********** @ yahoo.com>写道:On Mon, 20 Oct 2003 21:16:46 -0700, "Dave" <be***********@yahoo.com> wrote: 罗杰 < GN **** @ hotmail.com>在消息中写道新闻:xg ******************* @ nwrddc02.gnilink.net ..。"Roger" <gn****@hotmail.com> wrote in messagenews:xg*******************@nwrddc02.gnilink.net.. .谁能告诉我这两个有什么区别? 类nodeType {int int data; nodeType * link; }; main() {node> * test; test-> data = 10; cout< ;< nodeType->数据; // 10 } 这是第二个: main() {node> * node = new nodeType; test-> data = 10; cout<<测试 - >数据; // 10 我有点困惑,因为第一个是由我的导师给出的。在第一种情况下如何分配内存?有没有使用动态内存分配的好处或缺点? 提前致谢。 Can anyone tell me what is the difference between these two? class nodeType { int data; nodeType *link; }; This is the first one: main() { nodeType *test; test->data=10; cout << nodeType->data; // 10 } and this is the second one: main() { nodeType *test=new nodeType; test->data=10; cout << test->data; // 10 } I am kind of confused because the first one was given by my instructor. How is the memory allocated in the first situation?? Is there any benefit or drawback of not using dynamic memory allocation?? Thanks in advance. 好吧,看起来你的导师做了一个在第一种情况下犯错!直到指针指向某个东西,它才能被*使用!Well, looks like your instructor made a blunder in the first case! Until apointer points at something, it *can''t* be used! 他或她至少犯了五个错误,_assuming_代码是不是 意在说明以下缺陷: 1)该课程没有任何内容可供访问。 2)非法声明''main''。 3)忘记分配一个对象。 4)忘记包含< iostream> 5)忘记在最后输出逻辑换行符。 但我们怎么知道这个问题不是伪装的作业,找5 以下代码中的缺陷?He or she made at least five blunders, _assuming_ the code was notmeant to illustrate the following defects:1) There''s nothing accessible in that class.2) Illegal declaration of ''main''.3) Forgetting to allocate an object.4) Forgetting to include <iostream>5) Forgetting to output a logical newline at the end.But how do we know this question isn''t homework in disguise, "find 5defects in the following code"? 这篇关于课堂提问 - 请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 04:13