Covariant return type is a C++ language feature that makes us free of all the hassles that comes with downcasting. First we give an example of what kind of hassles that we have o take in case of downcasting.
From the code we can see that the clone function makes a copy of the object and returns it in an “upcasted” state (so, if we call the CloneMe() function from a subclass CDerived, we will have to receive it in a CBase* and then downcast it again to get a CDerived) .
This kind of downcasting actually can complicate things. Firstly, we have to use dynamic_cast<> to downcast it again to its actual type (CDerived*) from what is returned (CBase*). Secondly, we have to check it again to see whether downcast was successful or not (if CDerived is not the actual object it was cloned from?? Maybe the CloneMe was called from CDerived2 which also derives from CBase and we are trying to cast it to CDerived? In this situation, dynamic_cast<> will return NULL. )
So, to relieve us, C++ has a language feature which is called Covariant return type. In that thing we can just return a CDerived* instead of a CBase* in the virtual CloneMe() function. And also we can upcast it to CBase* without any problem. The compiler does that for us.
12. Maximal Munch rule
Look at the code of MaximalMunch.cpp . The decleration of "vector
So, the important thing to remember is to put a space simply to make the compiler stop complaining.
For more see here
13. Zombie objects
Zombie objects are created if no facilities are provided to throw and catch exception from constructor and if any exception occurs in the middle of the construction process. This situation creates the objects which are halfway alive.
Zombie objects are also created when the language does not provide support exception handling mechanism in the constructor or the language at all.
So, in the case the exception handling mechanism is not provided with the constructors, each object has to be checked to see whether it is in its zombie state or not .
14. Resource deallocation primitives
It simply means the overloads of operator delete, closing file stopping semaphores etc.
15. Named Constructor Idiom
Named constructor idiom is needed for the situations where one has to have two overloaded constructors but the both the constructors take the same number and types of parameters. It is the internal processing of the parameters to create the object that differs. For Example
CPoint(float aX, float aY); //rectangular coordinate system
CPoint(float aRadius, float aTheta); // polar coordinates system
In the situation, the overloaded constructor will not work. So, we have to use the named constructor idiom.
How the named constructor Idiom works? The code shows you how to do that.
Source1

0 comments:
Post a Comment