这个指针指向基类的构造函数? [英] this pointer to base class constructor?

查看:138
本文介绍了这个指针指向基类的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个派生类,它也应该实现一个接口,有一个基类可以调用的函数。下面给出一个警告,因为将这个指针传递给基类构造函数是不安全的:

  struct IInterface 
{
void FuncToCall()= 0;
};

struct Base
{
Base(IInterface * inter){m_inter = inter; }

void SomeFunc(){inter-> FuncToCall(); }

IInterface * m_inter;
};

struct Derived:Base,IInterface
{
Derived():Base(this){}

FuncToCall };

这是最好的方法是什么?我需要提供接口作为基础构造函数的参数,因为它不总是作为接口的dervied类;有时它可能是一个完全不同的类。



我可以添加一个函数到基类SetInterface(IInterface * inter),但我想避免这样。

c> c>。在这种实际情况下,虽然,它似乎是安全的,因为你只发布到基类,它只存储它,并且不调用它,直到稍后的某个点,在那时,构建将完成。但是,如果你想摆脱警告,你可以使用静态工厂方法:

  struct Base 
{
public:
Base(){}

void setInterface(IInterface * inter){m_inter = inter; }

void SomeFunc(){inter-> FuncToCall(); }

IInterface * m_inter;
};

struct派生的:Base,IInterface
{
private:
Derived():Base(){}

public:
static Derived * createInstance(){
Derived instance = new Derived();
instance-> setInterface(instance);
return instance;
}

FuncToCall(){}
};

请注意, Derived 的构造函数是private以确保仅通过 createInstance 进行实例化。


I want to implement a derived class that should also implement an interface, that have a function that the base class can call. The following gives a warning as it is not safe to pass a this pointer to the base class constructor:

struct IInterface
{
    void FuncToCall() = 0;
};

struct Base
{
    Base(IInterface* inter) { m_inter = inter; }

    void SomeFunc() { inter->FuncToCall(); }

    IInterface* m_inter;
};

struct Derived : Base, IInterface
{
    Derived() : Base(this) {}

    FuncToCall() {}
};

What is the best way around this? I need to supply the interface as an argument to the base constructor, as it is not always the dervied class that is the interface; sometimes it may be a totally different class.

I could add a function to the base class, SetInterface(IInterface* inter), but I would like to avoid that.

解决方案

You shold not publish this from the constructor, as your object is not yet initialized properly at that point. In this actual situation, though, it seems to be safe, since you are publishing it only to the base class, which only stores it and does not invoke it until some point later, by which time the construction will have been finished.

However, if you want to get rid of the warning, you could use a static factory method:

struct Base
{
public:
    Base() { }

    void setInterface(IInterface* inter) { m_inter = inter; }

    void SomeFunc() { inter->FuncToCall(); }

    IInterface* m_inter;
};

struct Derived : Base, IInterface
{
private:
    Derived() : Base() {}

public:
    static Derived* createInstance() {
        Derived instance = new Derived();
        instance->setInterface(instance);
        return instance;
    }

    FuncToCall() {}
};

Note that the constructor of Derived is private to ensure that instantiation is done only via createInstance.

这篇关于这个指针指向基类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆