通过基类引用引用赋值 [英] Reference assignment through base class reference

查看:70
本文介绍了通过基类引用引用赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,

#include< stdio.h>


班级基地

{

};

类Der1:公共基地

{

};

类Der2:公共基地

{

};


int main()

{

Der1 d1; Der2 d2;

Base& b1 = d1;

Base& b2 = d2;

b1 = b2;

}


上面的代码被编译。但是我想知道这个任务是什么b1 =

b2;如果这些是指针,那么它是可以理解的。但是如果引用的话,

如何在赋值之上表现出来。是否展示UB?


问候,

Siddharth

解决方案

siddhu写道:


#include< stdio.h>



^^^^^^^^^^^^^^^^

这是为了什么?


>

class Base

{

};

class Der1:public Base

{

};

class Der2:public Base

{

};


int main()

{

Der1 d1; Der2 d2;

Base& b1 = d1;

Base& b2 = d2;

b1 = b2;

}


上面的代码被编译。但是我想知道这个任务是什么b1 =

b2;如果这些是指针,那么它是可以理解的。但是如果引用的话,

如何在赋值之上表现出来。它是否展示了UB?



否,没有UB。 'd1''的''基础''子对象获得与''基础''相同的''基础''值,因为'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' />
V

-

请在通过电子邮件回复时删除资金''A'

我这样做没有回复最热门的回复,请不要问


siddhu写道:


尊敬的专家,


#include< stdio.h>


等级基础

{

};

类Der1:公共基地

{

};

类Der2:公共基地< br $>
{

};


int main()

{

Der1 d1; Der2 d2;

Base& b1 = d1;

Base& b2 = d2;

b1 = b2;

}


上面的代码被编译。但是我想知道这个任务是什么b1 =

b2;如果这些是指针,那么它是可以理解的。但是如果引用的话,

如何在赋值之上表现出来。它是否展示UB?



这里没有UB。你的班级可能不会期待它。它通常被称为切片,称为切片。它通常也不是预期在实现中发生的事情,因此事情可能会出错。


可以通过制作基类构造函数protected。


Gianni Mariani写道:


siddhu写道:


>尊敬的专家,

#include< stdio.h>

班级基础
{
};
类Der1:公共基地
{
};
类Der2:公共基地
{
};

int main()
{Der / d1; Der2 d2;
Base& b1 = d1;
基数& b2 = d2;
b1 = b2;
}

上面的代码被编译。但是我想知道这个任务是什么b1 =
b2;如果这些是指针,那么它是可以理解的。但是如果引用的话,上面的赋值是如何表现的呢?它是否展示了UB?



这里没有UB。你的班级可能不会期待它。它通常被称为切片,称为切片。它通常也不是预期在实现中发生的事情,因此事情可能会出错。


可以通过制作基类构造函数protected。



构造函数?不是复制赋值运算符?顺便说一句,切片如何通过引用将副本分配给对象?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?

Regards,
Siddharth

解决方案

siddhu wrote:

#include <stdio.h>

^^^^^^^^^^^^^^^^
What''s that for?

>
class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?

No, no UB. ''Base'' subobject of ''d1'' gets the same ''Base'' value as
the ''Base'' subobject of ''d2''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


siddhu wrote:

Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?

No UB here. Your class might not expect it though. It is commonly
referred to as "slicing". It''s also usually not something that is
expected to happen in the implementation and so things can co awry.

It can be prevented by making the base class constructor "protected".


Gianni Mariani wrote:

siddhu wrote:

>Dear experts,
#include <stdio.h>

class Base
{
};
class Der1:public Base
{
};
class Der2:public Base
{
};

int main()
{
Der1 d1;Der2 d2;
Base& b1 = d1;
Base& b2 = d2;
b1 = b2;
}

Above code gets compiled. But I wonder what does the assignment b1 =
b2; do?If these are pointers then its understandable. But how does
above assignment behave in case of references. Does it exhibit UB?


No UB here. Your class might not expect it though. It is commonly
referred to as "slicing". It''s also usually not something that is
expected to happen in the implementation and so things can co awry.

It can be prevented by making the base class constructor "protected".

Constructor? Not the copy assignment operator? How does slicing get
into copy assigning objects through references, by the way?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于通过基类引用引用赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆