The process of determining the types and values of the template arguments from the type of the function arguments is called template argument deduction.
int compare(const int &v1, const int &v2)
{
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
int compare(const double &v1, const double &v2)
{
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
int main()
{
compare(1, 0); // ok: binds template parameter to int
compare(3.14, 2.7); // ok: binds template parameter to double
return 0;
}
In the first call, compare(1, 0), those arguments are type int; in the second, compare(3.14, 2.7), they have type double.
Wednesday, July 1, 2009
Wednesday, May 20, 2009
What are Functors or Function Objects?
A functor is an object that can behave like a function. It does so by overloading operator () (i.e., function call). It should generally implement operator () as a const member function, i.e., doesn't change the object or is stateless. It should implement cheap copy construction and assignment because temporaries are often created and should be optimized. A functor is suitable for working with template functions, such as the STL algorithms sort() and copy(), because of these properties.
Saturday, April 25, 2009
Design Issues to consider while creating a Class
1. Implement all the default functions provided by the compiler like Constructor, Copy Constructor, Destructor, Assignment Operator and Address-of Operator to avoid compiler defining its own
Just define it even if you think you don't need it at the moment, your class will be Scalable if you define all this functions and avoid any nasty surprises for your users, if they use any of this functions without knowing that they are not at all implemented.
Incase, you don't want to provide implementation for any of them, just provide an empty function for future use.
Copy-Constructor
You will avoid issues related to shallow copy if you provide your own Copy Constructor.
Constructor
Use initialization list instead of assignment while initializing data members. This is mandatory for Consts and References but you can use initialization list for all the data as it is more efficient then assignment.
Assignment Operator
1. Check for self assignment
2. Delete previous data before assignment
3. Return object reference to allow chains of assignments
4. Assign values to all data members
Destructor
Declare destructors as virtual in your base classes
2. If you want to make your class behave just like a interface for your more specific derived classes, you can provide atleast one pure virtual function, so that your class will become abstract class and you will not be able to create object of it.
3. Make all your data as private. Constructor, Destructor and any other functions as public.
Declare your Constructor as private if you want to make your class as final or not-inheritable.
4. Prefer pass-by-reference to pass-by-value
Just define it even if you think you don't need it at the moment, your class will be Scalable if you define all this functions and avoid any nasty surprises for your users, if they use any of this functions without knowing that they are not at all implemented.
Incase, you don't want to provide implementation for any of them, just provide an empty function for future use.
Copy-Constructor
You will avoid issues related to shallow copy if you provide your own Copy Constructor.
Constructor
Use initialization list instead of assignment while initializing data members. This is mandatory for Consts and References but you can use initialization list for all the data as it is more efficient then assignment.
Assignment Operator
1. Check for self assignment
2. Delete previous data before assignment
3. Return object reference to allow chains of assignments
4. Assign values to all data members
Destructor
Declare destructors as virtual in your base classes
2. If you want to make your class behave just like a interface for your more specific derived classes, you can provide atleast one pure virtual function, so that your class will become abstract class and you will not be able to create object of it.
3. Make all your data as private. Constructor, Destructor and any other functions as public.
Declare your Constructor as private if you want to make your class as final or not-inheritable.
4. Prefer pass-by-reference to pass-by-value
Friday, April 17, 2009
How to prevent Object Slicing in C++?
Make the function in base class as pure virtual function, so that will restrict creation of base class object (while creating derived class object) which will eventually avoid object slicing..
By making function as pure virtual in base class, the compiler would prevent object slicing because that wouldn’t allow you to “create” an object of the base type (which is what happens when you upcast by value)
This could be the most important value of pure virtual functions: to prevent object slicing by generating a compile-time error message if someone tries to do it.
By making function as pure virtual in base class, the compiler would prevent object slicing because that wouldn’t allow you to “create” an object of the base type (which is what happens when you upcast by value)
This could be the most important value of pure virtual functions: to prevent object slicing by generating a compile-time error message if someone tries to do it.
Subscribe to:
Posts (Atom)