简体   繁体   English

友谊与传承

[英]Friendship and inheritance

I'm working on a small project and i'm sort of stuck because I don't really understand how friendship and inheritance interact with each other.我正在做一个小项目,我有点卡住了,因为我真的不明白友谊和继承是如何相互作用的。 I'll show you some sample code.我将向您展示一些示例代码。

namespace a
{
    class Foo
    {
    public:
        Foo(int x) : m_x(x) {}
    protected:
        friend class b::Derived;
        friend class a::Base;
        int m_x;
    };

    class Base
    {
    public:
        Base(Foo foo) : m_foo(foo) {}
    protected:
        Foo m_foo;
    };
}
namespace b
{
    class Derived : public a::Base
    {
    public:
        Derived(a::Foo foo)
            : Base(foo)
        {
            m_foo.m_x;
        }
    };
}
e0265: at line 29: member a::Foo::m_x (declared at line 10) is inaccessible

Apparently Derived can't access protected members of Foo, seemingly because Derived::m_foo is a derived member, so constructing Derived will fail.显然 Derived 无法访问 Foo 的受保护成员,似乎是因为 Derived::m_foo 是派生成员,因此构造 Derived 将失败。 Can anyone explain this in detail to me?谁能给我详细解释一下?

Apparently Derived can't access private members of Foo, seemingly because Derived::m_foo is a derived member, so constructing Derived will fail.显然 Derived 不能访问 Foo 的私有成员,貌似是因为 Derived::m_foo 是派生成员,所以构造 Derived 会失败。

Sorry, this is not an apparent misunderstanding of friend.抱歉,这不是对朋友的明显误解。

A friend class can access any attribute.友元类可以访问任何属性。

You have an unrelated coding error ... the comments reveal that you are missing an initialization (of Base::m_foo) in Base.您有一个不相关的编码错误……注释表明您缺少 Base 中的初始化(Base::m_foo)。 Fix that, add a few data items to Foo, then run your demo:解决这个问题,向 Foo 添加一些数据项,然后运行您的演示:

#include <iostream>
using std::cout, std::endl; 

class Foo
{
public:
   Foo(int x): m_x(x){}
   ~Foo(){}

   int m_z;   // add public

protected:
   int m_y;   // add protected

private:      // change to private
   friend class Derived;
   int m_x;
};

class Base
{
public:
   Base() : m_foo(0) // add m_foo Initialization (with 0)
      {}
   virtual ~Base(){}
protected:
   Foo m_foo;
};

class Derived : public Base
{
public:
   Derived(Foo foo)
      {
         foo.m_y   = 11;
         foo.m_z   = 22;
         std::cout << foo.m_x << "   "
                   << foo.m_y << "   "
                   << foo.m_z << std::endl;  //friend class can access
      }
};

class T914_t // ctor and dtor compiler provided defaults
{
public:
   int operator()(int argc, char* argv[]) { return exec(argc, argv);  }

private: // methods

   int exec(int , char** )
      {
         Foo     f(99);
         Derived d(f);

         return 0;
      }

}; // class T914_t

int main(int argc, char* argv[]) { return T914_t()(argc, argv); } // call functor

Typical output from class Derived ctor accessing private, protected, and public data attributes:类派生 ctor 访问私有、受保护和公共数据属性的典型输出:

99 11 22 99 11 22

I found the problem.我发现了问题。 The namespace b and therefore the class derived were not visible to the friend declaration in Foo.命名空间 b 以及派生的类对于 Foo 中的友元声明是不可见的。 When I Forward declared b and derived everything worked as intended and derived could access private/protected members.当我转发声明 b 并派生时,一切都按预期工作,派生可以访问私有/受保护的成员。

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

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