English 中文(简体)
我能否教授活力——新trick子?
原标题:Can I teach dynamic_cast<>() new tricks?

在C++中,有没有办法构建你的班级,这样,你可以给你的班子打一个点子,告诉他们有活力的——播种;以及,如何投给你正在总结执行情况的另一班子? 经营者是否 cast? Imagine I有一个摘要接口基级,得出具体结果。 以及具体B,但具体 B 核对了与具体类型A的接口。 如果我收到请求,请具体谈谈 我从具体A处工作:

class Abstract {
public:
  virtual void interface() = 0;

};

class concreteA : public Abstract {
public:
  virtual void interface();

};

class concreteB : public Abstract {
public:
  concreteB(concreteA &underlying)
    : _underlying(&underlying) {
  }
  virtual void interface();

  operator concreteA*() {
    return _underlying;
  }

private:
  concreteA *_underlying;

};

void
myTest() {
  concreteA myClassA;
  concreteB myClassB(myClassA);
  Abstract *abstract = &myClassB;
  concreteA *underlying = dynamic_cast<concreteA *>(abstract);
}
最佳回答

页: 1 一个充满活力的画面告诉汇编者,“我不想完全改变这个目标,我只是想把这个目标看上去,好像是这样,但却没有改变。 如果你必须改变,就归还民族解放军或放弃一个例外。” 动态的投射不会试图代表你进行任何这种转换。 为此,需要<代码>static_cast或boost:lexical_cast

这是因为投放者可以:

  • Reinterpret an existing object in a new way without changing it
  • Change an object in some way in order to coerce it to be another type, such as int -> short, or double -> int.

仅能做one,而不是两者兼有。

关于投放经营人的“双重”性质的更多信息,请见,该文章由Eric Lippert提供,该文章的用心是C#,但大多也适用于C++。

具体来说,见最近的C++0x草案第5.7节——行为从C++03起没有改变。

问题回答

暂无回答




相关问题
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?

热门标签