c++-faq

Proper stack and heap usage in C++?

青春壹個敷衍的年華 提交于 2019-11-26 00:45:32
问题 I\'ve been programming for a while but It\'s been mostly Java and C#. I\'ve never actually had to manage memory on my own. I recently began programming in C++ and I\'m a little confused as to when I should store things on the stack and when to store them on the heap. My understanding is that variables which are accessed very frequently should be stored on the stack and objects, rarely used variables, and large data structures should all be stored on the heap. Is this correct or am I incorrect

Constructor initialization-list evaluation order

落花浮王杯 提交于 2019-11-26 00:39:38
问题 I have a constructor that takes some arguments. I had assumed that they were constructed in the order listed, but in one case it appears they were being constructed in reverse resulting in an abort. When I reversed the arguments the program stopped aborting. This is an example of the syntax I\'m using. The thing is, a_ needs to be initialized before b_ in this case. Can you guarantee the order of construction? e.g. class A { public: A(OtherClass o, string x, int y) : a_(o), b_(a_, x, y) { }

What is the point of function pointers?

我是研究僧i 提交于 2019-11-26 00:32:39
问题 I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can\'t think of a case where it\'s better or unavoidable to use a function pointer. Could you give some example of good use of function pointers (in C or C++)? 回答1: Most examples boil down to callbacks : You call a function f() passing the address of another function g() , and f() calls g() for some specific task. If you pass f() the address of h() instead, then f()

Lifetime of temporaries

喜欢而已 提交于 2019-11-26 00:25:51
问题 The following code works fine, but why is this correct code? Why is the \"c_str()\" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn\'t seem to be like this. So, now I assume that the temporary returned by foo() will be destroyed after the call to bar() - is this correct? And why? std::string foo() { std::string out = something...; return out; } void bar( const char* ccp ) { // do something with the

How do you serialize an object in C++?

帅比萌擦擦* 提交于 2019-11-26 00:17:53
问题 I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)? Are there any C++ serialization online code samples or tutorials? EDIT: Just to be clear, I\'m looking for methods on converting an object into an array of bytes, then back into an object. I can handle the socket transmission. 回答1: Talking about

Can a class member function template be virtual?

可紊 提交于 2019-11-25 23:59:35
问题 I have heard that C++ class member function templates can\'t be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function? 回答1: Templates are all about the compiler generating code at compile-time . Virtual functions are all about the run-time system figuring out which function to call at run-time . Once the run-time system figured out it would need to call a templatized virtual function, compilation is all done and the compiler

Difference between private, public, and protected inheritance

谁都会走 提交于 2019-11-25 23:57:12
问题 What is the difference between public , private , and protected inheritance in C++? All of the questions I\'ve found on SO deal with specific cases. 回答1: To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:". There are three accessors that I'm aware of: public , protected and private . Let: class Base { public: int publicMember; protected: int protectedMember; private: int privateMember; }; Everything that

What is this weird colon-member (“ : ”) syntax in the constructor?

半世苍凉 提交于 2019-11-25 23:56:02
问题 Recently I\'ve seen an example like the following: #include <iostream> class Foo { public: int bar; Foo(int num): bar(num) {}; }; int main(void) { std::cout << Foo(42).bar << std::endl; return 0; } What does this strange : bar(num) mean? It somehow seems to initialize the member variable but I\'ve never seen this syntax before. It looks like a function/constructor call but for an int ? Makes no sense for me. Perhaps someone could enlighten me. And, by the way, are there any other esoteric

Why isn&#39;t sizeof for a struct equal to the sum of sizeof of each member?

痴心易碎 提交于 2019-11-25 23:55:30
问题 Why does the sizeof operator return a size larger for a structure than the total sizes of the structure\'s members? 回答1: This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS ). Mis-aligned access might be a soft error. Either corrected in hardware, for a modest performance-degradation. Or corrected by emulation in software, for a severe

What is the correct answer for cout << a++ << a;?

。_饼干妹妹 提交于 2019-11-25 23:54:07
问题 Recently in an interview there was a following objective type question. int a = 0; cout << a++ << a; Answers: a. 10 b. 01 c. undefined behavior I answered choice b, i.e. output would be \"01\". But to my surprise later I was told by an interviewer that the correct answer is option c: undefined. Now, I do know the concept of sequence points in C++. The behavior is undefined for the following statement: int i = 0; i += i++ + i++; but as per my understanding for the statement cout << a++ << a ,