- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
不可否认,这个问题的标题听起来与你的邻居迈克反复问的问题几乎一模一样。我发现很多问题的措辞相同,但没有一个是我的问题。
首先,对于这个问题的上下文,我想澄清几点:
1,c++访问控制是基于类而不是基于实例。因此,下面的代码是完全有效的。
class Base
{
protected:
int b_;
public:
bool IsEqual(const Base& another) const
{
return another.b_ == b_; // access another instance's protected member
}
};
2,我完全理解为什么以下代码无效 - 另一个可以是兄弟实例。
class Derived : public Base
{
public:
// to correct the problem, change the Base& to Derived&
bool IsEqual_Another(const Base& another) const
{
return another.b_ == b_;
}
};
现在是时候提出我真正的问题了:
假设在 Derived 类中,我有一个 Base 实例数组。所以实际上,Derived 是一个 Base(IS-A 关系),而 Derived 由 Base(复合关系)组成。我从某个地方读到这(指的是 IS-A 和 Has-A 的设计)是一种设计味道,我一开始就不应该有这样的场景。好吧,例如,分形的数学概念可以通过 IS-A 和 Has-A 关系来建模。但是,让我们暂时忽略对设计的意见,只关注技术问题。
class Derived : public Base
{
protected:
Base base_;
public:
bool IsEqual_Another(const Derived& another) const
{
return another.b_ == b_;
}
void TestFunc()
{
int b = base_.b_; // fail here
}
};
报错信息已经很清楚的说明错误了,所以你的回答就不用再重复了:
Main.cpp:140:7: error: ‘int Base::b_’ is protected int b_; ^ Main.cpp:162:22: error: within this context int b = base_.b_;
实际上,根据以下两个事实,上面的代码应该可以工作:
1,C++ 访问控制基于类而不是基于实例(因此,请不要说我只能访问 Derived 的 b_;我不能访问独立的 Base 实例的 protected 成员 - 它基于类) .
2,错误消息说“在此上下文中”——上下文是派生的(我试图从派生中访问 Base 实例的 protected 成员。这是 protected 成员的特性——它应该能够从在 Base 或从 Base 派生的任何东西中。
那么为什么编译器给我这个错误?
最佳答案
访问规则原则上可以为这种特殊情况提供豁免,其中已知 Base
是最派生类,即对象的动态类型。但这会使事情变得复杂。 C++ 已经足够复杂了。
一个简单的解决方法是在 Base
中提供一个 static
protected
访问器函数。
一个更 hack'ish 的解决方法是使用成员指针臭名昭著的类型系统漏洞。但如果必须坚持基本设计,我会选择 static
函数。因为我认为当生成的代码一开始就很难正确,而且维护人员也很难理解时,节省几次击键没有多大意义。
具体例子:
class Base
{
protected:
int b_;
static
auto b_of( Base& o )
-> int&
{ return o.b; }
public:
auto IsEqual( const Base& another ) const
-> bool
{
return another.b_ == b_; // access another instance's protected member
}
};
关于C++ 保护 : fail to access base's protected member from within derived class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588930/
如果我不定义自己的构造函数,Base *b = new Base; 与 Base *b = new Base(); 之间有什么区别吗? 最佳答案 初始化是标准中要遵循的一种 PITA...然而,这两个
是否有现成的函数可以在 C# 中进行基本转换?我希望将以 26 为基数和以 27 为基数的数字转换为以 10 为基数。我可以在纸上完成,但我不是一个非常有经验的程序员,如果可能的话,我宁愿不要从头开始
JNA 中'base'是什么意思 Pointer.getPointerArray(long base) Pointer.getStringArray(long base) ? JNA Document
我正在做一个将数字从 10 进制转换为 2 进制的基本程序。我得到了这段代码: #include #include #include #include using namespace std;
“假设以下代码: public class MultiplasHerancas { static GrandFather grandFather = new GrandFather();
当我分析算法的时候,我突然问自己这个问题,如果我们有三元计算机时间复杂度会更便宜吗?还是有任何基础可以让我们构建计算机,这样时间复杂度分析就无关紧要了?我在互联网上找不到太多,但是基于三元的计算机在给
一个简化的场景。三个类,GrandParent,Parent 和 Child。我想要做的是利用 GrandParent 和 Parent 构造函数来初始化一个 Child 实例。 class Gran
我编写了一个简单的函数来将基数为 10 的数字转换为二进制数。我编写的函数是我使用我所知道的简单工具的最佳尝试。我已经在这个网站上查找了如何执行此操作的其他方法,但我还不太了解它。我确定我编写的函数非
我尝试了以下代码将数字从 base-10 转换为另一个 base。如果目标基地中没有零(0),它就会工作。检查 79 和 3 并正确打印正确的 2221。现在尝试数字 19 和 3,结果将是 21 而
这个问题在这里已经有了答案: Is Big O(logn) log base e? (7 个答案) 关闭 8 年前。 Intro 练习 4.4.6 的大多数解决方案。算法第三版说,n*log3(n)
如何判断基类(B)的指针是否(多态)重写了基类的某个虚函数? class B{ public: int aField=0; virtual void f(){}; }; class C
我测试了这样的代码: class A { public A() { } public virtual void Test () { Console.WriteL
两者都采用相同的概念:定义一些行和列并将内容添加到特定位置。但是 Grid 是最常见的 WPF 布局容器,而 html 中基于表格的布局是 very controversial .那么,为什么 WPF
我试图在 JS 中“获得”继承。我刚刚发现了一种基本上可以将所有属性从一个对象复制到另一个对象的简洁方法: function Person(name){ this.name="Mr or Miss
class A { public override int GetHashCode() { return 1; } } class B : A { pu
我有一个 Base32 信息哈希。例如IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ ,我需要将其转换为base16。 我怎样才能用 PHP 做到这一点? 我的代码如下所示: $ha
我已经使用其实验界面对 Google Analytics 进行了一些实验,一切似乎都运行良好,但我无法找到 Google Analytics 属性如何达到变体目标的答案,即归因 session - 基
if (state is NoteInitial || state is NewNote) return ListView.builder(
MSVC、Clang 和 GCC 不同意此代码: struct Base { int x; }; struct Der1 : public Base {}; struct Der2 : public
我已经尝试构建一个 Base 10 到 Base 2 转换器... var baseTen = window.prompt("Put a number from Base 10 to conver
我是一名优秀的程序员,十分优秀!