在C ++中覆盖成员变量 [英] Overriding a member variable in C++

查看:169
本文介绍了在C ++中覆盖成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些棘手的问题在一些C ++代码,这是最容易描述使用代码。我有类似的东西:

I have run into a bit of a tricky problem in some C++ code, which is most easily described using code. I have classes that are something like:

class MyVarBase
{
}

class MyVar : public MyVarBase
{
    int Foo();
}

class MyBase
{
public: 
    MyBase(MyVarBase* v) : m_var(v) {}
    virtual MyVarBase* GetVar() { return m_var; }
private:
    MyVarBase* m_var;
}



我还有一个MyBase的子类,需要有一个MyVar类型的成员因为它需要调用Foo。将Foo函数移动到MyVarBase中不是一个选项。是否有意义:

I also have a subclass of MyBase that needs to have a member of type MyVar because it needs to call Foo. Moving the Foo function into MyVarBase is not an option. Does it make sense to do this:

class MyClass : public MyBase
{
public:
    MyClass(MyVar* v) : MyBase(v), m_var(v) {}
    MyVar* GetVar() { return m_var; }
private:
    MyVar* m_var;
}

这似乎工作,但看起来真的很糟糕,我不知道将导致内存泄漏或破坏复制构造函数。我的其他选项可能是在MyClass中将MyVar变量命名为别的名称,但它等于基本中的m_var指针,或者MyVar类型上的模板MyBase。

This seems to work but looks really bad and I'm not sure if it's going to cause a memory leak or break a copy constructor. My other options might be to name the MyVar variable in MyClass something else but have it be equal to the m_var pointer in the base, or to templatise MyBase on the MyVar type.

所有这些选项似乎不太理想,所以我想知道是否有其他人遇到过这种情况,如果有一个好的方法使它工作。

All these options don't seem ideal so I wanted to know if anyone else has run into a situation like this and if there is a good way to make it work.

推荐答案

正确的做法是让变量只在基类中。因为派生类知道它必须是动态类型 MyVar ,这是完全合理的:

The correct way to do this is to have the variable only in the base class. As the derived class knows it must be of dynamic type MyVar, this is totally reasonable:

class MyClass : public MyBase
{
public:
    MyClass(MyVar* v) : MyBase(v) {}
    MyVar* GetVar() { return static_cast<MyVar*>(MyBase::GetVar()); }
}

由于MyVar派生自MyVarBase,所以不同的返回类型 GetVar 仍然可以工作,如果GetVar是虚拟的(如这里的情况)。注意,使用该方法,在 MyBase 中必须没有可以将指针重置为不同的函数,很明显。

Since MyVar is derived from MyVarBase, the different return-types of GetVar would still work if GetVar was virtual (as is the case here). Note that with that method, there must be no function in MyBase that can reset the pointer to something different, obviously.

请注意, static_cast 是这种情况下的正确投射。使用一个注释器提出的 dynamic_cast 会告诉GetVar的读者和用户 MyBase :: GetVar()可以返回一个指向非MyVar类型的对象的指针。但这并不反映我们的意图,因为你只有通过MyVar。因此是软件开发中最重要的事情。你可以做的是断言它是非空的。它将在运行时中断您的项目的调试生成中的错误消息:

Note that static_cast is the right cast in that case. Using dynamic_cast, as proposed by one commenter, will tell the readers and users of GetVar that MyBase::GetVar() could return a pointer to an object not of type MyVar. But that doesn't reflect our intention, as you only ever pass MyVar. To be consequent is the most important thing in software development. What you could do is to assert it is non-null. It will abort at runtime with an error-message in debug-builds of your project:

MyVar* GetVar() { 
    assert(dynamic_cast<MyVar*>(MyBase::GetVar()) != 0);
    return static_cast<MyVar*>(MyBase::GetVar()); 
}

这篇关于在C ++中覆盖成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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