简体   繁体   English

新建对象并将对象的地址分配给其基类指针时,编译器会做什么?

[英]what does compiler do when new an object and assign the address of the created object to its base class pointer

Example 3 :(page 377) 例3 :(第377页)

class A {virtual void f();};
class B {virtual void f(); virtual void g();};
class C: A, B {void f();};
A* pa = new C;
B* pb = new C;
C* pc = new C;
pa->f();
pb->f();
pc->f();
pc->g()

(1) In Multiple inheritance for C++ , Bjarne wrote: On entry to C::f , the this pointer must point to the beginning of the C object (and not to the B part). (1)在C ++的多重继承中 ,Bjarne写道:在进入C::fthis指针必须指向C对象的开头(而不是B部分)。 However, it is not in general known at compile time that the B pointed to by pb is part of a C so the compiler cannot subtract the constant delta(B) . 但是,在编译时通常不知道pb指向的BC一部分,因此编译器无法减去常数delta(B)

Why does the compiler not know that B pointed to by pb is part of a C at compile time? 为什么编译器在编译时不知道pb指向的BC一部分? Based on my understanding, B pointed to by pb is definitely a part of C since we new C and C inherits from B ! 根据我的理解, pb指向的B肯定是C的一部分,因为我们new CC继承自B

What about B* pb = new B; B* pb = new B; ? does the compile know B pointed to by pb is a standlone object? 编译器是否知道pb指向的B是独立对象?

Let's make a few minor changes to your code: 让我们对代码进行一些小的更改:

struct A {virtual void f() {}};
struct B {virtual void f() {} virtual void g() {}};

void foo(B* bp)
{
   bp->f();
}

In the above call, there is no way of knowing which sub-type of B bp points to. 在上述调用中,无法知道B bp指向的子类型。 It could be C , D , or something else. 可能是CD或其他。 If you step into the call you would know which sub-type of B bp points to. 如果您进入呼叫,您将知道B bp指向的子类型。

struct C : A, B {void f() {}};
struct D : B {void f() {}};

int main()
{
   B* pb1 = new C;
   B* pb2 = new D;

   foo(pb1);
   foo(pb2);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将派生类对象分配给基类对象时会发生什么(我的意思是对象组合,而不是指针) - what happens when assign a derived class object to a base class object(I mean object assginment, not pointer) 编译器在这段代码中做了什么(指向派生类对象的基类指针)? - What did compiler do in this piece of code(Base class pointer to derived class object)? 使用基本 class 指针创建 object 时缺少派生 class 析构函数 - Missing Derived class Destructor when object is created with the base class pointer 当我有一个指向其基类的指针时,该对象是否克隆? - Clone an object when I have a pointer to its base class? 返回对象时编译器会做什么 - What does compiler do when returning an object 当新的 object 分配给它的地址时,object 是否必须销毁? - Does object have to be destroyed when a new object is assigned to its address? 当基类指针指向基类对象时,编译器是否将使用动态绑定? - when a base class pointer point to a base class object whether the compiler will use dynamic binding? 尝试将新的派生对象分配给基类指针,但似乎没有任何反应 - Trying to assign a new derived object to a base class pointer but it seems to act like nothing happened 如何在c ++中将指向基类的指针赋给派生类的对象? - How to assign a pointer to base class to an object of a derived class in c++? 对象的“ this”指针是否等于指向其(单个)基类的指针? - Is an object's 'this' pointer equal to a pointer to its (single) base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM