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 is it wrong to use std::auto_ptr<> with STL containers? ...
Why does the 'sizeof' operator return a size larger for a structure than the total sizes of the structure's members? ...
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 ...
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' on the heap? ...
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? What is the RAII idiom? What is the difference between C++'s RAII idiom and C#'s 'using' statement? ...
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...
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...
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 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; }; ...
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(...
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? ...
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:...
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! ...
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. ...
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...
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...
What is the difference between these three? ...
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 ...