我有一个关于c ++ 11指针的问题.具体来说,如何将基类的唯一指针转换为派生类?
class Base
{
public:
int foo;
}
class Derived : public Base
{
public:
int bar;
}
...
std::unique_ptr<Base> basePointer(new Derived);
// now, how do I access the bar member?
Run Code Online (Sandbox Code Playgroud)
它应该是可能的,但我无法弄清楚如何.每次我尝试使用
basePointer.get()
Run Code Online (Sandbox Code Playgroud)
我最终得到了可执行文件崩溃.
在此先感谢,任何建议将不胜感激.
保存文件时,我试图禁止vscode格式化我的python导入。我有一些必须在各种导入之间运行的代码,因此顺序很重要,但是每次保存时,都只会将导入推到顶部。
我试着把
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
Run Code Online (Sandbox Code Playgroud)
在我的用户设置中,但这并不能解决问题。
谢谢!
编辑-我想保持格式上保存,除了导入
对不起,如果以前出现过,但我没有看到另一个关于此的帖子.但如果有,请给我链接.
在这里,我有一个简单的程序.当它运行时,打印出"6 4"
public static void main(String[] args) {
Foo foo = new Foo();
foo.t = 6;
foo.f = 4;
Bar b = new Foo();
ArrayList<Bar> list = new ArrayList<Bar>();
list.add((Bar) foo);
Foo foo2 = (Foo) list.get(0);
System.out.println(foo2.t + " " + foo2.f);
}
static class Bar {
int t;
}
static class Foo extends Bar {
int f;
}
Run Code Online (Sandbox Code Playgroud)
这看起来很奇怪,因为我认为当我将Foo添加到列表中时,它会删除f字段,因为列表中包含没有f的Bars.
但似乎f在被投入酒吧之后仍然存在.有人可以解释为什么会这样吗?