c++-faq

What C++ idioms should C++ programmers use? [closed]

Deadly 提交于 2019-11-27 09:01:35
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What C++ idioms should C++ programmers know? By C++ idioms, I mean design patterns or way of doing certain things that are only

Which C++ idioms are deprecated in C++11?

家住魔仙堡 提交于 2019-11-27 08:54:37
问题 With the new standard, there are new ways of doing things, and many are nicer than the old ways, but the old way is still fine. It's also clear that the new standard doesn't officially deprecate very much, for backward compatibility reasons. So the question that remains is: What old ways of coding are definitely inferior to C++11 styles, and what can we now do instead? In answering this, you may skip the obvious things like "use auto variables". 回答1: Final Class: C++11 provides the final

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

本小妞迷上赌 提交于 2019-11-27 05:58:01
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.) Jonathan Wakely 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 platforms) might not be "location invariant" meaning the object's address is part of its value. For

Is it possible to prevent stack allocation of an object and only allow it to be instantiated with 'new'?

孤街浪徒 提交于 2019-11-27 04:10:02
Is it possible to prevent stack allocation of an object and only allow it to be instiated with 'new' on the heap? 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&); }; In the case of C++11 class Foo { public: ~Foo(); static Foo* createFoo() { return new Foo(); } Foo(const Foo &) = delete; // if needed, put as private Foo & operator=(const Foo &) = delete; /

What's the meaning of * and & when applied to variable names?

纵然是瞬间 提交于 2019-11-27 03:42:16
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. sbi 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 variable's type to be a reference type : int& . The same applies to * : When applied as a unary prefix

FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

怎甘沉沦 提交于 2019-11-26 22:41:19
This does not compile in C++: class A { }; class B : public A { }; ... A *a = new B(); B *b = dynamic_cast<B*>(a); John Dibling Because dynamic_cast can only downcast polymorphic types, so sayeth the Standard. You can make your class polymoprphic by adding a virtual destructor to the base class. In fact, you probably should anyway (See Footnote). Else if you try to delete a B object through an A pointer, you'll evoke Undefined Behavior . class A { public: virtual ~A() {}; }; et voila! Footnote There are exceptions to the "rule" about needing a virtual destructor in polymorphic types. One such

Rationale of enforcing some operators to be members

你。 提交于 2019-11-26 22:39:08
There are 4 operators in C++ which can be overloaded but cannot be overloaded as freestanding (aka nonmember, standalone) functions. These operators are: operator = operator () operator -> operator [] This thread explains perfectly well the rationale behind prohibiting operator = to be a nonmember function. Any ideas about the other three? The four operators mentioned in the original posting, = , () , -> and [] , must indeed be implemented as non-static member functions (by respectively C++98 §13.5.3/1, §13.5.4/1, §13.5.5/1 and §13.5.6/1). Bjarne Stroustrup's rationale was, as I recall from

How do I prevent a class from being allocated via the 'new' operator? (I'd like to ensure my RAII class is always allocated on the stack.)

大城市里の小女人 提交于 2019-11-26 22:10:28
I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from being allocated via the 'new' operator? Kevin All you need to do is declare the class' new operator private: class X { private: // Prevent heap allocation void * operator new (size_t); void * operator new[] (size_t); void operator delete (void *); void operator delete[] (void*); // ... // The rest of the implementation for X // ... }; Making 'operator new' private effectively prevents code outside the class from using 'new' to create an instance of X. To complete things, you should hide 'operator

Explain C++ SFINAE to a non-C++ programmer

微笑、不失礼 提交于 2019-11-26 21:25:01
What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? Jerry Coffin Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck typing -- for example, when you invoke a function, you can pass an object X to that function as

Passing shared pointers as arguments

匆匆过客 提交于 2019-11-26 21:23:47
If I declare an object wrapped in a shared pointer: std::shared_ptr<myClass> myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //the called method void DoSomething(std::shared_ptr<myClass> arg1) { arg1->someField = 4; } Does the above simply increment the shared_pt's reference count and everything is cool? Or does it leave a dangling pointer? Are you still supposed to do this?: DoSomething(myClassObject.Get()); void DoSomething(std::shared_ptr<myClass>* arg1) { (*arg1)->someField = 4; } I think that the 2nd way may be more efficient