I noticed that there are two ways to create C++ objects: BTree *btree = new BTree; and BTree btree; From what I can tell, the only difference is in how class objects are accessed (. vs. -> operator), and when the first way is used, private integers get initialized to 0. Which way is better, and what's the difference? How do you know when to use one or the other? Two differences:
我注意到有两种方法来创建C ++对象: BTree *btree = new BTree; 和 BTree btree; 从我所知道的,唯一的区别在于如何访问类对象(.vs - > - operator),并且当使用第一种方法时,私有整数被初始化为0。 哪种方式更好,有什么区别? 你怎么知道什么时候使用其中一种? 两个区别: 他们在内存的不同部分创建对象(堆vs堆栈) 对象的生命周期是不同的:在第一种情况下,代码显式地管理内存分配, 并且它还必须
Possible Duplicate: Why would you ever want to allocate memory on the heap rather than the stack? Test2 *t2 = new Test2(); t2->test(); Test2 t3; t3.test(); Why would I want to create a pointer object of type Test2? Why not just do non pointer version of Test2? Why would I want to do pointer objects? Found answer here: Why would you ever want to allocate memory on the heap rather tha
可能重复: 为什么你会想要在堆上分配内存而不是堆栈? Test2 *t2 = new Test2(); t2->test(); Test2 t3; t3.test(); 为什么我要创建一个Test2类型的指针对象? 为什么不只是做Test2的非指针版本? 为什么我想要做指针对象? 在这里找到答案: 为什么你会想要在堆上分配内存而不是堆栈? 什么时候最好使用栈而不是堆,反之亦然? 何时使用“新”,何时不使用C ++? 我应该什么时候在C ++中使用新的关键字?
What is the difference between this: Myclass *object = new Myclass(); and Myclass object = new Myclass(); I have seen that a lot of C++ libraries like wxWidgets, OGRE etc use the first method... Why? The second is wrong ! You may use MyClass object; That will work. Now, concerning how to choose between these two possibilities, it mainly depends on how long your object should live. Se
这有什么区别: Myclass *object = new Myclass(); 和 Myclass object = new Myclass(); 我看到很多C ++库像wxWidgets,OGRE等使用第一种方法...为什么? 第二个错误! 你可以使用 MyClass object; 那可行。 现在,关于如何在这两种可能性之间做出选择,主要取决于对象应该生存多久。 看到这里有一个彻底的答案。 Myclass *object = new Myclass(); //object is on the heap Myclass object; //object is on the st
Possible Duplicate: Stack,Static and Heap in C++ Hi guys, I am currently preparing for interviews, and quite often i see questions like, where are "static" variables or where are "local"/"global" variables stored and im totally puzzled. I'm aware of two kinds of memory: Stack and Heap. Other than this, are there any other types of memory, where different
可能重复: 堆栈,静态和堆在C ++中 嗨,大家好, 我目前正在准备采访,很多时候我会看到像“静态”变量或“本地”/“全局”变量存储在哪里以及我完全困惑的问题。 我知道两种内存:堆栈和堆。 除此之外,是否还有其他类型的内存,其中存储了不同类型的变量。 在一些地方,我甚至读了一些关于“数据段”的内容,但我不确定这些东西是如何映射到彼此的。 任何人都可以提供任何链接/解释给不同类型的内存,以及所有不同类型的变
Possible Duplicates: How is heap and stack memories mananged, implemented, allocated? Stack,Static and Heap in C++ In C/C++ we can store variables, functions, member functions, instances of a class either on a stack or a heap. How is each implemented? How is it managed (high level)? Does gcc preallocates a chunk of memory to be used for the stack and heap, and then doles out on request?
可能重复: 堆和堆栈的回忆是如何处理,实施和分配的? 堆栈,静态和堆在C ++中 在C / C ++中,我们可以将变量,函数,成员函数,类的实例存储在堆栈或堆中。 各自如何实施? 它如何管理(高级别)? gcc是否预先分配了一堆用于栈和堆的内存,然后根据请求进行分配? 原始内存来自RAM吗? 函数可以分配在堆而不是堆栈上吗? --Clarification-- 我真的在询问关于堆栈和堆栈内存的实现和管理。 在阅
In C++, is there any difference between: struct Foo { ... }; and typedef struct { ... } Foo; In C++, there is only a subtle difference. It's a holdover from C, in which it makes a difference. The C language standard (C89 §3.1.2.3, C99 §6.2.3, and C11 §6.2.3) mandates separate namespaces for different categories of identifiers, including tag identifiers (for struct / union / enum ) and
在C ++中,是否有任何区别: struct Foo { ... }; 和 typedef struct { ... } Foo; 在C ++中,只有一个微妙的区别。 这是来自C的一个延期,它在这方面有所作为。 C语言标准(C89§3.1.2.3,C99§6.2.3和C11§6.2.3)为不同类别的标识符分别命名空间,包括标签标识符(用于struct / union / enum )和普通标识符(用于typedef和其他标识符)。 如果你只是说: struct Foo { ... }; Foo x; 你会得到一个编译器错误,因为Fo
This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start with an obvious difference: If you don't specify public: or private: , members of a struct are public by default; members of a cla
这个问题已经在C#/ .Net的上下文中提出过了。 现在我想了解C ++中结构和类之间的区别。 请讨论技术差异以及OO设计中选择其中一个的原因。 我将从一个明显的区别开始: 如果您不指定public:或private: :,则默认情况下,结构的成员是公共的; 一个班级的成员默认是私人的。 我确信在C ++规范的晦涩的角落还有其他的不同之处。 你忘记了类和结构之间棘手的第二个区别。 定义标准(C ++ 98到C ++ 11中的第11.2.2节)
In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful? It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of compositio
在C ++中,我想不出一个我想从基类继承private / protected的情况: class Base; class Derived1 : private Base; class Derived2 : protected Base; 它真的有用吗? 当你想访问基类的一些成员,但没有将它们暴露在你的类接口中时,这很有用。 私有继承也可以被看作是某种组合:C ++ faq-lite给出了下面的例子来说明这个语句 class Engine { public: Engine(int numCylinders); void start(); // St
This question already has an answer here: What is the difference between new/delete and malloc/free? 15 answers The compiler will translate a call to a member function into a call to a static function, with an implied this parameter pointing to the object. That means the contents of the object, valid or invalid, are not relevant to the whether the call is made. This changes if the method
这个问题在这里已经有了答案: new / delete和malloc / free有什么区别? 15个答案 编译器会将对成员函数的调用转换为对静态函数的调用,并暗示this参数指向该对象。 这意味着对象的内容(有效或无效)与是否进行呼叫无关。 如果该方法是虚拟的,这将改变,因为vtable指针必须是有效的。 构造函数将初始化vtable指针。 malloc不会调用构造函数,它甚至不知道构造函数是什么 - 它是C的剩余行李。 请注意,这不是标准
This question already has an answer here: What is the difference between new/delete and malloc/free? 15 answers malloc and free are purely memory-management functions, they don't know anything about classes (and existed long before C++). They're low-level memory manipulation. C++ added classes to C, and as part of that process, added new and delete to create and destroy instances
这个问题在这里已经有了答案: new / delete和malloc / free有什么区别? 15个答案 malloc和free是纯粹的内存管理功能,他们不知道类的任何内容(并且早在C ++之前就已存在)。 他们是低级别的内存操作。 C ++向C添加了类,并且作为该过程的一部分,添加了new和delete来创建和销毁类的实例。 这与低级内存管理不同。 malloc是一个C函数,它在构造函数之前进行日期。 free是一个C函数,它在日期解析器之前。 它们都