Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在派生类中定义嵌套类成员函数_C++_Inheritance_Composition - Fatal编程技术网

C++ 无法在派生类中定义嵌套类成员函数

C++ 无法在派生类中定义嵌套类成员函数,c++,inheritance,composition,C++,Inheritance,Composition,我有一个由另一个类组成的基类(我们称之为Component)。如果我从基类继承,是否可以向组件类添加功能(假设您不能修改基类代码)?我基本上想要一个“派生::组件::foo”函数 #include <iostream> class Base { public: void Print() { std::cout << c.data; } class Component { public:

我有一个由另一个类组成的基类(我们称之为Component)。如果我从基类继承,是否可以向组件类添加功能(假设您不能修改基类代码)?我基本上想要一个“派生::组件::foo”函数

#include <iostream>

class Base
{
    public:
        void Print() { std::cout << c.data; }

        class Component
        {
            public:
                int data;
        };
        Component c;
};

class Derived : public Base
{
    private:
        void Component::foo(int value) 
        { 
            this->data = value; 
        }
    public:
        void bar(int value)
        {
            this->c.foo(value);
        }
    };

int main() {
    Derived d;
    d.bar(4);
    d.Print();
}
#包括
阶级基础
{
公众:
void Print(){std::cout data=value;
}
公众:
空心条(int值)
{
此->c.foo(值);
}
};
int main(){
导出d;
d、 酒吧(4);
d、 打印();
}
此代码在Ubuntu上的G++4.8下给出以下错误:


错误:无法在“派生”中定义成员函数“Base::Component::foo”

您需要在
Component
类中声明
foo
函数。然后在
组件本身中定义它:

#include <iostream>

class Base
{
public:
    void Print() { std::cout << c.data; }

    class Component
    {   
    public:
        int data;
        void foo( int value )
        { 
            data = value; 
        }
    };  

    Component c;
};

class Derived : public Base
{
private:
public:
    void bar(int value)
    {   
        c.foo(value);
    }   
};

int main() {
    Derived d;
    d.bar(4);
    d.Print();
}
#包括
阶级基础
{
公众:
void Print(){std::cout
[…]添加功能[…](假设您不能修改基本代码)[…]

根据所讨论的实际基类的外观,您可以尝试对
组件进行简单的子类化:

/* using struct to have public accessibility */
struct Base {
  struct Component {
    int data;
    virtual ~Component() {} // ABSOLUTELY NECESSARY
  };
  std::unique_ptr<Component> component; // ABSOLUTELY NECESSARY

  void print(void) {
    std::cout << component->data << endl;
  }
};

/* The decorated component, with the additional functionality */
struct DecoratedComponent : public Base::Component {
  void set(int d) {
    data = d;
  }
};

这仅在基使用引用或某种指针来存储/访问其组件时才有效。此外,如果基管理组件的生存期,则
组件必须具有虚拟析构函数。

这在任何情况下都无效:您需要在类declar中定义函数初始化。您不能在另一个类中定义一个类的成员。是否有方法添加派生::组件::foo成员函数?@ruslank是的,但您需要在
派生的
类中定义
组件
类。
组件
的当前路径仅为(据我所知)通过
Base::Component
,它是在那里定义的。如果我不能访问Base的源代码,这会起作用吗?如果你不能更改
Base
来添加
foo()
声明到
组件,则为否。派生类不能向基的嵌套类添加成员,当然也不能为尚未声明的方法定义实现。谢谢,这种方法有助于扩展组件功能。理想情况下,我们可以将“DecoratedComponent”和其他将基本方法添加到派生类中。有没有一种干净的方法可以做到这一点?@ruslank Hm…你的意思是,如果,例如,你也要将
Base
子类化?那么,你可以将
DecoratedComponent
(声明)作为该子类的成员。
/* using struct to have public accessibility */
struct Base {
  struct Component {
    int data;
    virtual ~Component() {} // ABSOLUTELY NECESSARY
  };
  std::unique_ptr<Component> component; // ABSOLUTELY NECESSARY

  void print(void) {
    std::cout << component->data << endl;
  }
};

/* The decorated component, with the additional functionality */
struct DecoratedComponent : public Base::Component {
  void set(int d) {
    data = d;
  }
};
Base the_base;
auto the_component = std::make_unique<DecoratedComponent>();
// Inject the decorated component
the_base.component = the_component;
the_component.set(42);
the_base.print(); // 42