English 中文(简体)
某类衍生物的点子能否被打入基类的点子?
原标题:Can a pointer of a derived class be type cast to the pointer of its base class?
  • 时间:2010-01-28 16:26:53
  •  标签:
  • c++

以新方式交还的衍生产品,其类型可归入基类点。

这是否真实或虚假?

我知道有活力的预测会被用于 down。 一般来说,如何将一个衍生等级的点推到基级的点子?

最佳回答

是的。 从点子向衍生物类别转换到点子级是默示的。 因此,以下措施完全是罚款:

struct B { };
struct D : B { };

D* my_d_ptr = new D;
B* my_d_ptr_as_a_b_ptr = my_d_ptr;
问题回答

暗指向基点的点人。 这是多吗? 一个衍生产品类别的例子应始终作为基础类别的一个实例安全使用。 因此,没有必要作出明确表述。

如果产阶级公开而非虚拟地继承基数,情况就是这样:

页: 1 (Ccast将做这项工作,但你在使用这一黑客之前应两次思考!)

class Base { };

class Derived : protected Base { };

int main()
{
   Base* b = new Derived(); // compile error
}

如果基类模棱两可,也不会有工作:

class Base { };

class Derived1 : public Base { };
class Derived2 : public Base { };

class MostDerived : public Derived1, Derived2 { };

int main()
{
   Base* b = new MostDerived(); // won t work (but you could hint compiler
                                // which path to use for finding Base
}

Edit: 添加编码样本,添加模棱两可的使用案例,删除了虚拟继承例子。

你的问题并不明确,因为它混杂了几个不同的东西。

一方面,向下级(can)的点子转换为基级(假定基数可以进入)。 这是一种自然的转变,不需要任何东西。

另一方面,请参见<代码>dnamic_cast及其履行的能力>。 但是,降幅投向相反的方向:从点到基级到点到衍生等级。 可根据需要和需要多少时间进行抽查。

...... 与此同时,你谈论的是投下<条码>新的结果,当然,这种结果只能加以预测,但不能降幅。

因此,你问什么? 预测或预测?





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签