- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
与此错误消息的其他示例不同,我已经有一个指向 A
的指针并且想要检索实际的子类。
这种安排是一些 C++ 包装的 C 代码的一部分 A
是一些 POD C 结构(whatswhy 没有动态转换)而 test
是 C 中的一些回调调用 C++ 功能并检索应使用强制转换的正确对象。但是为了防止 C++ 用户代码弄乱 C-Baseclass,我希望继承 protected
。
MSVC 不会提示这个但 g++ 会提示!?从标准的角度来看,哪一个是正确的?为什么?
#include <iostream>
using namespace std;
// plain C structure
struct A{
int i;
};
// some C++ Wrapper class
struct B: protected A{
A* get() { return this; }
void print(){cout << i << endl;}
};
extern "C" {
// C callback that gives it this pointer
void test(A* io_self){
auto b2 = static_cast<B*>(io_self);
b2->print();
}
}
int main()
{
B b;
test(b.get());
return 0;
}
给出:
$g++ -std=c++11 -o main *.cpp
main.cpp: In function ‘void test(A*)’:
main.cpp:21:43: error: ‘A’ is an inaccessible base of ‘B’
auto b2 = static_cast<B*>(io_self);
^
最佳答案
来自 c++11 N3337 草案(有点旧,但它是我身边的那个)5.2.9/11 (static_cast):
A prvalue of type “pointer to cv1 B,” where B is a class type, can be converted to a prvalue of type “pointer to cv2 D,” where D is a class derived (Clause 10) from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists (4.10), cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B is neither a virtual base class of D nor a base class of a virtual base class of D.
在这种情况下,由于您使用了 protected
继承,因此没有从 B
到 A
的有效标准转换,因此您的 static_cast
是非法的(g++ 诊断它是正确的)。
在这种情况下,由于您提供了一个围绕 C API 的 C++ 包装器,我认为最简单的方法是坚持公共(public)继承,并相信您的用户不会直接滥用 C API 如果他们已经有意识地选择使用您的 C++ API
关于c++ - 沮丧 : why: ‘A’ is an inaccessible base of ‘B’ ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47909196/
我正在构建一个包含大约 30 个表单的应用程序。我需要管理 session ,因此我希望有一个可从所有表单访问的全局 LoggedInUser 变量。我读过“David Heffernan”关于全局变
有的朋友在开机或是使用电脑时会遇到inaccessible boot device的提示,从而无法正常进入系统。这可能是因为我们的操作导致系统出现了损坏,我们可以尝试进入WinRE来修复问题或重置系
谁能解释一下这个屏幕: 如您所见,我有一个简单的数组 - 当我使用 A[0], A 1 时,调试器显示其元素的值...但我无法获取具有本地变量 A[i] 的元素(但“i”的值正确显示)。 项目设置:
我有这两个类: class Hand { public: int getTotal(); std::vector& getCards(); void add(Card& car
我收到此错误,但我认为只有在成员(member)的保护级别太高且无法访问时才会收到此错误,但我还是收到了。 Shopable.h: #ifndef _SHOPABLE_H_ #define _SHOP
我遇到了一个非常奇怪的错误,因为它只发生在从生成的 .apk 安装应用程序之后。当我尝试通过 IDE 运行该应用程序时,它工作正常。 java.lang.IllegalAccessError: Met
我在以下代码中遇到了一个奇怪的(或者可能不是)错误: template class Registrer { public: Registrer() { Registry::re
与此错误消息的其他示例不同,我已经有一个指向 A 的指针并且想要检索实际的子类。 这种安排是一些 C++ 包装的 C 代码的一部分 A 是一些 POD C 结构(whatswhy 没有动态转换)而 t
我尝试通过以下方式实现单例类(我使用 VS2008 SP1): namespace firstNamespace { class SingletonClass { privat
有人可以帮我解释为什么当底层函数中引发异常时 TStrings 会作为“不可访问的值”返回吗? function GetStrings():TStrings; begin result := TS
(在 Azure 门户中报告支持问题时,似乎没有选择 Application Insights 的选项,因此发布了这篇文章) 在大约 12 小时内,当我尝试查看特定应用服务的 Application
我已经阅读了一些有关此问题的信息,但没有得到任何有帮助的答案,就我而言,我在if (!_positioner.GetPosition(ref position, _content, contentSi
(在 Azure 门户中报告支持问题时,似乎没有选择 Application Insights 的选项,因此发布了这篇文章) 在大约 12 小时内,当我尝试查看特定应用服务的 Application
我正在尝试一个多态性示例,但我的代码中出现以下错误: public class CPolygon { CPolygon() {} public int width {get; set;
我有这段代码,并产生了错误,只是因为我已经添加到它的类的构造函数中。 class NestedClass { class A { A() {} } class B
在对许多示例(使用 Closure Compiler)进行缩小后,指令内链接函数中的访问范围是“未定义的”,但在缩小前工作正常。 例如,Angular 教程中的以下代码经过一些改动。缩小后 $scop
当访问修饰符被设置为 private 或 protected 时,为什么我无法创建基类的指针并将其指向子类? #include using namespace std; class father {
子类“caesar”的构造函数出错。它说名称,类型由于其保护级别而无法访问。怎么来的?因为这是从“Cipher”类派生的子类,所以它不应该给出这样的错误。我怎样才能克服这种情况。但我希望这些变量是私有
我有一个代码如下- #include #include class A{ int a; public: virtual void sayHello(){ std::cout say
即使构建配置设置为“调试”并且优化为 False,我也会收到“由于优化,此处无法访问所有用户的变量”。所以,我无法调试我的程序。 为什么我会得到这个? 当我按下“运行”按钮时会运行哪个版本? 怎么才能
我是一名优秀的程序员,十分优秀!