我在采访中被问到这个问题,我不确定以下情况下的行为:
class A
{
virtual fun1(){...}
virtual fun2(){...}
};
class B : public A
{
virtual fun1(){...}
virtual fun2(){...}
};
Run Code Online (Sandbox Code Playgroud)
现在如果,
A* AObj = new A;
B* BObj = (B*) AObj;
Run Code Online (Sandbox Code Playgroud)
是否BObj因为 virtual 关键字而可以访问 B 的方法,或者是否因为它指向 的对象而不能访问AObj?
有人可以帮助我了解向下转型到底如何影响访问吗?
我是一个喜欢深入细节的人。这次我创建了非常简单的功能,我称之为“场景”(查看代码)。首先给大家介绍一下我的看法:
struct ScenarioContext
{ virtual ~ScenarioContext() = default; };
struct IScenarioStep
{
virtual ~IScenarioStep() = default;
virtual void run( ScenarioContext& ) = 0;
};
struct ScenarioContainer final
{
std::list<std::unique_ptr<IScenarioStep>> m_scenarioStepList;
};
struct Scenario
{
explicit Scenario( ScenarioContainer&&, std::unique_ptr<ScenarioContext>&& = nullptr );
void execute(); // Runs the steps one by one and passes context ref to steps
std::unique_ptr<ScenarioContext> m_context;
ScenarioContainer m_container;
};
Run Code Online (Sandbox Code Playgroud)
现在示例“ScenarioStep”实现:
struct SimpleContext
: ScenarioContext
{
bool isFirstStepDone = false;
bool isSecondStepDone = false;
bool isThirdStepDone = false;
}; …Run Code Online (Sandbox Code Playgroud) class Base {
};
class Derived: private Base {
};
int main() {
Derived d;
static_cast<Base>(d);
}
Run Code Online (Sandbox Code Playgroud)
我知道由于私有继承,这样的强制转换是一个错误。然而,我感兴趣的是为什么错误消息是:
error: cannot cast 'const Derived' to its private base class 'const Base'
Run Code Online (Sandbox Code Playgroud)
特别是,为什么它不将“Derived”转换为“Base”?为什么这里有一个const?提前致谢。
我想知道是否有人可以为我解释关于一个类演员的事情.
我正在玩Android,我有一个名为ExApp的Application子类.
我想从我的一个活动中调用ExApp的方法,所以我这样做:
ExApp ex = ((ExApp)getapplication());
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我需要一套双括号?为什么我不能:
ExApp ex = (ExApp)getApplication();
Run Code Online (Sandbox Code Playgroud)
?
谢谢.
我只是想知道如何使用子类来实现超类,例如.
class Animal {
void poo() {
System.out.println("general poo");
}
}
class Horse extends Animal{
void poo() {
System.out.println("horse poo");
}
}
Animal animal1 = new Horse();
// using this I get the implementation of the Horse class's methods
animal1.poo(); //should return horse poo
Run Code Online (Sandbox Code Playgroud)
试图升级它以获得超级类实现,但无济于事
((Animal)animal1).poo() // still returns the horse class's implementation of the method
Run Code Online (Sandbox Code Playgroud)
如何使用animal1对象获取超类实现?
尽管dynamic_cast返回a 0如果正在处理的指针是不兼容的类型,为什么你会避免使用dynamic_cast?
我有以下内容:
class A { ... };
class B : public A { ... };
// ...
B b;
const A& aref(b);
// ...
const B& bref(aref);
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到:
no suitable user-defined conversion from "const A" to "const B" exists
Run Code Online (Sandbox Code Playgroud)
现在,如果这些是指针而不是引用,我会使用
bptr = dynamic_cast<B*>(aptr);
Run Code Online (Sandbox Code Playgroud)
但参考文献没有.我该怎么办?切换到指针?别的什么?
public class A
{
public void printA(){
System.out.println("A");
}
}
public class B extends A
{
public void printB(){
System.out.println("B");
}
}
public class C extends B
{
public void printC(){
System.out.println("C");
}
}
public class test {
public static void main(String[] args)
{
A a = new B();
a.printA(); // work
B b = (B) a;
b.printB(); // work
C c = (C) b;
c.printC(); // not work throw java.lang.ClassCastException
}
}
Run Code Online (Sandbox Code Playgroud)
我有三个A类和B类,C
C从B延伸,B从A延伸到A
为什么从A到B,而不是从B到C,尽管它们之间的关系如A和B,B是父的C那么JVM怎么工作?
正如标题所说,如果我将指向基类的指针转换为派生类,当指针为空时,从C++ 11/C++ 14标准的角度来看,这是一个安全的操作吗?
struct base
{
virtual ~base() = default;
};
struct derived : base {};
struct wrapper
{
using allowed_derived_t = derived;
base* base_ptr = nullptr;
void set_ptr(base* ptr)
{
if (!dynamic_cast<allowed_derived_t*>(ptr))
throw std::logic_error("Check your user code");
base_ptr = ptr;
}
allowed_derived_t* ptr() const
{ return static_cast<allowed_derived_t*>(base_ptr); }
};
Run Code Online (Sandbox Code Playgroud)
ptr()如果我在打电话之前打电话给它是安全的方法set_ptr吗?因为,在设置指针之前,base_ptr它不是必需的类型(allowed_derived_t),但是,动态指向对象不是错误的类型(因为没有指向对象).
标准在那种情况下说了什么?
我将以下代码中的Model2实例上转换为Object类,然后在Model1类的测试方法中向下转换回类-Model2 。但是thenUov在向下转换后其属性值显示为null。实例在Upcasting-then-Downcasting之后松开其状态时,是否期望这样做?能否请您分享一些细节。另外,是否有一种方法可以在上传后保留实例的状态?
public class Demo{
public static void main(String[] args) {
Model1 m1 = new Model1();
m1.setAttr1(10);
Model2 m2 = new Model2();
m2.setAttr2(10);
m1.test(m2);
}
}
class Model1 {
private Integer attr1;
public Integer getAttr1() {
return attr1;
}
public void setAttr1(Integer attr1) {
this.attr1 = attr1;
}
public void test(Object o) {
Model2 m = (Model2) o;
System.out.println(m.getAttr2());
}
}
class Model2 {
private Integer attr2;
public Integer getAttr2() {
return attr2;
}
public void setAttr2(Integer attr1) …Run Code Online (Sandbox Code Playgroud) downcast ×10
c++ ×6
java ×4
casting ×3
inheritance ×3
polymorphism ×2
upcasting ×2
android ×1
dynamic-cast ×1
performance ×1
pointers ×1
reference ×1
types ×1