c++-faq

Why does volatile exist?

浪子不回头ぞ 提交于 2019-11-26 11:57:29
What does the volatile keyword do? In C++ what problem does it solve? In my case, I have never knowingly needed it. Doug T. volatile is needed if you are reading from a spot in memory that, say, a completely separate process/device/whatever may write to. I used to work with dual-port ram in a multiprocessor system in straight C. We used a hardware managed 16 bit value as a semaphore to know when the other guy was done. Essentially we did this: void waitForSemaphore() { volatile uint16_t* semPtr = WELL_KNOWN_SEM_ADDR;/*well known address to my semaphore*/ while ((*semPtr) != IS_OK_FOR_ME_TO

Why do I see strange values when I print uninitialized variables?

纵饮孤独 提交于 2019-11-26 11:53:40
In the following code, the variable has no initial value and printed this variable. int var; cout << var << endl; output : 2514932 double var; cout << var << endl; output : 1.23769e-307 I don't understand these output numbers. Can any one explain this to me? GManNickG Put simply, var is not initialized and reading an uninitialized variable leads to undefined behavior . So don't do it. The moment you do, your program is no longer guaranteed to do anything you say. Formally, "reading" a value means performing an lvalue-to-rvalue conversion on it. And §4.1 states "...if the object is

When to make a type non-movable in C++11?

久未见 提交于 2019-11-26 11:48:25
问题 I was surprised this didn\'t show up in my search results, I thought someone would\'ve asked this before, given the usefulness of move semantics in C++11: When do I have to (or is it a good idea for me to) make a class non-movable in C++11? (Reasons other than compatibility issues with existing code, that is.) 回答1: Herb's answer (before it was edited) actually gave a good example of a type which shouldn't be movable: std::mutex . The OS's native mutex type (e.g. pthread_mutex_t on POSIX

Copy constructor and = operator overload in C++: is a common function possible?

China☆狼群 提交于 2019-11-26 11:31:36
Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? CB Bailey Yes. There are two common options. One - which is generally discouraged - is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also

What are the advantages of using nullptr?

不羁的心 提交于 2019-11-26 11:31:21
This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0 ? In that code, there doesn't seem to be an advantage. But consider the following overloaded functions: void f(char const *ptr); void f(int v); f(NULL); //which function will be called? Which function will be called? Of course, the intention here is to call f(char const *) , but in reality f(int) will be called! That is a big problem 1 , isn

Why are C++ inline functions in the header?

筅森魡賤 提交于 2019-11-26 11:31:05
NB This is not a question about how to use inline functions or how they work, more why they are done the way they are. The declaration of a class member function does not need to define a function as inline , it is only the actual implementation of the function. For example, in the header file: struct foo{ void bar(); // no need to define this as inline } So why does the inline implementation of a classes function have to be in the header file? Why can't I put the inline function the .cpp file? If I where to try to put the inline definition in the .cpp file I would get an error along the lines

Overloading member access operators ->, .* (C++)

佐手、 提交于 2019-11-26 11:03:19
I understand most operator overloading, with the exception of the member access operators -> , .* , ->* etc. In particular, what is passed to these operator functions, and what should be returned? How does the operator function (e.g. operator->(...) ) know what member is being refered to? Can it know? Does it even need to know? Finally, are there any const considerations that need to be taken into account? For example, when overloading something like operator[] , generally you will need both a const and non-const version. Do member access operators require const and non-const versions?

Is it possible to prevent stack allocation of an object and only allow it to be instantiated with &#39;new&#39;?

喜欢而已 提交于 2019-11-26 11:00:12
问题 Is it possible to prevent stack allocation of an object and only allow it to be instiated with \'new\' on the heap? 回答1: One way you could do this would be to make the constructors private and only allow construction through a static method that returns a pointer. For example: class Foo { public: ~Foo(); static Foo* createFoo() { return new Foo(); } private: Foo(); Foo(const Foo&); Foo& operator=(const Foo&); }; 回答2: In the case of C++11 class Foo { public: ~Foo(); static Foo* createFoo() {

What&#39;s the meaning of * and & when applied to variable names?

淺唱寂寞╮ 提交于 2019-11-26 10:36:21
问题 In C++, what is the difference between: void func(MyType&); // declaration //... MyType * ptr; func(*ptr); // compiler doesnt give error func(ptr); // compiler gives error i thought & represents memory address so // this statement should correct as ptr is only a pointer // or address of some real var. 回答1: The unary prefix operator & , when applied to an object, yields the address of the object: &obj . The type modifier & , when applied to a variable about to be declared, will modify the

Object destruction in C++

爷,独闯天下 提交于 2019-11-26 10:16:53
When exactly are objects destroyed in C++, and what does that mean? Do I have to destroy them manually, since there is no Garbage Collector? How do exceptions come into play? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ . If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom , where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.) fredoverflow In the