Tuesday, July 13, 2010

C++ Basics: C++ Basic

C++ Basics: C++ Basic: "Copy Constructor.CC takes a reference to a const parameter to guarantee that CC doest change, and & becuause a value parameter would require..."

Sunday, July 11, 2010

C++ Basic

Copy Constructor.

CC takes a reference to a const parameter to guarantee that CC doest change, and & becuause a value parameter would require making a copy to invoke CC.

As the name suggests, a CC is called whenever an object is copied. This happens in the following cases:

  • When an object is create from another object during initialization (Class a = b)
  • When an object is created from another object as a parameter to a constructor (Class a(b))
  • When an object is passed by value as an argument to a function (function(Class a))
  • When an object is return from a function (Class a; … return a;)
  • When an exception is thrown using this object type (Class a; …. throw a;)

The default copy constructor is a shallow (member wise) copy.

Some Rules of Thumb:

  • Don’t write a CC if a bit-by-bit copy works for the class
  • If defined own CC, probably because deep copy needed or some special resource management, then need to release these resources at the end, so define a destructor and also think of overloading the assignment operator (beware of self-assignment)
Conversion Constructors:
A constructor that can be called with a single argument is used for conversions from the type of the argument to the class type. Such a constructor is called a conversion constructor. Consider the following example:
class Point 
{ public:
     Point();
     Point( int );     //... 
};  
int main() 
{ }

Conversion constructors (C++ only)

A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit. The compiler uses conversion constructors to convert objects from the type of the first parameter to the type of the conversion constructor's class. The following example demonstrates this:
class Y {
int a, b;   
char* name; 
public:   
Y(int i) { };   
Y(const char* n, int j = 0) { }; 
};  
void add(Y) { };  
int main() {
    // equivalent to   
// obj1 = Y(2)   
Y obj1 = 2;    
// equivalent to   
// obj2 = Y("somestring",0)   
Y obj2 = "somestring";    
// equivalent to   
// obj1 = Y(10)   
obj1 = 10;    
// equivalent to   
// add(Y(5))   
add(5); }
The above example has the following two conversion constructors:
  • Y(int i)which is used to convert integers to objects of class Y.
  • Y(const char* n, int j = 0) which is used to convert pointers to strings to objects of class Y.
The compiler will not implicitly convert types as demonstrated above with constructors declared with the explicit keyword. The compiler will only use explicitly declared constructors in new expressions, the static_cast expressions and explicit casts, and the initialization of bases and members. The following example demonstrates this:
class A { public:   
explicit A() { };   
explicit A(int) { }; 
};  
int main() {   
A z; //  A y = 1;   
A x = A(1);   
A w(1);   
A* v = new A(1);   
A u = (A)1;   
A t = static_cast(1); }

The compiler would not allow the statement A y = 1 because this is an implicit conversion; class A has no conversion constructors.

A copy constructor is a conversion constructor.