# ----------------------------------------------------------------------------
I was getting an error that looked something like:
expected unqualified-id before '(' token
The files referenced were system include files and all contained a
reference to std::max. It turns out one of my own header files defined
a max macro that didn't jive with the reference in the system header
files. A temporary workaround was to insert this after including the
offending header file:
#undef max
# ----------------------------------------------------------------------------
To accommodate polymorphism with STL containers like vector, you cannot
declare the vectors to hold instances of the base class:
std::vector my_list;
Any classes derived from Base and stored in this vector will lose
information when they are packed in to the vector. This is called
slicing. We really want to declare the vector to hold pointers to Base,
like so:
std::vector my_list;
All elements, both of Base and derived classes, are fully retained when
added to the vector. If we need to know the type of an element, we can
use dynamic_cast, or create some id associated with each class.
# ----------------------------------------------------------------------------
The keyword const after a function declaration means it won't change any
class or instance variables. In this example, myFunc() can't touch
my_data.
class MyClass {
public:
int myFunc() const;
private:
float my_data;
}
# ----------------------------------------------------------------------------
Constructors can't call other constructors for the same class to effect
a change on this.
For instance, in the following code, the second constructor just creates
an anonymous instance of MyClass and throws it away. The object it's
called on is unaffected.
MyClass() {
}
MyClass(int x) {
MyClass();
}
To support this kind of thing, an outside helper function has to be
created.
# ----------------------------------------------------------------------------
To use C libraries and object code code in C++, surround your C header with:
#ifdef __cplusplus
extern "C" {
#endif
// func declarations
#ifdef __cplusplus
}
#endif
This forces the C++ compiler to not mangle the function names, as it is
wont. Alternatively, just compile your C code with the C++ compiler.
# ----------------------------------------------------------------------------
A templated copy constructor taking const reference is of lower priority
than an implicit copy constructor. If I remove the const from the code
below, the templated constructor will get called.
#include
template
class Object {
public:
Object() : val(0) {}
Object(T val) : val(val) {}
template Object(const Object& src) {
std::cout << "casting" << std::endl;
val = (T) src.val;
}
T val;
};
int main(int argc, char **argv) {
Object a(3.5);
std::cout << "a.val: " << a.val << std::endl;
Object b(a);
std::cout << "b.val: " << b.val << std::endl;
Object c(a);
std::cout << "c.val: " << c.val << std::endl;
return 0;
}
# ----------------------------------------------------------------------------