More effective Flashcards
When to use pointers and when references?
Pointers: null is possible and legit, pointee changes.
References: always not null, refers to the same object.
Benefits of using C++ style casts?
- clear stand out in the code, can be found easily by human or tools.
- easy diagnostic of cast problems.
Passing polymorphic objects by value…
Don’t pass polymorphic objects by value. Never store them in arrays.
Why avoid default constructors if you don’t need them?
If an object makes no sense without initializing it with some values the class should not have the default ctor.
Problems for objects of classes without the default ctor?
- Creation of arrays.
- Cannot be used with many STL containers.
- Virtual base class w/o the def. ctor is the pain in the : most derived class must provide arguments for the virtual base class ctor.
What functions allow perform implicit type conversions of classes?
- Single-argument ctor (or multi-arg with defaults).
- Implicit type conversion operators, eg
operator double() const;
Usually don’t provide type conversion functions at all.
- Unexpected conversion to other type if you forget to provide the correct function (no error is reported but conversion is used implicitly).
- Mistypes (errors) can be hidden by using implicit type conversions.
How to prevent implicit type conversions?
- Use the “explicit” keyword.
2. Don’t implement the implicit type conversion operators.
If class A can be implicitly converted to class B and B can be converted to C will A be implicitly convertible to C?
No.
Why ++i is preferred over i++?
i++ needs to create a temp object to store the old value which makes it inefficient if the old value isn’t needed.
How do you define the ++i operator?
class Class{ Class& operator++(); };
How do you define the i++ operator?
class Class{ const Class operator++(int); };
What type should ++i operator return to be consistent with int?
A reference.
What type should i++ operator return to be consistent with int?
A const object.
What should i++++ return?
It shouldn’t compile: i++++ (i++)++ but i++ returns a const object and cannot be incremented.
How do you ensure that i++ and ++i operators do increment consistently?
Write i++ in terms of ++i. (It is also easier to maintain).
Why should you never overload operators &&, ||, and ,(comma)?
There are special rules for use of these operators with the built-in types: short circuit (&&, ||) and left-to-right evaluation (,). Overloading replaces the operators with function (method) calls which cannot reproduce the built-in behaviour. This would be a surprise for a programmer,
What is the new operator?
It’s a built-in operator which does two things:
1. Allocates enough memory using
void *operator new(size_t);
2. Calls a constructor to initialize an instance at the allocated memory.
What is the operator new?
It is the overloadable operator which allocates the memory:
void *operator new(size_t);
Can operator new have more than 1 argument?
It can. But the first one must have type size_t.
void *operator new(size_t a1, T2 a2, T3 a3);
To use:
void *raw = operator new(sizeof(T1), a2, a3);
or:
T1 *p = new(a2,a3) T1(…);
What is placement new?
It’s a version of operator new with 2 arguments: the second arg has type void*. It doesn’t allocate any memory only returns the value of its second argument:
void *operator new(size_t void *loc){
return loc;
}
What does delete operator do?
Calls the destructor then releases the memory:
T *p; … ; delete p;
T *p; … ; p->~T(); operator delete(p);
How do you declare operator delete for overloading?
void operator delete(void *loc);
Why exceptions are better than error codes?
Exceptions cannot be ignored.