Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
such as:
C++
class MyString
{
public:
    MyString();
};
class MyStringA
{
public :
    MyStringA();
}
int main()
{
    MyString *pStr=new MyString;
    MyStringA *pStrA=new MyStringA;
    if(pStrA==Mystring) ??????how check pStrA class object belong MyString Class object
    {
        cout << "Be MyString"<<endl;
    }
    else
    {
        cout << "not MyString"<<endl;
    }
}

englist only poor,Thank you to answer!
c++ fan!;
Posted
Updated 17-Feb-12 18:48pm
v4

Use rtti. U will perhaps be using typeid or dyn cast
 
Share this answer
 
C++
#include <iostream>
#include <typeinfo>
using namespace std;

// Debug in
// Code::Blocks 10.05
// GCC 4.4.1
// 2012-2-18
class MyString
{
public:
    MyString(){}  // Default Implementation
};

class MyStringA
{
public :
    MyStringA(){}  // // Default Implementation
}; // you forgot a semicolon here

int main()
{
    MyString *pStr = new MyString;
    MyStringA *pStrA = new MyStringA;
    if(typeid(*pStrA) == typeid(MyStringA)) // do the check
    {
       cout << "Be MyString"<<endl;
    }
    else
    {
        cout << "not MyString"<<endl;
    }
}
 
Share this answer
 
v3
Comments
Emilio Garavaglia 18-Feb-12 11:04am    
Reformatted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900