[英]Correct use of const c++
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. 我有这个示例代码(如下),example1()方法没有问题,example2()类似但我必须强制const_char使其编译,虽然我认为不需要example1()方法,example2()将是也不需要。
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()方法以进行编译或如何在example2()中正确调用buffer.add()而不强制执行const_cast? add() method is not modifying item, so const_cast is unnecessary. add()方法不修改项目,因此不需要const_cast。 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
}
You should do something like : 你应该做的事情如下:
Buffer<MyClass const*>
because &item on a const MyClass is a Myclass const* not a MyClass* 因为const MyClass上的&item是Myclass const *而不是MyClass *
Your Buffer
class template can be considered to be correct and it is your example2
function which is incorrect. 您的Buffer
类模板可以被认为是正确的,并且您的example2
函数是不正确的。 I'll proceed on that basis. 我会继续这样做。
In example1
, the function has a const reference parameter to an instance of a MyClass
. 在example1
,该函数具有一个到MyClass
实例的const引用参数。 The add
method of Buffer
then makes a copy of the value of the instance, placing it in its own memory buffer (I hope Buffer
is keeping track of all of this memory). 然后, Buffer
的add
方法复制实例的值,将其放在自己的内存缓冲区中(我希望Buffer
能够跟踪所有这些内存)。 Therefore the fact that example
takes a const reference is irrelevant to Buffer
since a copy of the value is made. 因此, example
采用const引用这一事实与Buffer
无关,因为该值的副本已生成。
In example2
, the add
method of Buffer
is taking a copy of the pointer to the instance of MyClass
and storing that in its own memory buffer. 在example2
, Buffer
的add
方法将指针的副本带到MyClass
的实例并将其存储在自己的内存缓冲区中。 In example2
you have instantiated Buffer
as holding non-const pointers to MyClass
, so that is what you should be giving it, so example2
should be: 在example2
你已经实例化了Buffer
作为持有MyClass
非常量指针,所以你应该给它,所以example2
应该是:
void example2( MyClass & item )
{
Buffer<MyClass *> buffer; // NOW with pointers to MyClass
buffer.add( &item );
}
Now, you must be aware that if buffer
were to be used, the item
must remain fixed in memory until you've finished with it. 现在,你必须意识到,如果buffer
将被使用,该item
必须保持,直到你完成它固定在内存中。 Whereas in example1
, the items can go away since you've safely stored copies in buffer
. 而在example1
,由于您已经将副本安全地存储在buffer
,因此这些项目可能会消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.