C++ 转发声明和类型ID

C++ 转发声明和类型ID,c++,gcc,declaration,forward-declaration,typeid,C++,Gcc,Declaration,Forward Declaration,Typeid,我想检查超类a的类型与子类B的类型(在超类a中有一个方法,以便B将继承它) 下面是我认为的诀窍(即使用转发声明): 如何解决这个问题?一种方法是将Check的定义从类定义中拉出,以便在编译器到达函数定义时定义B class A { //... void Check(); //... }; class B { /* ... */ }; void A::Check() { //... } 只需将Check()的定义移出 #include <iostream&

我想检查超类
a
的类型与子类
B
的类型(在超类
a
中有一个方法,以便
B
将继承它)

下面是我认为的诀窍(即使用转发声明):


如何解决这个问题?

一种方法是将
Check
的定义从类定义中拉出,以便在编译器到达函数定义时定义
B

class A {
    //...
    void Check();
    //...
};
class B { /* ... */ };

void A::Check() {
    //...
}

只需将Check()的定义移出

#include <iostream>
#include <typeinfo>

using namespace std;

class B;

class A {
  public:
    int i_;
    void Check ();
};

class B : public A {
  public:
    double d_;
};

void A::Check () {
  if (typeid (*this) == typeid (B))
    cout << "True: Same type as B." << endl;
  else
    cout << "False: Not the same type as B." << endl;
}

int main () {

  A a;
  B b;

  a.Check (); // should be false
  b.Check (); // should be true

  return 0;
}
#包括
#包括
使用名称空间std;
乙级;;
甲级{
公众:
国际组织;
无效支票();
};
B类:公共A{
公众:
双d_;
};
void A::检查(){
if(typeid(*this)=typeid(B))

不能将检查的定义移动到B类声明下面。

只需在B类声明之后移动函数体即可

#include <iostream>
#include <typeinfo>

struct A
{
    int i_;
    void Check();
};

struct B :  A
{
    double d_;
};

void A::Check()
{
    using namespace std;
    if (typeid (*this) == typeid (B))
    {
        cout << "True: Same type as B." << endl;
    }
    else
    {
        cout << "False: Not the same type as B." << endl;
    }
}

int main()
{
    A a;
    B b;

    a.Check(); // should be false
    b.Check(); // should be true

    return 0;
}
#包括
#包括
结构A
{
国际组织;
无效检查();
};
结构B:A
{
双d_;
};
void A::Check()
{
使用名称空间std;
if(typeid(*this)=typeid(B))
{

cout我认为您试图解决的问题最好通过虚拟方法来处理:

class A
{
    public:
        virtual bool Check() { return false; };
}


class B : public A
{
    public:
        // override A::Check()
        virtual bool Check() { return true; };
}
基类A中的方法不需要知道对象“真的”是A还是B。这违反了基本的面向对象设计原则。如果当对象是B时行为需要更改,那么该行为应该在B中定义并通过虚拟方法调用处理。


嗨, 即使将A::Check的定义放在类之外,结果也不会是您期望的结果。这是因为此B对象将转换为方法中的A对象,因此它指向A对象,因此TypeID总是不同的。要解决此问题,请声明方法为virtual


但是,我仍然不明白您为什么要执行这样的测试,正如CAdaker所说,这不是一个好的实践

我没有想到我违反了OOP的基本原则。更不用说我试图做的事情没有起作用。正如您所建议的,我使用虚拟函数重写了所有内容。谢谢!感谢您的快速回复呃!你的建议确实有效。但是我更喜欢用虚拟函数重写一切。我想这样做是为了根据类的类型触发不同的方法。如果b.Check()那么b.method_在_子类()中;否则b.method_在_超类()中;但多亏了你的提示,我认为这算不上是一种好的做法。
#include <iostream>
#include <typeinfo>

struct A
{
    int i_;
    void Check();
};

struct B :  A
{
    double d_;
};

void A::Check()
{
    using namespace std;
    if (typeid (*this) == typeid (B))
    {
        cout << "True: Same type as B." << endl;
    }
    else
    {
        cout << "False: Not the same type as B." << endl;
    }
}

int main()
{
    A a;
    B b;

    a.Check(); // should be false
    b.Check(); // should be true

    return 0;
}
class A
{
    public:
        virtual bool Check() { return false; };
}


class B : public A
{
    public:
        // override A::Check()
        virtual bool Check() { return true; };
}