正确使用 const c++
我有这个示例代码(如下), example1() 方法工作没有问题, example2() 类似,但我必须强制 const_char 使其编译,尽管我认为不需要 example1() 方法, example2() 将是也不需要。
我的问题是,如何修改 add() 方法以进行编译,或者如何在 example2() 中正确调用 buffer.add() 而不强制 const_cast ? add() 方法不修改 item,因此 const_cast 是不必要的。哪种形式是正确或合适的?
这是示例代码:
template <class Item>
class Buffer
{
public:
Item * _pItems;
int _nItems;
// ... constructor / destructors etc
void add( const Item & item ) // or maybe Item const & item
{
_pItems[_nItems++] = item;
}
};
class MyClass
{
public:
// data
};
void example1( const MyClass & item )
{
Buffer<MyClass> buffer;
buffer.add( item ); // WORKS, no problem
}
void example2( const MyClass & item )
{
Buffer<MyClass *> buffer; // NOW with pointers to MyClass
//buffer.add( item ); // ERROR: 'Buffer<Item>::add' : cannot convert parameter 1 from 'const MyClass' to 'MyClass *const &'
buffer.add( const_cast<MyClass *>( &item ) ); // forcing const_cast WORKS
}
I have this sample code ( below ), example1() method works with no problem, example2() is similar but I must force const_char to make it compile, although I consider as example1() method is not needed, example2() would be not needed either.
My question is, how can I modify add() method to make both compile or how must I correctly call buffer.add() in example2() without forcing const_cast? add() method is not modifying item, so const_cast is unnecessary. Which is the correct or suitable form?
Here's the sample code:
template <class Item>
class Buffer
{
public:
Item * _pItems;
int _nItems;
// ... constructor / destructors etc
void add( const Item & item ) // or maybe Item const & item
{
_pItems[_nItems++] = item;
}
};
class MyClass
{
public:
// data
};
void example1( const MyClass & item )
{
Buffer<MyClass> buffer;
buffer.add( item ); // WORKS, no problem
}
void example2( const MyClass & item )
{
Buffer<MyClass *> buffer; // NOW with pointers to MyClass
//buffer.add( item ); // ERROR: 'Buffer<Item>::add' : cannot convert parameter 1 from 'const MyClass' to 'MyClass *const &'
buffer.add( const_cast<MyClass *>( &item ) ); // forcing const_cast WORKS
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该这样做:
因为 const MyClass 上的 &item 是 Myclass const* 而不是 MyClass*
You should do something like :
because &item on a const MyClass is a Myclass const* not a MyClass*
您的 Buffer 类模板可以被认为是正确的,而您的 example2 函数是不正确的。我将在此基础上继续。
在
example1
中,该函数有一个指向MyClass
实例的 const 引用参数。然后,Buffer
的add
方法复制实例的值,将其放置在自己的内存缓冲区中(我希望Buffer
保留跟踪所有这些记忆)。因此,example
采用 const 引用的事实与Buffer
无关,因为已创建值的副本。在
example2
中,Buffer
的add
方法获取指向MyClass
实例的指针的副本并存储在它自己的内存缓冲区中。在example2
中,您已将Buffer
实例化为保存指向MyClass
的非常量指针,因此这就是您应该提供的内容,因此example2
应该是:现在,您必须知道,如果要使用
buffer
,则item
必须在内存中保持固定状态,直到您使用完毕为止。而在example1
中,这些项目可能会消失,因为您已将副本安全地存储在buffer
中。Your
Buffer
class template can be considered to be correct and it is yourexample2
function which is incorrect. I'll proceed on that basis.In
example1
, the function has a const reference parameter to an instance of aMyClass
. Theadd
method ofBuffer
then makes a copy of the value of the instance, placing it in its own memory buffer (I hopeBuffer
is keeping track of all of this memory). Therefore the fact thatexample
takes a const reference is irrelevant toBuffer
since a copy of the value is made.In
example2
, theadd
method ofBuffer
is taking a copy of the pointer to the instance ofMyClass
and storing that in its own memory buffer. Inexample2
you have instantiatedBuffer
as holding non-const pointers toMyClass
, so that is what you should be giving it, soexample2
should be:Now, you must be aware that if
buffer
were to be used, theitem
must remain fixed in memory until you've finished with it. Whereas inexample1
, the items can go away since you've safely stored copies inbuffer
.