Some statements:
1. Result of deleting an array of derived class objects through a base class pointer is undefined.
2. Avoid default constructors, instead use ctor(s) with default value for input parameter(s).
3.
Q: C++ *_cast explanation?
A:
const : const member functions(getter function), const pointers, const variables(data member or object)
ie: const char *const m_f(…) const {}
const_cast<> operator will cast in/out between const and non-const time, but be aware of read-only memory access problem when
cast from const to non-cast, since const type variable may have been allocated in read-only memory section!
dynamic_cast, const_cast, typeid Link: http://www.cplusplus.com/doc/tutorial/tut5-4.html
dynamic_cast, is used to perform safe casts down or across an inheritance hierarchy. That is, you use dynamic_cast to cast pointers or references to base class objects into pointers or references to derived or sibling base class objects in such a way that you can determine whether the casts succeeded.1 Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references).
i.e:
class Widget { … };
class SpecialWidget: public Widget { … };
void update(SpecialWidget *psw);
Widget *pw;
…
update(dynamic_cast<SpecialWidget*>(pw));
// fine, passes to update a pointer
// to the SpecialWidget pw points to
// if pw really points to one,
// otherwise passes the null pointer
explicit keyword: http://www.glenmccl.com/tip_023.htm
i.e.:
class A {
public:
explicit A(int);
};
void f(A) {}
void g()
{
A a1 = A(37);
A a2 = A(47);
A a3(57);
a1 = A(67);
f(A(77)); //
}
Q: What is the difference between a copy constructor and an overloaded assignment operator?
A: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
Q: What is a conversion constructor?
A: A constructor that accepts one argument of a different type.
Q: What is a virtual destructor?
A: The simple answer is that a virtual destructor is one that is declared with the virtual attribute. you destroy an object through a pointer or reference to a base class,
Q: What is a mutable member?
A: One that can be modified by the class even when the object of the class or the member function doing the modification is const.
Q: What is an explicit constructor?
A: A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction.
Q: Describe run-time type identification.
A: The ability to determine at run time the type of an object by using the typeid operator or the dynamic_cast operator.
Q: What problem does the namespace feature solve?
A: Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions.
Q: Implement String class that supports intuitive string concatenation?
A: Copy constructor (pass and return object(s) by value!!!) is the one to go…
//
class String {
public:
String(); //default ctor
String(const char *value); // constructor!
String(const String &src); // copy ctor
String operator+(const String &s1, const String &s2); // overloading + operator
String& operator=(const String& rhs); // Assignment ctor
~String() { //dtor
delete [] data;
}
private:
char *data;
}; //String{}
// a possible String constructor
String::String(const char *value)
{
if (value) { // if value ptr isn’t null
data = new char[strlen(value) + 1];
strcpy(data,value);
}
else { // handle null value ptr3
data = new char[1];
*data = ‘