Analysis: This is Adobe's latest exam for campus recruitment in 2007. In addition to the applicant's
In addition to the basic skills of C ++, it is a good question to test the ability to respond.
The keyword final is defined in Java, and the class modified by final cannot be inherited. But not in C ++
With the keyword final, it takes some effort to implement this requirement.
In C ++, the constructor of the subclass automatically calls the constructor of the parent class. Similarly,
The sub-class destructor automatically calls the parent class's destructor. If a class cannot be inherited, I
You just need to define both its constructor and destructor as private functions. So when a class tries
When it inherits, it will inevitably cause compilation errors due to the attempt to call constructor and destructor.
However, the constructor and destructor of this class are both private functions. How can we obtain this class?
? We can create and release class instances by defining static resources.
Based on this idea, we can write the following code:
//////////////////////////////////////// //////////////////////////
/////
// Define a class which can't be derived from
//////////////////////////////////////// //////////////////////////
/////
Class finalclass1
{
Public:
Static finalclass1 * getinstance ()
{
Return new finalclass1;
}
Static void deleteinstance (finalclass1 * pinstance)
{
Delete pinstance;
Pinstance = 0;
}
PRIVATE:
Finalclass1 (){}
~ Finalclass1 (){}
};
This class cannot be inherited, but I always think it is a little different from the general class.
Point is not convenient. For example, we can only get instances on the stack, but not instances on the stack.
Can I implement a class with the same usage as a general class except that it cannot be inherited? Solution
There are always, but some skills are required. See the following code:
//////////////////////////////////////// //////////////////////////
/////
// Define a class which can't be derived from
//////////////////////////////////////// //////////////////////////
/////
Template <typename T> class makefinal
{
Friend t;
PRIVATE:
Makefinal (){}
~ Makefinal (){}
};
Class finalclass2: virtual public makefinal <finalclass2>
{
Public:
Finalclass2 (){}
~ Finalclass2 (){}
};
This class is similar to a general class. You can create instances on the stack or stack.
Although makefinal <finalclass2> constructors and destructor are private
The class finalclass2 is its friend function, so makefinal is called in finalclass2.
Neither the <finalclass2> constructor nor the Destructor will cause compilation errors.
However, when we try to inherit a class from finalclass2 and create its instance
Translation.
Class try: Public finalclass2
{
Public:
Try (){}
~ Try (){}
};
Try temp;
Because the class finalclass2 is inherited from the class makefinal <finalclass2> virtual
When using the try constructor, finalclass2 is skipped and makefinal is called directly.
<Finalclass2> constructor. Unfortunately, try is not makefinal
<Finalclass2>, so you cannot call its private constructor.
Based on the above analysis, the classes that are attempted to inherit from finalclass2 will be compiled once instantiated.
Therefore, finalclass2 cannot be inherited. This satisfies our design requirements.