|
Boost Users : |
From: Joaquin M López Muñoz (joaquinlopezmunoz_at_[hidden])
Date: 2021-09-09 15:48:21
El 09/09/2021 a las 15:21, John Emmas escribió:
> On 09/09/2021 12:16, Joaquin M López Muñoz via Boost-users wrote:
>> [...]
>> if you mean for the elements to be allowed in both lists at the same
>> time, use tags:
>>
>> Â Â Â #include <boost/intrusive/list.hpp>
>> Â Â Â #include <string>
>> Â Â Â #include <utility>
>>
>> Â Â Â typedef
>> boost::intrusive::list_base_hook<boost::intrusive::tag<struct
>> animal_tag>> animals_hook;
>> Â Â Â typedef
>> boost::intrusive::list_base_hook<boost::intrusive::tag<struct
>> dog_tag>> dogs_hook;
>>
>> Â Â Â class animal :
>> Â Â Â Â Â public animals_hook, public dogs_hook
>> Â Â Â {
>> Â Â Â Â public:
>> Â Â Â Â Â Â Â animal (std::string n, int l) : name{std::move(n)}, legs{l} {}
>>
>> Â Â Â Â Â Â Â std::string name;
>> Â Â Â Â Â Â Â int legs;
>> Â Â Â };
>>
>> Â Â Â typedef
>> boost::intrusive::list<animal,boost::intrusive::base_hook<animals_hook>>
>> Animals;
>> Â Â Â typedef
>> boost::intrusive::list<animal,boost::intrusive::base_hook<dogs_hook>>
>> Dogs;
>>
>> Â Â Â int main()
>> Â Â Â {
>> Â Â Â Â Â Â Â animal a1{"labrador", 4};
>> Â Â Â Â Â Â Â animal a2{"bulldog", 4};
>>
>> Â Â Â Â Â Â Â Animals animals;
>> Â Â Â Â Â Â Â animals.push_back (a2);Â // <--- 'bulldog' works okay
>>
>> Â Â Â Â Â Â Â Dogs dogs;
>> Â Â Â Â Â Â Â dogs.push_back (a1);Â Â Â Â // <--- 'labrador' works okay
>> Â Â Â Â Â Â Â dogs.push_back (a2);Â Â Â Â // <--- 'bulldog' works okay
>>
>> Â Â Â Â Â Â Â return 0;
>> Â Â Â }
> [portion above moved from end of post]
Please don't top post. Thank you!
https://www.boost.org/community/policy.html
> Thanks Joaquin - one quick question... do I need to define 'struct
> animal_tag' and 'struct dog_tag' somewhere?
No, this is not needed.
> At present, your code compiles and links okay but I still see a
> runtime crash at the same line ('dogs.push_back (a2);')
It seems to work for me:
http://coliru.stacked-crooked.com/a/d605f0b3bccfe9fa
Please note that dogs in line 29 is defined as:
   Dogs dogs;
whereas in your original code this was:
  Animals dogs;
Maybe this is the problem?
JoaquÃn M López Muñoz
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net