派生类函数

发布于 2024-09-28 12:02:20 字数 881 浏览 2 评论 0原文

class Base
{
   protected: 
      int data;
   public:
      virtual int getData() { return data; }
      virtual void setData(int value) { data = value; }
};

class Child : protected Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();  // Line 140, Error
      }
};

使用 Worker 类编译文件时出错:

Base.hpp: In member function ‘int Worker::getGlobalData()’:
Base.hpp:22:19: error: ‘virtual int Base::getData()’ is inaccessible
Worker.cpp:140:34: error: within this context
Worker.cpp:140:34: error: ‘Base’ is not an accessible base of ‘Child’
class Base
{
   protected: 
      int data;
   public:
      virtual int getData() { return data; }
      virtual void setData(int value) { data = value; }
};

class Child : protected Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();  // Line 140, Error
      }
};

Error during compiling of file with Worker class:

Base.hpp: In member function ‘int Worker::getGlobalData()’:
Base.hpp:22:19: error: ‘virtual int Base::getData()’ is inaccessible
Worker.cpp:140:34: error: within this context
Worker.cpp:140:34: error: ‘Base’ is not an accessible base of ‘Child’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

゛时过境迁 2024-10-05 12:02:20

您真的将其设为公共基类吗?

//            vvvvvv important
class Child : public Base

否则它是私有的,并且您会收到与您所遇到的类似的错误,即:

“Base”不是“Child”的可访问基础

Did you actually make it a public base class?

//            vvvvvv important
class Child : public Base

Otherwise it's private, and you get errors similar to what you have, namely:

‘Base’ is not an accessible base of ‘Child’

挽清梦 2024-10-05 12:02:20

这编译:

class Base
{
   protected: 
      int data;
   public:
      virtual int getData() {return data;}
      virtual void setData(int value) { data = value; }
};

class Child : public Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();
      }
};

This compiles:

class Base
{
   protected: 
      int data;
   public:
      virtual int getData() {return data;}
      virtual void setData(int value) { data = value; }
};

class Child : public Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();
      }
};
給妳壹絲溫柔 2024-10-05 12:02:20

除了类->阶级与美德 -> virtual 你的代码完全没问题。绝对没有什么问题。因为从字面上看,由于拼写错误,这不会编译,我怀疑您的原始代码有点不同,因为从 Base 派生 Child 是私有的,或者 getData 是私有的。

Except for clas -> class and virtua -> virtual your code is perfectly fine. There is absolutely nothing wrong with it. Since literally this doesn't compile because of the typos I suspect your original code was a bit different in that the derivation of Child from Base was private or that getData was private.

橘和柠 2024-10-05 12:02:20

class Worker 不是 clas Worker

Worker 类中,obj.setData(val); 正在尝试访问私有Child 类的成员。

class Worker not clas Worker

In Worker class, obj.setData(val); is attempting to access a private member of Child class.

渡你暖光 2024-10-05 12:02:20

由于当前现有版本的代码通过 protected 继承将 Child 子类化为 Base,因此 public 函数位于 >BaseChild 中成为受保护,因此 Worker 无法通过 Child 对象访问它们。

Since the current existing version of the code has Child subclassing Base via protected inheritance, the public functions in Base become protected in Child, so Worker can not access them via Child object.

南冥有猫 2024-10-05 12:02:20

Child 类继承了 Base 类作为 protected,因此成员函数 Child::getData() 不可公开访问给 Child 的客户。

正如这里其他人所说,将 Base 的继承更改为 public 是解决此问题的一种方法。

class Child: public Base

另请注意,将 Child 对象转换为 Base 类型也会使该对象的 Base::getData() 函数可公开访问。

return static_cast<Base *>(&obj)->getData();

Class Child inherits class Base as protected, so the member function Child::getData() is not publically accessible to clients of Child.

As everyone else here said, changing the inheritance of Base to public is one way to fix it.

class Child: public Base

Also note that casting your Child object to type Base also makes the function Base::getData() of the object publically accessible.

return static_cast<Base *>(&obj)->getData();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文