Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:06:45+00:00 2026-05-13T19:06:45+00:00

I have a container of smart pointers to mutable objects. I have to write

  • 0

I have a container of smart pointers to mutable objects. I have to write two for_each loops, one for accessing the objects as read-only data and another for mutable data. The compiler is telling me that std::vector< boost::shared_ptr<Object> > is not the same as std::vector< boost::shared_ptr<const Object> >, note the const.

Here is my example code:

#include <vector>
#include "boost/shared_ptr.hpp"
#include <iterator>

class Field_Interface
{ ; };
typedef boost::shared_ptr<Field_Interface> Ptr_Field_Interface;
typedef boost::shared_ptr<const Field_Interface> Ptr_Const_Field_Interface;

struct Field_Iterator
  : std::input_iterator<std::forward_iterator_tag, Ptr_Field_Interface>
{
  // forward iterator methods & operators...
};

struct Const_Field_Iterator
  : std::input_iterator<std::forward_iterator_tag, Ptr_Const_Field_Interface>
{
  // forward iterator methods & operators...
};

struct Field_Functor
{
  virtual void operator()(const Ptr_Field_Interface&) = 0;
  virtual void operator()(const Ptr_Const_Field_Interface&) = 0;
};

class Record;
typedef boost::shared_ptr<Record> Ptr_Record;
typedef boost::shared_ptr<const Record> Ptr_Const_Record;

class Record_Base
{
  protected:
    virtual Field_Iterator beginning_field(void) = 0;
    virtual Field_Iterator ending_field(void) = 0;
    virtual Const_Field_Iterator const_beginning_field(void) = 0;
    virtual Const_Field_Iterator const_ending_field(void) = 0;

    void for_each(Field_Functor * p_functor)
    {
       Field_Iterator iter_begin(beginning_field());
       Field_Iterator iter_end(ending_field());
       for (; iter_begin != iter_end; ++ iter_begin)
       {
         (*p_functor)(*iter_begin);
       }
     }
};

class Record_Derived
{
public:
   typedef std::vector<Ptr_Field_Interface> Field_Container;
   typedef std::vector<Ptr_Record>          Record_Container;
private:
   Field_Container m_fields;
   Record_Container m_subrecords;
};

Given all the above details, how do I implement the pure abstract methods of Record_Base in Record_Derived?

I’ve tried:

  • returning m_fields.begin(), which
    returns conversion errors (can’t
    convert std::vector<...> to
    Field_Iterator
    )
  • returning &m_fields[0], which is
    dangerous, because it assumes stuff
    about the internals of std::vector.

BTW, I’m not using std::for_each because I have to iterate over a container of fields and a container of sub-records.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:06:45+00:00Added an answer on May 13, 2026 at 7:06 pm

    What you’re doing resembles both the Composite and Visitor patterns. Those two patterns mesh well together, so it seems you are on the right track.

    To implement the composite pattern, assign the following roles (refer to Composite pattern UML diagram):

    • Leaf -> Field
    • Composite -> Record
    • Component -> Abstract base class of Field and Record (can’t think of a good name)

    Component operations that are called on Composite types, are passed on recursively to all children (Leaves and other nested Composite types).

    To implement the Visitor pattern, overload operator() in your functor classes for each Component subtype (Field and Record).

    I recommend that you get a copy of the Design Patterns book by the “Gang of Four”, which explains these concepts better and goes into much more detail than I possibly can.

    Here is some sample code to whet your appetite:

    #include <iostream>
    #include <vector>
    #include "boost/shared_ptr.hpp"
    #include "boost/foreach.hpp"
    
    class Field;
    class Record;
    
    struct Visitor
    {
        virtual void operator()(Field& field) = 0;
        virtual void operator()(Record& field) = 0;
    };
    
    class Component
    {
    public:
        virtual bool isLeaf() const {return true;}
        virtual void accept(Visitor& visitor) = 0;
    };
    typedef boost::shared_ptr<Component> ComponentPtr;
    
    class Field : public Component
    {
    public:
        explicit Field(int value) : value_(value) {}
        void accept(Visitor& visitor) {visitor(*this);}
        int value() const {return value_;}
    
    private:
        int value_;
    };
    
    class Record : public Component
    {
    public:
        typedef std::vector<ComponentPtr> Children;
        Record(int id) : id_(id) {}
        int id() const {return id_;}
        Children& children() {return children_;}
        const Children& children() const {return children_;}
        bool isLeaf() const {return false;}
        void accept(Visitor& visitor)
        {
            visitor(*this);
            BOOST_FOREACH(ComponentPtr& child, children_)
            {
                child->accept(visitor);
            }
        }
    
    private:
        int id_;
        Children children_;
    };
    typedef boost::shared_ptr<Record> RecordPtr;
    
    struct OStreamVisitor : public Visitor
    {
        OStreamVisitor(std::ostream& out) : out_(out) {}
        void operator()(Field& field) {out_ << "field(" << field.value() << ") ";}
        void operator()(Record& rec) {out_ << "rec(" << rec.id() << ") ";}
        std::ostream& out_;
    };
    
    int main()
    {
        RecordPtr rec(new Record(2));
            rec->children().push_back(ComponentPtr(new Field(201)));
            rec->children().push_back(ComponentPtr(new Field(202)));
        RecordPtr root(new Record(1));
            root->children().push_back(ComponentPtr(new Field(101)));
            root->children().push_back(rec);
    
        OStreamVisitor visitor(std::cout);
        root->accept(visitor);
    }
    

    In Record, you may want to provide methods for manipulating/accessing children, instead of returning a reference to the underlying children vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why don't you use ADO.NET's SqlBulkCopy programmatically in PowerShell? You… May 16, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer Your code creates 1000 Threads. That is enormously expensive, requiring… May 16, 2026 at 11:40 am
  • Editorial Team
    Editorial Team added an answer I expect the inbuilt implementation simply uses Activator.CreateInstance<T> for simplicity.… May 16, 2026 at 11:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have to register an object in a container upon its creation. Without smart
I've been trying to use smart pointers to upgrade an existing app, and I'm
How should I avoid using the this pointer in conjunction with smart pointers? Are
I currently have a tomcat container -- servlet running on it listening for requests.
I have class foo that contains a std::auto_ptr member that I would like to
I have a std::queue that is wrapped as a templated class to make a
I have draggable list of < li > elements which I'm able to drag
There is a container, for example lets say which has a volume of V.
I have a module which runs standalone in a JVM (no containers) and communicates
I have the following code to hide/show elements depending on what value is selected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.