切片副本,抽象类有什么问题? [英] slicing copy, what's wrong with abstract class?

查看:54
本文介绍了切片副本,抽象类有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码来验证如何禁用切片复制

根据C ++ Gotchas项目30


以下是我的类层次结构,请注意B是抽象类!!


B级

{

公开:

显式B( INT32 i = 0):i_(i){}

virtual~B(){}

virtual void do_it()= 0;

virtual void pay()const = 0;

protected:

private:

INT32 i_;

} ;


D级:公共B

{

公开:

显式D(INT32 j = 0):B(),j_(j){}

虚拟空虚do_it()

{

j_ = 100;

}

虚拟空虚支付()const {}

受保护:

私人:

INT32 j_;

};


按照书,下面的代码无法编译,但它可以

是编译在vc7.1

D d,d1(1);

B * const pb =& d1;

* pb = d; // B是抽象类,根据本书

//应该发出编译

错误但是它可以在vc7.1中编译


什么是我或书中的东西?


谢谢

i have written some code to verify how to disable slicing copy
according C++ Gotchas item 30

the follow is my class hierarchy, and note that B is abstract class!!

class B
{
public:
explicit B(INT32 i =0):i_(i){}
virtual ~B(){}
virtual void do_it() = 0;
virtual void pay() const = 0;
protected:
private:
INT32 i_;
};

class D: public B
{
public:
explicit D(INT32 j=0):B(),j_(j){}
virtual void do_it()
{
j_ = 100;
}
virtual void pay() const {}
protected:
private:
INT32 j_;
};

accring to book, the following code can not be compiled, however it can
be compiled in the vc7.1

D d,d1(1);
B *const pb=&d1;
*pb = d; // B is abstract class, and this should be issued a compile
error according to the book
// however it can be compiled in the vc7.1

what''s something with me or the book?

thanks

推荐答案



baibaichen写道:

baibaichen wrote:
我已经编写了一些代码来验证如何禁用切片复制
根据C ++ Gotchas项目30

以下是我的班级层次结构,并注意B是抽象类!!

B班
{
公开:
显式B( INT32 i = 0):i_(i){}
virtual~B(){}
virtual void do_it()= 0;
virtual void pay()const = 0; 私人:
INT32 i_;
};

D类:公共B
{
公开:显式D(INT32 j = 0):B(),j_(j){}
virtual void do_it()
{
j_ = 100;
} virtual void pay()const {}
protected:
私有:
INT32 j_;
};

按照书,下面的代码无法编译,但是它可以在vc7中编译。 1 d / d
D d,d1(1);
B * const pb =& d1;
* pb = d; // B是抽象类,根据书中这应该发出编译错误
//但它可以在vc7.1中编译


我没有这本书,但你确定它的内容是什么?为什么

不应该编译,operator =在B中公开。让它受到保护并且

它不会编译但你仍然能够分配D.

什么是我或书中的东西?

谢谢
i have written some code to verify how to disable slicing copy
according C++ Gotchas item 30

the follow is my class hierarchy, and note that B is abstract class!!

class B
{
public:
explicit B(INT32 i =0):i_(i){}
virtual ~B(){}
virtual void do_it() = 0;
virtual void pay() const = 0;
protected:
private:
INT32 i_;
};

class D: public B
{
public:
explicit D(INT32 j=0):B(),j_(j){}
virtual void do_it()
{
j_ = 100;
}
virtual void pay() const {}
protected:
private:
INT32 j_;
};

accring to book, the following code can not be compiled, however it can
be compiled in the vc7.1

D d,d1(1);
B *const pb=&d1;
*pb = d; // B is abstract class, and this should be issued a compile
error according to the book
// however it can be compiled in the vc7.1

I don''t have the book but are you sure that''s what it says? Why
shouldn''t it compile, operator= is public in B. Make it protected and
it won''t compile but you''ll still be able to assign D.
what''s something with me or the book?

thanks





baibaichen写道:
baibaichen wrote:
我已经编写了一些代码来验证如何禁用切片复制
根据C ++ Gotchas项目30

以下是我的类层次结构,并注意B是抽象类!!

B类
{
公开:
显式B(INT32 i = 0):i_(i){}
虚拟~B(){}
虚拟空虚do_it()= 0;
虚拟空虚pay()const = 0;
受保护:
私有:
INT32 i_;
};

D类:公共B
公开:
显式D(INT32 j = 0):B( ),j_(j){}
虚拟空洞do_it()
{
j_ = 100;
}
虚拟空虚pay()const {}
受保护:
私人:
INT32 j_;
};

按照书,下面的代码无法编译,但是它可以在vc7.1中编译

> D d,d1(1);
B * const pb =& d1;
* pb = d; // B是抽象类,这应该根据本书发出编译错误
//但是它可以在vc7.1中编译

什么''我或书中的东西?
i have written some code to verify how to disable slicing copy
according C++ Gotchas item 30

the follow is my class hierarchy, and note that B is abstract class!!

class B
{
public:
explicit B(INT32 i =0):i_(i){}
virtual ~B(){}
virtual void do_it() = 0;
virtual void pay() const = 0;
protected:
private:
INT32 i_;
};

class D: public B
{
public:
explicit D(INT32 j=0):B(),j_(j){}
virtual void do_it()
{
j_ = 100;
}
virtual void pay() const {}
protected:
private:
INT32 j_;
};

accring to book, the following code can not be compiled, however it can
be compiled in the vc7.1

D d,d1(1);
B *const pb=&d1;
*pb = d; // B is abstract class, and this should be issued a compile
error according to the book
// however it can be compiled in the vc7.1

what''s something with me or the book?




据我所知,没有办法避免使用抽象逻辑切片。


使用具体类型,您可以通过使您的班级

不可复制来避免切片。

这可以通过制作复制构造函数和赋值运算符来完成

私有,没有实现。

但这种方法不适用于抽象类型。

此外,如果你传递原始指针,你就不应该'没有任何

切片。

使用抽象指针,你可以在使用具有克隆逻辑的智能指针

时切片。



To my knowledge, there is no way to avoid slicing with abstract logic.

With concrete types, you can avoid slicing by making your class
non-copyable.
This can be done by making the copy constructor and assignment operator
private, and with no implementation.
But this method will not work on abstract types.
Moreover, if you''re passing raw pointers, you shouldn''t get any
slicing.
With abstract pointers, you can get slicing when using smart pointers
that have clone logic.




Axter写道:

Axter wrote:
据我所知,没有办法避免使用抽象逻辑进行切片。


你没看过我上面的帖子吗?我只是展示了它是如何完成的。

使用具体类型,你可以通过使你的类不可复制来避免切片。
这可以通过制作复制构造函数来完成和赋值运算符
私有,没有实现。
但是这个方法不适用于抽象类型。


否,但您可以保护复制和复制结构。那个

防止切片,同时允许派生类具有复制和

赋值语义。

此外,如果你传递原始指针,你不应该得到任何切片。
使用抽象指针,你可以在使用具有克隆逻辑的智能指针时切片。
To my knowledge, there is no way to avoid slicing with abstract logic.
Didn''t you read my above post? I just showed how it can be done.
With concrete types, you can avoid slicing by making your class
non-copyable.
This can be done by making the copy constructor and assignment operator
private, and with no implementation.
But this method will not work on abstract types.
No but you can make copying and copy-construction protected. That
prevents slicing whilst allowing the derived classes to have copy and
assignment semantics.
Moreover, if you''re passing raw pointers, you shouldn''t get any
slicing.
With abstract pointers, you can get slicing when using smart pointers
that have clone logic.




毫无疑问,Axter克隆指针的另一个案例。



No doubt yet another case for the Axter clone-pointer.


这篇关于切片副本,抽象类有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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