有没有办法在超类构造函数中知道调用对象的子类? C ++

时间:2017-01-13 10:23:51

标签: c++ rtti

我想我有类似的东西:

class A 
{
  public:
     A(A* owner)
     {
        m_owner=owner;
        if (dynamic_cast<B*>(this))
        {
          m_id=sm_bid; 
          sm_bid++;
        }
        else
        {
          m_id=sm_aid;
          sm_aid++;
        }
     }
  private:
    int m_id;
    A*  m_owner;
    static int sm_aid;
    static int sm_bid;
};

A::sm_aid=0;
A::sm_bid=0;

class B: public A
{
    B(A* owner) : A(owner) {}
};

不幸的是,dynamic_cast无法捕获它是一个B对象(实例化时)。这听起来合乎逻辑(因为超类中的构造函数在调用子类中的构造函数之前被调用。有没有办法实现这样的功能?

4 个答案:

答案 0 :(得分:2)

您的设计看起来很复杂,几乎可以肯定,无需了解它的基础就可以实现。

然而,用C ++射击自己是神圣的权利:)因此,你可以尝试使用Curiously Recurring Template Pattern (CRTP)来实现你的目标(通过静态多态性在构造函数中进行多态行为)。

Here is the quick and dirty sketch

    @Configuration
    public class DbConfig {

    @Bean
    public DataSource dataSource() {

        //Create the DataSource with integration-DB properties

        return dataSource;
    }
}

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes=DbConfig.class)
    public class EntityDaoTest {

    }

输出:

#include <iostream>
#include <type_traits>

struct Owner
{};

struct B;

template <class Derived>
struct A : public Owner
{
    A(Owner*)
    {
        if (std::is_same<Derived, B>::value)
            std::cout << "called from derived B" << std::endl;
        else
            std::cout << "not called from derived B\n" << std::endl;
    }
};

struct B : public A<B>
{
    B(Owner* owner) : A<B>(owner) {}
};

int main()
{
    A<void> a(nullptr);
    B b(&a);
}

注意:我注意到您正在使用静态变量进行计数。请注意,每个模板实例化都有自己的静态变量!

答案 1 :(得分:2)

首先,要使dynamic_cast工作,你需要在基类中至少有一个虚方法(通常析构函数是虚拟的,因为你经常因为其他原因需要它)。

其次,在构造A子对象时,在A的构造函数中,所有虚函数都将引用A的类定义(即使您实际构造A }作为B)和dynamic_cast的子对象会说this不是B类型。

这是因为在 B构造函数完成之前,对象不是类型B的对象,所以任何和所有检查都试图确定它是否为类型B会说它不是因为该对象实际上不是类型B(还)。

我希望这能为你解决一些困惑。

修改
我刚刚注意到你问题的最后一段,你要求解决这个问题。

在不了解问题的情况下,很难真正帮助您实现此功能。我的建议是不这样做,也许完全使用另一个类来为你的对象分配id而不是试图让A去做。

如果确实必须在A,那么这可能会有所帮助:

struct A
{
  template <class FinalType>
  A(A* owner, FinalType *){...}//use std::is_same to check if `B` or not
};

struct B : A
{
  B(A * owner):A(owner, this){}
};

答案 2 :(得分:1)

如果A是抽象类型,则可以使用基于标记的重载技术。

class B;
class C;

class A
{
public:
    template<class Derived>
    struct my_derived
    {
        using type = Derived;
    };

protected:
    explicit A(my_derived<B>)
    {
        // Do whatever you want when derived is B
    }

    explicit A(my_derived<C>)
    {
        // Do whatever you want when derived is C
    }

private:

};

class B : A
{
public:
    B() : A(A::my_derived<B>())
    {
    }
};

class C : A
{
public:
    C() : A(A::my_derived<C>())
    {
    }
};

另一种方法是基于模板的编程,如AMA建议的那样。 虽然模板是C ++中的强大功能,但它们有自己的问题。它们无法在cpp文件中实现。它们会导致代码膨胀,因为更大的代码大小会影响性能,从而导致更高的指令缓存丢失率。

所以我认为这种方法比具有运行时性能影响的RTTI更好,你必须在基类中声明一个虚函数,以及基于模板的方法。

答案 3 :(得分:0)

看起来这很好用:

#include <iostream>
#include <type_traits>

class Owner
{};

class B;

template <class Derived>
class A : public Owner
{
    public:
    A(Owner* owner)
    {
        m_owner=owner;
        if (std::is_same<Derived, B>::value)
            std::cout << "called from derived B" << std::endl;
        else
            std::cout << "not called from derived B\n" << std::endl;
    }
    private:
    Owner* m_owner;
};

class B : public A<B>
{
    public:
    B(Owner* owner) : A<B>(owner)
    {}
};

int main()
{
    A<void> a(nullptr);
    B b(&a);
}

感谢AMA。任何事情都可以在C ++中完成