简体   繁体   English

C ++覆盖成员变量

[英]C++ override a member variable

This may be a silly question, but I was wondering if it is possible to override the type of a member variable as long as it is a child type. 这可能是一个愚蠢的问题,但我想知道是否有可能覆盖成员变量的类型,只要它是子类型。

Basically I have a parent class A that has a variable like so: 基本上我有一个父类A,它有一个像这样的变量:

TSubclassOf<AItem> ItemClass;

then in class B which extends class AI was wondering if I could change the type to a different one like so: 然后在扩展类AI的B类中想知道我是否可以将类型更改为不同的类型:

TSubclassOf<AWeapon> ItemClass;

where Weapon extends Item, so it is still a subclass of Item. 其中Weapon扩展Item,因此它仍然是Item的子类。

Is this possible? 这可能吗?

If not, is it possible to hide a variable in a child class? 如果没有,是否可以隐藏子类中的变量? ie I could hide ItemClass and create a new variable called WeaponClass. 即我可以隐藏ItemClass并创建一个名为WeaponClass的新变量。

Thanks in advance for the help 在此先感谢您的帮助

By declaring TSubclassof<AWeapon> ItemClass; 通过声明TSubclassof<AWeapon> ItemClass; in the derived class, you are basically hiding ItemClass in the base class. 在派生类中,您基本上是在基类中隐藏ItemClass So, it will look like you "changed" the type, but you didn't, only in the derived class will you see the new type variable. 所以,它看起来像你“改变”了类型,但你没有,只有在派生类中你会看到新的类型变量。

If you call a member function of the base class, which accesses ItemClass , it will use the "old" type variable. 如果调用访问ItemClass的基类的成员函数,它将使用“旧”类型变量。

If not, is it possible to hide a variable in a child class? 如果没有,是否可以隐藏子类中的变量? ie I could hide ItemClass and create a new variable called WeaponClass. 即我可以隐藏ItemClass并创建一个名为WeaponClass的新变量。

That's possible, you could declare ItemClass as a private variable, so that the derived class doesn't have access to it. 这是可能的,您可以将ItemClass声明为private变量,以便派生类无权访问它。 Then when you can create WeaponClass , without there being a ItemClass in the derived class. 然后,当您可以创建WeaponClass ,派生类中没有ItemClass

It depends, as many things do, on visibility. 在可见度方面,这取决于许多事情。 If ItemClass is private , then class B wouldn't even see it in the first place, so there's no chance whatsoever of a naming clash. 如果ItemClassprivate ,那么B类甚至不会在第一时间看到它,所以命名冲突没有任何机会。 If it's protected , then... things get a little complicated. 如果它protected ,那么...事情变得有点复杂。 It won't throw any errors, but you'll hide the name in the subclass, but not in the superclass. 它不会抛出任何错误,但你会在子类中隐藏名称,但不会在超类中隐藏。 See this bit of code as an example: 以这个代码为例:

#include <iostream>
#include <string>

struct message {
    std::string the_message;
    message() : the_message("default string") {}
    message(std::string a) : the_message(a) {}
    virtual ~message() = default;

    virtual void foo() { std::cout << the_message << std::endl; }
};

struct message_sub : message {
    int the_message;

    message_sub(int a) : the_message(a) {}
};

int main(int, char**) {
    message *a_message = new message_sub(12);
    a_message->foo();
    delete a_message;
}

Try it online 在线尝试

As you can see if you run it, the output is not 12 , as you might expect, but default string . 正如您所看到的,如果您运行它,输出不是12 ,正如您所料,但是default string That's because the superclass method foo will see things in its scope; 那是因为超类方法foo会在范围内看到事物; ie it'll see the version whose name you're hiding in the subclass. 即它会看到你的名字隐藏在子类中的版本。 The only way to fix this problem is to copy-and-paste foo into the subclass, and if it's not virtual in the subclass, there's no way to make it work. 解决这个问题的唯一方法是将foo复制并粘贴到子类中,如果它在子类中不是virtual的,则无法使其工作。

Because of this, I'd advise against whatever you're trying to do here; 因此,我会建议不要你在这里做什么; you can't write a default implementation that uses member variables of the subclasses, only those that exist in the superclass. 您不能编写使用子类成员变量的默认实现,只能编写超类中存在的实现变量。 Two variables with the same name but different types are different variables. 具有相同名称但不同类型的两个变量是不同的变量。 If this is just wondering if you can safely name things whatever you want, yes, you can, especially if they're private . 如果这只是想知道你是否可以安全地命名任何你想要的东西,是的,你可以,特别是如果它们是private

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM