且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将派生类对象分配给基类指针

更新时间:2023-02-13 08:40:00

发生此错误的原因是,当派生为protected或private时实例化该对象的任何人都不能访问该基类.可以像其他人建议的那样通过类型转换覆盖此行为,但是不建议这样做,这是预期的行为.

在这里看到一个很好的解释:
http://vapvarun .com/study/softE/john%20wiley%20and%20sons%20-%20programming%20with%20object-oriented%20programming/5399final/lib0164.html [
The reason for this error is because the base class should not be accessible by whomever instantiates the object when derived as protected or private. This behavior can be overriden by typecasting as others have suggested, but it is not recommended and is expected behavior.

See a good explanation here:
http://vapvarun.com/study/softE/john%20wiley%20and%20sons%20-%20programming%20with%20object-oriented%20programming/5399final/lib0164.html[^]

If you want to access the base pointer, then you want a public derivation.


我认为您遇到错误C2243.
由于您是在保护模式下继承A的,因此您无法从B类访问A类零件.请查看链接以获取更多信息.

Msdn(错误信息):
http://msdn.microsoft.com/en-us/library/s5480d1f.aspx [ ^ ]

***:
http://***.com/questions/1471800/getting-rid-of-error-c2243 [^ ]
I think you are getting error C2243.
Since you are inherited A in protected mode you can not access the Class A part from Class B.Check the links for more info.

Msdn(Error info):
http://msdn.microsoft.com/en-us/library/s5480d1f.aspx[^]

***:
http://***.com/questions/1471800/getting-rid-of-error-c2243[^]


错误没有错误C2243出现很多类型转换问题
您可以尝试如下
Error no error C2243 comes for lots of Typecasting issues
You may try like follows
A *ptr;
B *b(30,50);
ptr=(A*)b;