c++-faq

Why is it wrong to use std::auto_ptr<> with STL containers?

Why is it wrong to use std::auto_ptr<> with STL containers? ...

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

Why does the 'sizeof' operator return a size larger for a structure than the total sizes of the structure's members? ...

How do I remove code duplication between similar const and non-const member functions?

Let's say I have the following class X where I want to return access to an internal member: class Z { // details }; class X { std::vector<Z> vecZ; public: Z& Z(size_t index) { // ... // massive amounts of code for // validating index Z& ret = vecZ[index]; // even more code for ...

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.)

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? ...

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

Is it possible to prevent stack allocation of an object and only allow it to be instiated with 'new' on the heap? ...

How to typedef a pointer to method which returns a pointer the method?

Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB(); ... }; The methods stateA() and stateB() should be able return pointers to stateA() and stateB(). How to typedef the StateMethod? ...

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

Does C++ support 'finally' blocks? What is the RAII idiom? What is the difference between C++'s RAII idiom and C#'s 'using' statement? ...

Proper replacement for the missing 'finally' in C++

Since there is no finally in C++ you have to use the RAII design pattern instead, if you want your code to be exception safe. One way to do this is by using the destructor of a local class like this: void foo() { struct Finally { ~Finally() { /* cleanup code */ } } finalizer(); // ...code that might throw an exceptio...

The Definitive C++ Book Guide and List

Provide QUALITY books and an approximate skill level. Add a short blurb/description about each book that you have personally read/benefited from. Feel free to debate quality, headings, etc. Books that meet the criteria will be added to the list. Books that have reviews by the Association of C and C++ Users (ACCU) have links to the revie...

Determine array size in constructor initializer

In the code below I would like array to be defined as an array of size x when the Class constructor is called. How can I do that? class Class { public: int array[]; Class(int x) : ??? { } } ...

What does a colon following a C++ constructor name do?

What does the colon operator (":") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0);? class MyClass { public: MyClass() : m_classID(-1), m_userdata(0) { } int m_classID; void *m_userdata; }; ...

How come a non-const reference cannot bind to a temporary object?

Why is it not allowed to get non-const reference to a temporary object, which function getx() returns? Clearly, this is prohibited by C++ Standard but I am interested in the purpose of such restriction, not a reference to the standard. struct X { X& ref() { return *this; } }; X getx() { return X();} void g(X & x) {} int f(...

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

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? ...

Adding an include guard breaks the build

I added #ifndef..#define..#endif to a file of my project and the compiler fails. As soon as I remove it or put any other name in the define it compiles fine. What could be the problem? Sounds like the file is already declared, but I do not know where. I'm fine just removing it, but I really want to know why this is happening. error:...

How might I overload the "new" operator to allocate memory from a secondary memory device?

I am looking for a syntax to allocate memory from a secondary memory device and not from the default heap. How can i implement it? Using malloc() would by default take it from heap... Surely there must be another way! ...

How can I avoid including class implementation files?

Instead of doing #include "MyClass.cpp" I would like to do #include "MyClass.h" I've read online that not doing so is considered bad practice. ...

How to pass objects to functions in C++?

I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to the objects. It would be great if yo...

Initializing members with members

This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C...

Undefined, unspecified and implementation-defined behavior

What is the difference between these three? ...

How to do static de-initialization if the destructor has side effects and the object is accessed from another static object's destructor?

There is a simple and well-known pattern to avoid the static initialization fiasco, described in section 10.13 of the C++ FAQ Lite. In this standard pattern, there is a trade-off made in that either the constructed object gets never destructed (which is not a problem if the destructor does not have important side effects) or the static ...