嵌套类继承自嵌套类 [英] nested class inherits from nested class

查看:108
本文介绍了嵌套类继承自嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望C类从A类继承其虚拟函数,而D类(C类中的嵌套类)从B类(A类中的嵌套类)继承其数据字段,这就是我所拥有的.

I wanted the class C inherits from class A its virtual functions, and class D(nested class in class C) inherits from class B(nested class in class A) its data fields, here is what I have.

file1.h

class A{
 public:
     virtual void foo()=0;
     class B{
       public:
         int data;
     };
};

file2.h

class C : public A{
 public:
     class D : public A::B{

     };    
};

file2.cpp

file2.cpp

void C::foo(){//code}
C::D::D():data(0){//Bad initialization list, error being data is not a member of C::D even it should inherits from its base class
 data = 0; //good! compiler can see data is a member of A::B,and C::D inherits from it
}

我有两个问题,第一个是我正在采取正确的方法来实现这种继承.其次,正如我所说,为什么编译器可以在手动初始化过程中看到来自A :: B的数据,却看不到初始化列表中的数据?他们不应该在同一个范围内吗?非常感谢

I got two questions, first one is that is what I am doing the correct way to achieve this kind of inheritance. Secondly, as I commented, why compiler can see data is from A::B in the manual initialization process but not in the initialization list? Shouldn't them be in the same scope? Thank you very much

编辑1 :

因此,如果类C :: D(foo)不直接继承自A :: B(foo),而C继承自A,则我的理解是,由于C继承自A及其所有公共字段,包括其内部类A :: B(foo),D(foo)的名称与A :: B(foo)完全相同,并且是C的内部类,例如,对于两个内部类都使用foo

So if class C::D(foo) doesn't directly inherits from A::B(foo), but C inherits from A, my perception is that since C inherits from A and all its public fields, including its inner class A::B(foo), D(foo) has the exactly same name as A::B(foo) and is an inner class of C, like this, i.e used foo for both inner classes

class A{
 public:
     class foo{
       public:
         int data;
     };
};

class C : public A{
 public:
     class foo{

     };    
};

当我直接调用C :: foo时,会使编译器感到困惑吗?因为在范围内有两个名称为构造函数的构造函数?还是选择将其称为最近的",例如C:foo?而不是爬上继承链?非常感谢

Would it be confusing for the compiler when I call the C::foo directly? since there are two constructors with the name in the scope? or it chooses to call the "nearest" one, e.g C:foo? instead of climbing up the inheritance chain? Thank you very much

推荐答案

  1. 是的,您的方法是实现这种继承的正确方法.

  1. Yes, your approach is the correct way to achieve this kind of inheritance.

初始化程序列表用于控制传递给构造函数的参数.您可以将参数传递给B的构造函数,但不能直接初始化B的成员(这是其构造函数的工作).如果没有为基类或成员指定构造函数,则使用默认构造函数. 在您的情况下,向B添加一个构造函数即可实现所需的功能.

Initializer lists are there to control the arguments passed to the constructor. You can pass arguments to B's constructor, but not directly initialize a member of B (it's the job of its constructor). If there is no constructor specified for a base class or a member, the default constructor is used. In your case, add a constructor to B to achieve what you want.

class A {
public:
    class B{
    public:
        B(int i) : data(i) {}
        int data;
    };
}; 

class C : public A {
    class D : public A::B {
    };
};

C::D::D() :B(0) { }

这篇关于嵌套类继承自嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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