Why do I get an error when calling front() on boost::ptr_list?
I have got a virtual class basic_action
. Class sippeers
inherits class basic_action
. 开发者_开发知识库To store instances of sippeers
classes I'm using boost::ptr_list
. Here's code example:
boost::ptr_list<basic_action> ActionsList;
sippeers spclass;
ActionsList.push_back(&spclass);
basic_action *sp = ActionsList.front();
Here I create an instance of prt_list
with pointers to instances of my basic_action
classes.
Next I make new instance of my sippeers
class.
Next I insert pointer to sippeers
class into ptr_list
.
The last string fails.
Cannot convert from 'basic_action' to 'basic_action *'.
But there IS a basic_action *
inside, not basic_action
!
boost::ptr_list::front()
returns a reference to the templated type, not a pointer.
So in this case it's returning a basic_action&
.
See the documentation here for ptr_sequence_adapter
, from which ptr_list
is derived.
So your code should read:
boost::ptr_list<basic_action> ActionsList;
sippeers spclass;
ActionsList.push_back(&spclass);
basic_action &sp = ActionsList.front();
ptr_list::front() returns a reference to the first object in the list. If basic_action was a concrete type you could both of the following.
// 1
basic_action& sp = ActionsList.front();
// 2
basic_action sp = ActionsList.front();
#1 would make sp a reference to the first object in the list. In other words, any changes you made through sp would also change the first object in the list.
#2 would instantiate a new basic_action object and copy the contents of the first object in the list into this new object. Any changes to it would not affect the first item in the list.
If basic_action is an abstract class option #2 is no longer available to you because you cannot instantiate abstract class objects.
also, you shouldn't put stack allocated objects into a boost pointer container. Bad things will happen when the ptr_list goes out of scope and tries to delete all the objects it contains. Instead do something like:
boost::ptr_list<basic_action> ActionsList;
ActionsList.push_back(new sippeers);
basic_action& sp = ActionsList.front();
If you look at the header then you will see that:
template
<
class T,
class VoidPtrSeq,
class CloneAllocator = heap_clone_allocator
>
class ptr_sequence_adapter
{
public: // construct/copy/destroy
template< class InputIterator >
assign( InputIterator first, InputIterator last );
template< class InputRange >
assign( const InputRange& e );
public: // element access
T& front();
(Note; ptr_list directly inherits from ptr_sequence_adapter) So you are getting the type, not a reference to the type; it does an automatic dereference for you:
basic_action sp = ActionsList.front();
is correct.
精彩评论