C ++ object-oriented programming in how to determine the type of an object class

In the C ++ object-oriented programming, it is sometimes necessary to determine the type of an object class, then there may be the following two ways:

Method 1: Using the typeid () function determines :( disadvantages: when there is inheritance, the subclass pointer and assigned to the parent pointer, the object type determination supertype this case, not the subclass type)

 

For a parent CFather, which has two subclasses CChildOne, CChildTwo. So how to determine the class type of an object. Such as:

CChildOne objChildOne;

ChildTwo* pChildTwo;

if(typeid(objChildOne) ==typeid(CChildOne))

{

Cout << "class type of the object is CChildOne" << endl;

}

if(typeid(*pChildTwo) ==typeid(CChildTow))

{

Cout << "type of this object is CChildTwo" << endl;

}

 

However, this method seems there is a problem: If CFirstChild CSuper class is a subclass:

CFirstChild* pChild = new CFirstChild("米兰");

    if(typeid(*pChild) ==typeid(CFirstChild))

    {

       cout << "CFirstChild" << endl; // Here is a subclass of objects, no problem

    }

 

    CSuper * pObj = pChild; // here into a parent object, the object is not a subclass of

    if(typeid(*pObj) == typeid(CSuper))

    {

       cout << "CSuper" << endl; // so the implementation of the conditions

    }

    if(typeid(*pObj) == typeid(CFirstChild))

    {

       cout<<"CFirstChild"<<endl;

    }

 

    CSuper * pObj2 = new CFirstChild ( "Inter"); // here to become a parent class object, not the object of a subclass

    if(typeid(*pObj2) == typeid(CSuper))

    {

       cout << "CSuper" << endl; // so the implementation of the conditions

    }

    if(typeid(*pObj2) == typeid(CFirstChild))

    {

       cout<<"CFirstChild"<<endl;

    }

 

    delete pChild;

    delete pObj2;

 

Method 2: The best way is to define a virtual function of the return type

 

At runtime type identification, the easiest way is all class (parent class and subclass) implements a virtual method returns the class name, returns the class name according to the typeid reuse () is determined. Such as:

Definition of national parent CCounty,

Wherein the virtual CCounty * GetClassType (void); return to its class type:

CCounty* CCounty::GetClassType(void)

{

    return this;

}

Subclassing: CUSA, while achieving virtual functions virtual CUSA * GetClassType (void);

CUSA* CUSA::GetClassType(void)

{

    return this;

}

Subclassing: CIraq, while achieving virtual functions virtual CIraq * GetClassType (void);

* :: GetClassType Iraq Iraq (void)

{

    return this;

}

 

Thus when using the virtual function call directly GetClassType () can return the corresponding class name, using the typeid () can determine the type of its corresponding. Such as:

CCounty* pCounty = new CUSA();

if (typeid (* (pCounty-> GetClassType ())) == typeid (CUSA)) // Here is a subtype of CUSA

and:

CCounty* pCounty = new CIraq();

if (typeid (* (pCounty-> GetClassType ())) == typeid (CIraq)) // Here is a subtype CIraq

Published 29 original articles · won praise 14 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43778179/article/details/104626226