父类可以访问C ++中派生类的对象吗?

Can objects of derived classes in C++ be accessible by parent class?

规则规定,在公共说明符的情况下,派生类的对象可以被视为基类的对象(反之亦然)。那是什么意思?

所有公共元素(在任何类中)都可以被任何人/任何地方访问吗?

这是否意味着对于定义为public的父类属性和方法,派生类可以访问这些属性和方法。但是,如果派生类的属性和方法是公共的,那么父类/基类就不能访问它们?

1
2
3
4
5
In a public base class
Base class members                   How inherited base class members appear in derived class
private:   x ------------------------> x is inaccessible
protected: y ------------------------> protected: y
public:    z ------------------------> public: z

但反过来呢?

  • 如何从父类方法的代码访问子类(非继承的、非虚拟的)成员?
  • 如果我有一个class Circle。我举了一个例子,访问公共属性radius的circle r;就是r.radius。如果我在我的主要部分中写了这个,那么属性必须是公共的。如果我想在派生类中访问同一个radius,为什么我不能这样做呢?因为该方法/成员是公共的?
  • 答案很简单,是的,他们可以。更好的问题是"他们应该是吗?".事实上,这被认为是闻起来很像一个设计,需要重新考虑,除了在一些非常罕见和特殊的情况下,甚至可能不会…
  • 他们可能不应该这样做,但我正试图理解所有适用的规则——可以做什么——所以所有的场景都被覆盖了。这就是为什么我不确定上面的反之亦然的说法。


你问:

Can objects of derived classes in C++ be accessible by parent class?

对。下面是我见过几次的模式。

你还问:

Can all public elements (in any class) be accessible by anything/anywhere?

是的,当然。

访问派生类成员的基类的示例模式

实体H:

1
2
3
4
5
6
7
8
9
10
11
12
class Attribute;

class Entity
{
   public:
      Entity(Attribute* att);
      void save(FILE* fp);
      void print();

   private:
      Attribute* att_;
};

属性.h:

1
2
3
4
5
6
7
8
9
#include"Entity.h"

class Attribute : public Entity
{
   public:
      void save(FILE* fp);
      void print();

};

实体.cc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include"Entity.h"
#include"Attribute.h" // Needed to access Attribute member functions

Entity::Entity(Attribute* att) : att_(att) {}

void Entity::save(FILE* fp)
{
   // Save its own data.
   //...

   // Save the Attribute
   att_->save(fp);
}

void Entity::print()
{
   // Print its own data to stdout.
   //...

   // Print the Attribute
   att_->print();
}

我想正确的问题是"C++中派生类的成员(不是对象)是否可以被父类访问?"

派生类可以访问基类的公共成员和受保护成员(数据成员和函数)。

类的公共成员可以从任何地方访问。当然,它们需要通过该类的一个实例来访问。

But in the case that the attributes and methods from the derived class are public theyaren't accessible by the parent/base class?

如果您有派生类的实例,则可以是。然后可以从基类访问派生类的公共成员。

编辑:

例如,基类成员访问派生类的公共成员,反之亦然。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Base
{
    public:
    void funcBase();
};

class Derived : public Base
{
    public:
    void funcDerived();

};

void
Base::funcBase()
{
    Derived d;
    d.funcDerived();
}

void
Derived::funcDerived()
{
    Base b;
    b.funcBase();
}

int main()
{
    Base b;
    Derived d;

    b.funcBase();
    d.funcDerived();
}

  • 你说一个类的公共成员可以从任何地方访问。在这种情况下,派生类中的方法不能访问父类的成员。这就是我困惑的地方。
  • 他们当然可以。我就是这么说的。下面是一个示例,其中派生类成员访问基类的公共成员,而基类成员访问派生类的公共成员。class base public:void funcbase();class derived:public base public:void funcderived();void base::funcbase()derived d;d.funcderived();void derived::funcderived()base b;b.funcbase();int main()base b;derived d;b.funcbase();d.funcderived();
  • 我懂了。因此,基类可以访问派生类中的方法。谢谢您。


这意味着如果你有Bar,它是Foo的派生类。

1
2
3
class Foo

class Bar : public Foo

所以你可以说,正如预期的那样

1
2
Foo* myFoo = new Foo;
Bar* myBar = new Bar;

你也可以用Bar制作Foo,因为BarFoo的一种。

1
Foo* myOtherFoo = new Bar;

不能从Foo中生成Bar,这就是"派生类的对象可以被视为基类的对象(反之亦然)"的意思。

1
Bar* myOtherBar = new Foo;

  • 所以如果我理解正确的话,你就不能从foo进入酒吧。因为FOO本质上不是一种酒吧。但我的困惑是在公共变量的情况下。如果某个东西是公共的,它可以从主函数或任何其他随机函数访问。为什么一个随机的函数访问条不能从foo来呢?
  • 因为它不知道它实际上是一个Bar。当我说Foo* myOtherFoo = new Bar;时,公开使用myOtherFoo的人只知道它是Foo。它也是一个Bar,但没有人知道。对于Bar* myBar = new Bar;,您只能将其视为Bar,因为这实际上是指向Bar的指针。