继承自STL bitset [英] Inheriting from STL bitset
问题描述
我想要的东西非常像bitset< 64>但是有两个额外的功能:设置/获取两个32位字,转换为
unsigned long long。
我可以通过继承bitset< 64>轻松地做到这一点,但我知道STL
没有虚拟析构函数。我可以通过在派生类中显式调用
基类析构函数来解决这个问题吗?
是否有其他方法可以获得所有的bitset< 64>功能没有
重写了很多代码?
欢呼
shaun
I want something which is very like a bitset<64> but with a couple of
extra functions: set/get the two 32 bit words, and conversion to
unsigned long long.
I can do this easily by inheriting from bitset<64>, but I know that STL
has no virtual destructor. Can I get around this by calling the
baseclass destructor explicitly in my derived class?
Is there another way to get all of the bitset<64> functionality without
rewriting a lot of code?
cheers
shaun
推荐答案
" shaun roe" < SH ******* @ wanadoo.fr>在消息中写道
新闻:sh ***************************** @ news-reader.wanadooportails。 com ...
"shaun roe" <sh*******@wanadoo.fr> wrote in message
news:sh*****************************@news-reader.wanadooportails.com...
我想要的东西非常像bitset< 64>但是有一些额外的功能:设置/获取两个32位字,并转换为
unsigned long long。
我可以通过继承bitset< 64>,但我知道STL
没有虚拟析构函数。我可以通过在派生类中显式调用
基类析构函数来解决这个问题吗?
是否有其他方法可以获得所有的bitset< 64>没有重写大量代码的功能?
I want something which is very like a bitset<64> but with a couple of
extra functions: set/get the two 32 bit words, and conversion to
unsigned long long.
I can do this easily by inheriting from bitset<64>, but I know that STL
has no virtual destructor. Can I get around this by calling the
baseclass destructor explicitly in my derived class?
Is there another way to get all of the bitset<64> functionality without
rewriting a lot of code?
从没有
虚拟的类继承是没有错的析构函数,只要:
1)你不能在派生类中添加成员对象,这可能会因为破坏而被切掉,或者
2)你要小心永远不要通过一个指针来销毁这样一个物体
到基地(它会切片)。
>
就这么做。
PJ Plauger
Dinkumware,Ltd。
http://www.dinkumware.com
shaun roe写道:
shaun roe wrote:
我想要的东西非常像bitset< 64>但有几个额外的功能:设置/获取两个32位字,并转换为
unsigned long long。
C ++中没有unsigned long long。你必须谈论你正在使用的一些
编译器扩展。
我可以通过继承bitset< 64>轻松地做到这一点,但我知道STL
没有虚拟析构函数。我可以通过在派生类中显式调用
基类析构函数来解决这个问题吗?
你不需要。当您使用基类指针删除对象
派生类时,需要使用虚拟析构函数。如果你不打算使用
动态内存分配,派生类_will_的析构函数会调用
基类的析构函数。
是否有其他方法可以获得所有的bitset< 64>功能没有重写很多代码?
I want something which is very like a bitset<64> but with a couple of
extra functions: set/get the two 32 bit words, and conversion to
unsigned long long.
There is no unsigned long long in C++. You must be talking about some
compiler extension you''re using.
I can do this easily by inheriting from bitset<64>, but I know that STL
has no virtual destructor. Can I get around this by calling the
baseclass destructor explicitly in my derived class?
You don''t need to. Virtual destructor is needed when you delete an object
of derived class using a base class pointer. If you''re not going to use
dynamic memory allocation, the destructor of the derived class _will_ call
the destructor of the base class.
Is there another way to get all of the bitset<64> functionality without
rewriting a lot of code?
做你继承的东西,看看它是怎么回事。
V
Do your inheriting thing and see how it goes.
V
" shaun roe" < SH ******* @ wanadoo.fr>在消息中写道
新闻:sh ***************************** @ news-reader.wanadooportails。 com ...
"shaun roe" <sh*******@wanadoo.fr> wrote in message
news:sh*****************************@news-reader.wanadooportails.com...
我想要的东西非常像bitset< 64>但是有一些额外的功能:设置/获取两个32位字,并转换为
unsigned long long。
我可以通过继承bitset< 64>,但我知道STL
没有虚拟析构函数。我可以通过在派生类中显式调用
基类析构函数来解决这个问题吗?
是否有其他方法可以获得所有的bitset< 64>没有重写大量代码的功能?
I want something which is very like a bitset<64> but with a couple of
extra functions: set/get the two 32 bit words, and conversion to
unsigned long long.
I can do this easily by inheriting from bitset<64>, but I know that STL
has no virtual destructor. Can I get around this by calling the
baseclass destructor explicitly in my derived class?
Is there another way to get all of the bitset<64> functionality without
rewriting a lot of code?
只提供几个非会员功能有什么问题
做你需要的东西?
例如:
unsigned long long asUint64(std :: bitset< 64> const& bs);
从没有虚拟
函数且没有受保护成员的类派生的重点是什么?
添加创建其他类型甚至是什么意思
没有新的数据成员而且没有新的不变量可以保留?
有可能派生出新的类型,但风险和弊端
结果不值得IMNSHO。
问题无论是哪种方式都可能是std :: bitset
的接口无法提供对任何东西的高效r / w访问较低的
位(适合无符号长)。
根据应用程序的不同,这可能是避免
std的原因: :bitset - 特别是sinc你似乎已经在制作
了你的平台上有64位整数的假设。
问候,
Ivan
-
http: //ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格
What''s wrong with just providing a couple of non-member functions
that do what you need ?
E.g.:
unsigned long long asUint64( std::bitset<64> const& bs );
What is the point of deriving from a class that has no virtual
functions and no protected members ?
What is even the point of adding creating an additional type
that has no new data members and no new invariants to preserve?
It is possible to derive a new type, but the risks and drawbacks
that result are not worth it IMNSHO.
The problem either way might be that the interface of std::bitset
does not provide efficient r/w access to anything but the lower
bits (that fit in an unsigned long).
Depending on the application, this might be a reason to avoid
std::bitset -- especially since you seem to already be making
the assuption that a 64 bit integer is available on your platform.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
这篇关于继承自STL bitset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!