Q: What is the meaning of the 'Alloc' template parameter in 'vector'?
Q: How do I define my own allocator for use in STL containers?
A: The whole concept is very well explained in Gabriel and Andreas' article about Allocators.
The template parameter 'Alloc' specifies the type of allocator that the container should use to manage its memory. Every time the container needs a bit of memory, it will ask the allocator to retrieve it for him. The allocator usually just calls 'new' (and 'delete' when it has to free memory), but there can be more sophisticated strategies for allocators.
Usually, you are fine with the standard allocator provided by your STL. However, at times you may need to specify the allocation strategy explicitly. In theory, writing a custom allocator is not very difficult, but the task is complicated by the fact that the standard C++ design relies on template member functions which quite a few compilers don't support (e.g. Visual C++ 6).
A simple example for an allocator which never frees memory is this code (note that you need a recent compiler to get this to work):
Q: What is the best way to remove all elements of a particular value from a container?
A: Use the so-called 'remove/erase' idiom:
This is because of the simple fact that 'remove()' is a generic algorithm which takes iterators as its arguments. The iterators know nothing of their container; the STL was designed so as to grant the smallest possible amount of coupling. Thus, the iterators cannot call any container's 'erase()' or similar function.
'remove()' DOES return the container's new 'end()' (which just
happens to be the first element that was removed) and this value
can be used in the container's 'erase()' call.
Note: Most of the material in this article is taken from codeguru.com
Q: How to convert from std::string to CString?
A: This is simple... Use the CString constructor.
Let me add that CStringT can construct from both character or wide-character strings. i.e. It can convert from char* (i.e. LPSTR) or from wchar_t* (LPWSTR).
In other words, char-specialization (of CStringT) i.e. CStringA, wchar_t-specilization CStringW, and TCHAR-specialization CString can be constructed from either char or wide-character, null terminated (null-termination is very important here) string sources.
Q: How to convert from CString to std::string?
A: There are really many ways to do it.
But, the simplest one is just this -
CString strSomeCstring ("This is a CString Object");
// Use ANSI variant CStringA to convert to char*; construct from it -
std::string strStdString (CStringA (strSomeCstring));
CStringA is a template specialization of class CStringT for type char avaílable with Visual Studio 7.x and better.
Q: What are correlations between the CString Family and the Standard String Family?
A: If we were to draw a correlation between the CString Family and Standard String Classes, we would notice -
However, the standard library itself doesn't contain a "CString" equivalent - one that can at best be -
This makes CString (VS 7.x +) "cool".
Q: The history of CString...
A: CString really wasn't always as likeable as it is today. If one were to compare the CString of Visual Studio 7.x and better with the class supplied by MSVC 6.0, one might not be wrong in judging the erstwhile version as relatively inflexible, and restrictive.
What made CString of yesterday "inflexible"?
1.Only for MFC-folks!
Yes... CString with MSVC 6.0 was notoriously connected to MFC Projects as if other project types didn't have any need for a TCHAR String Wrapper Class! One reason for it would be the linker error one would get for using this class in a non-MFC Project.
2. Need to use a CString? Convert to TCHAR!
CString is a string wrapper for TCHAR strings. In doing so, it came with a good number of helpful member methods. However, the implementation of this class in MSVC 6.0 was such that if a programmer needed to make use of the utility member methods, he would need to convert his string data type to TCHAR - back and forth.
This "restriction" automatically excluded programmers using ANSI strings (char*) in UNICODE builds from using this class, and ditto for those using UNICODE strings (WCHAR*) in non-UNICODE builds from using this class - as in both these cases, the strings in question aren't TCHAR strings - the type that CString wraps.
What makes CString of today "flexible" and actually more "likeable"?
1. Use it in any project type - even in a console application!
Yes... All the programmer needs to do is to include the following header -
2. Don't change your String Type to suit CString! Choose the right CString instead!
With Visual Studio 7.x and better, CString is actually a specialization of template Class CStringT for type T as TCHAR.
Additionally, there exist two useful specializations of CStringT -
Note: Most of the material in this article is taken from codeguru.com
Q: What is the difference between '\n' and '\r\n'?
A:
Background
There are a few characters which can indicate a new line. The usual ones are these two:
This difference gives rise to a number of problems. For example, a file created under Unix (so with newlines as a single LF) will not open correctly under Window's Notepad. Any Windows program that expects newlines to be CRLF will not work correctly with these files.
To unify things a bit, so that writing portable C/C++ programs is possible, file streams have both a "translated" and an "untranslated" mode. If you open a file in translated mode, the runtime library will convert a '\n' to the appropriate newline character(s). If the following program is compiled under Unix, the file will contain a single LF to indicate the newline. If it's compiled under windows, it will contain a CRLF.
If you look at the generated file with a hex editor, you will see that the windows version has generated the following:
H e l l o W o r l d CR LF
0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x6F 0x72 0x6C 0x64 0x0D 0x0A
So file streams are handled in a transparent way, provided of course that you only handle files compatible with your operating system. But many times you have to pass multi-line strings directly to some system functions.
In practice
In Windows you have to pass multi-line strings with '\r\n', otherwise the system functions don't recognize them correctly as multi-line. This is true e.g. for setting the text of Edit controls, Labels, Windows etc. Also, when you read multi-line text from a file that initially contains '\r\n' in translated mode, the string in memory will contain only a single '\n'. See for example the documentation on MSDN about 'fread()':
Q: What types of strings are there?
A:
Note: Most of the material in this article is taken from codeguru.com
Q: What is a singleton class?
A: A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.
Q: Can you give me an example, where it is used?
A: The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:
// Other non-static member functions
private:
CMySingleton() {}; // Private constructor
CMySingleton(const CMySingleton&); // Prevent copy-construction
CMySingleton& operator=(const CMySingleton&); // Prevent assignment
};
Q: Can I extend the singleton pattern to allow more than one instance?
A: The general purpose of the singleton design pattern is to limit the number of instances of a class to only one. However, the pattern can be extended by many ways to actually control the number of instances allowed. One way is shown below...
static int nCount; // Current number of instances
static int nMaxInstance; // Maximum number of instances
public:
~CMyClass(); // Public Destructor
static CMyClass* CreateInstance(); // Construct Indirectly
//Add whatever members you want
};
Now if you want to create an instance of the class:
Note, that the successfully created instance(s) need(s) to be released to ensure that no memory leak occurs:
Note: Most of the material in this article is taken from codeguru.com
Q: I have the following code:
template <typename T>
T foo(const T& t);
// end of foo.h
//------------------------------------------------
// In foo.cpp
template <typename T>
T foo(const T& t)
{
// function body
}
// end of foo.cpp
//------------------------------------------------
// In main.cpp
#include "foo.h"
int main()
{
int i = 1;
int j = foo(i);
}
But when I compile it, the linker tells me that "foo" (in whatever way the complier mangles foo<int>) is an unresolved external symbol.
A: There are actually two solutions to this problem - the "correct" one and the one that works. But before getting to that we need to understand why there is a problem.
The thing to understand about template code is that it's, well, a template. It's not real code. It's like when you have a letter template in your word processor: it's not a real letter until you fill in details like the person it's addressed to. In the example given, the compiler can't compile the definition of foo in "foo.cpp" because it doesn't know what T is. Until it knows that, it can't decide whether or not the operation you perform using T objects are valid or not. For example foo might call a specific member function of T called, say, bar(). But T is only a placeholder. Until you call foo with a specific type, how can the compiler know whether or not the code is valid?
So it comes down to this: templated functions (and classes) don't actually exist until you use them. And in order for the compiler to generate the real code for the function (or class), it has to have the complete definition of the template available at the point of use. If the full definition is not available, the compiler will assume that it's been defined somewhere else and just plant code for the call. And here's the problem with the example above: at the time foo is used (in main()), the compiler only has the declaration of foo available, so it assumes that foo<int> (because that's the instantiation that's wanted) is defined elsewhere. But the definition is in foo.cpp, and there is no use of foo<int> there, so the compiler won't bother generating it. And so we get to the situation that compiling main() plants a call to foo<int>, but compiling foo.cpp produces nothing. And that leads to an unresolved external reference.
The "correct" solution.
I call this correct, because it keeps the example code the way it is laid out. The standard defines a keyword export that is designed for just this situation. By defining foo in foo.cpp with export:
The definition becomes available to the compiler whilst it is comping main.cpp, and so it now generates the code for foo<int> and the external is resolved.
The solution that works.
Unfortunately, not many compilers support export, so the "correct" solution is more likely to produce a compilation error that it is to solve the problem. So the practical solution is define the function in the header file as an inline function:
This way, because it's in the header file, the compiler can see it at the point of use and can generate the appropriate code and the external is resolved.
For those who think that's a bit ugly, and would like a solution that's half-way to the "correct" one (for that day when their compiler supports export), put the inine definition into a file called "foo.inl" and #include "foo.inl" at the end of the original foo.h. Then, when you get your new compiler, rename foo.inl to foo.cpp, change inline to export and remove the #include from foo.h.
Note: Most of the material in this article is taken from codeguru.com
Q: How to use class member functions as callbacks?
A: The problem is that every callback function has its own prototype, which determines the parameters that gets passed from the operating system to it.
In C++ every member function has a hidden parameter - the so-called 'this' pointer which will be automatically passed to the function. C++ is able to associate a function with a particular instance of an object by means of the 'this' pointer. Member functions access member variables through the 'this' pointer...
private:
int integer_;
};
If you compile this code it will be compiled as
private:
int integer_;
};
The operating system does not call callback functions through objects therefore it cannot handle the automatically added 'this' pointer... To get a member functions working as a callback routine you need to tell the compiler explicitly not to expect a 'this' pointer. To avoid the automatic 'this' pointer you have two possibilities:
Non-member functions are not part of a class and therefore do not have a 'this' pointer. Static member functions do not receive a 'this' pointer either...thus, if you want to use a member function as a callback routine you need to declare it as 'static'...
Note: Most of the material in this article is taken from codeguru.com
Q: What are the differences between constant objects?
A: To make any object constant, one uses the keyword 'const'. However, depending on the actual place you insert this keyword, it can have different meanings for the associated object:
Note: Most of the material in this article is taken from codeguru.com
Q: Someone told me that if I want to increment a variable and that's all, I should use ++x instead of x++. Is this true?
A: If incrementing is all that you want to do (you are not assigning the variable to something else), then yes this is a good practice, especially if you are using the increment operator on a class.
++x;
For the standard data types there is usually no performance difference, but for classes there are! The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference. The following code exemplifies this:
MyClass operator++(int) //Postfix increment operator (x++)
{
MyClass temp = *this;
//Perform increment operation
return temp;
}
};
Q: So what form of increment/decrement operator should I use for fundamental types when I can use either. For example:
A: Use the pre-incement/decrement form (++i and --i)
Even though there is little impact for fundamental types, since we programmers are human, you will produce programming habits. And those habits will cary over to when we deal with classes instead, for example iterators.
There was an informal study done (sorry I forgot the source) on a number of people who say "I only use post-incrementing/decrementing for fundamental types." The results showed that they very frequently used the post-increment/decrementing operators for iterators and other classes leading to a great amount of wasted CPU cycles in the end code.
Note: Most of the material in this article is taken from codeguru.com