Home
61.
My class encapsulates a dynamic array of integers. What functions and operators should I implement for a minimum?

When programming such a class, you need to clearly define the behavior in the scenario where an instance is being copied directly into another via assignment or copied indirectly by being passed to a function by value. You typically implement the copy constructor, copy assignment operator, and the destructor. You also implement the move constructor and move assignment operator if you want to tweak the performance of this class in certain cases. To enable an array-like access to elements stored inside an instance of the class, you would want to overload the subscript operator[].

62.
I want to create my own smart pointer class. What functions and operators do I need to implement for a minimum?

A smart pointer needs to supply the ability of being used as a normal pointer as in *pSmartPtr or pSmartPtr->Func(). To enable this you implement operator (*) and operator (->). In addition, for it to be smart, you also take care of automatic resource release/returns by programming the destructor accordingly, and you would clearly define how copy or assignment works by implementing the copy constructor and copy assignment operator or by prohibiting it by declaring these two as private.

63.
Can my subscript operator [] return const and non-const variants of return types?
const Type& operator[](int index);
Type& operator[](int index); // is this OK?

No, C++ does not allow two functions with the same name to have different return values. You can program two implementations of operator [] with identical return types, one defined as a const function and the other not. In this case, C++ compiler picks the non-const version for assignment-related activities and the const version otherwise:
Type& operator[](int Index) const;
Type& operator[](int Index);

64.
Would you ever declare the copy constructor or copy assignment operator as private?

Yes, but only if I don't want my class to allow copying or assignment. Such a restriction would be necessity when programming a singleton-a class that permits the existence of only one instance.

65.
Would it make sense to define a move constructor and move assignment operator for your class Date?

As there are no dynamically allocated resources contained within class Date that cause unnecessary memory allocation and deallocation cycles within the copy constructor or copy assignment operator, this class is not a good candidate for a move constructor or move assignment operator.

66.
Is it okay to modify the contents of a const-object by casting a pointer or reference to it using const_cast?

Most definitely not. The result of such an operation is not defined and is definitely not desired.

67.
I need a Bird*, but have a Dog* at hand. The compiler does not allow me to use the pointer to the Dog object as a Bird*. However, when I use reinterpret_cast to cast the Dog* to Bird*, the compiler does not complain and it seems I can use this pointer to call Bird's member function, Fly(). Is this okay?

Again, definitely not. reinterpret_cast changed only the interpretation of the pointer, and did not change the object being pointed to (that is still a Dog). Calling a Fly() function on a Dog object will not give the results you are looking for, and could possibly cause an application failure.

68.
I have a Derived object being pointed to by a objBase that is a Base*. I am sure that objBase points to a Derived object, so do I really need to use dynamic_cast?

Because you are sure that the object being pointed to is a Derived type, you can save on runtime performance by using static_cast.

69.
C++ provides casting operators, and yet I am advised to not use them as much as possible. Why is that?

You keep aspirin at home, but you don't make it your staple diet just because it's available, right? Use casts only when you need them.

70.
Why should I use inclusion guards in my header files?

Inclusion guards using #ifndef, #define, and #endif protect your header from multiple or recursive inclusion errors, and in some cases they even speed up compilation.