class Integer
{
public:
Integer(int x) : m_x(x) {};
operator int (void)
{
return m_x;
}
operator const char*(void) //conversion from int to const char (*), also note that the compiler does not make difference between const char (*) and char[]
{
char buff[100];
sprintf( buff, "%d", m_x );
return buff;
}
private:
int m_x;
};
int main()
{
Integer p(7897);
double d = p; //conversion from class "Integer" to int and then assigning to double
cout<<d<<endl;
cout<<p<<endl; //conversion to const char (*)
}
2. Const value/const pointer
Const before "*" refers to the value. Const after the "*" refers to the pointer.
double const* const p1; // Const pointer to const double
// . you can't have the pointer point to another address
// . you can't mutate the value through the pointer
double* const p3; // Const pointer to double
// . you can't have the pointer point to another address
// . you can mutate the value through the pointer
3. Supply objects by reference rather than value
We have polymorphism (calling the correct method declared with virtual) only when we are using pointers (or references). If you supply Derived d; to func(Base b), there is no chance any Derived method will be called. The parameter of func must be a pointer or reference.
4. Virtual destructor
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructing Base";}
// this is a destructor:
~Base(){ cout<<"Destroying Base";}
};
class Derive: public Base
{
public:
Derive(){ cout<<"Constructing Derive";}
~Derive(){ cout<<"Destroying Derive";}
};
void main()
{
Base <strong>basePtr = new Derive();
delete basePtr;
}
On basePtrconstruction first Base constructor is called and then Derived.
On deletion without virtual keyword only ~Base is called and this leads to potential memory leak.
On deletion with virtual keyword first Derived destructor is called and then Base destructor is called which is kind of natural all destructors to be called.
5. The dynamic_cast<> and typeid operator in C++ are part of RTTI
This is a very nice tutorial: http://www.codingunit.com/cplusplus-tutorial-typecasting-part-2-rtti-dynamic_cast-typeid-and-type_info
Base to derived conversion is not allowed with dynamic_cast unless the base class is polymorphic.
An "up-cast" is always valid with both static_cast and dynamic_cast, and also without any cast, as an "up-cast" is an implicit conversion.
Type id example:
bool bb=1;
std::cout<< typeid(bb).name();
int (*a);
const type_info& p = typeid(a); //note that it returns reference and we make it const
cout<< p.name();
6. mutable
Mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.
struct data
{
char name[80];
mutable double salary;
}
const data MyStruct = { "Satish Shetty", 1000 }; //initlized by complier
strcpy ( MyStruct.name, "Shilpa Shetty"); // compiler error because MyStruct is const
MyStruct.salaray = 2000 ; // complier is happy allowed because salary is mutable
7. Why the parameter of the copy constructor is a reference?
Because taking its argument by value would cause infinite recursion (when you pass something by value, you're passing a copy of the original item which triggers another copy constructor...)
8. Under normal circumstances, the return type of a virtual function and it’s override must match
Thus, the following will not work:
class Base
{
public:
virtual int GetValue() { return 5; }
};
class Derived: public Base
{
public:
virtual double GetValue() { return 6.78; }
};
However, there is one special case in which this is not true. If the return type of a virtual function is a pointer or a reference to a class, override functions can return a pointer or a reference to a derived class. These are called covariant return types. Here is an example:
class Base
{
public:
// This version of GetThis() returns a pointer to a Base class
virtual Base</strong> GetThis() { return this; }
};
class Derived: public Base
{
// Normally override functions have to return objects of the same type as the base function
// However, because Derived is derived from Base, it's okay to return Derived* instead of Base*
virtual Derived* GetThis() { return this; }
};
9. Reading and writing C++ objects to file
One should use packaging from Boost: http://www.boost.org/doc/libs/1_41_0/libs/serialization/doc/index.html




