small design issues with creating views (portions) of a class

Discussion in 'C++' started by er, Sep 9, 2007.

  1. er

    er Guest

    hi,

    here's a piece of code and questions below. any suggestion would be
    appreciated.

    class Write_view{
    public:
    Write_view(Obj&,unsigned int index){/*.../*};
    void fun(...){/*...*/};//writes in portion index of Obj
    };

    class Obj{
    public:
    Write_view view(unsigned int index){
    return Write_view(*this,index)
    };
    friend class Write_view;

    private:
    //some data
    };

    a) should i be worried that Obj may be deleted before Write_view?
    perhaps i could make the constructor of Obj private and use a friend
    helper function to call the constructor and return a shared_ptr<Obj>.
    is that an overkill or it sounds fine?

    b) i'm unhappy with view(index) returning an object because i only
    need the same object no matter how often i call it on the same object.
    how about this?

    shared_ptr<Write_view> view(unsigned int index){
    if(count[index]==0){
    views[index]=shared_ptr<Write_view>(new
    Write_view(*this,index));
    };
    return views[index];// map<unsigned int, shared_ptr<Write_view> >
    views
    };

    or this?

    Write_view& view(unsigned int index){
    return *shared_ptr<Write_view>(new Write_view(*this,index));
    };

    c) is there a better way altogether?
     
    er, Sep 9, 2007
    #1
    1. Advertising

  2. er

    er Guest

    On Sep 8, 8:44 pm, er <> wrote:
    > hi,
    >
    > here's a piece of code and questions below. any suggestion would be
    > appreciated.
    >
    > class Write_view{
    > public:
    > Write_view(Obj&,unsigned int index){/*.../*};
    > void fun(...){/*...*/};//writes in portion index of Obj
    >
    > };
    >
    > class Obj{
    > public:
    > Write_view view(unsigned int index){
    > return Write_view(*this,index)
    > };
    > friend class Write_view;
    >
    > private:
    > //some data
    >
    > };
    >
    > a) should i be worried that Obj may be deleted before Write_view?
    > perhaps i could make the constructor of Obj private and use a friend
    > helper function to call the constructor and return a shared_ptr<Obj>.
    > is that an overkill or it sounds fine?
    >
    > b) i'm unhappy with view(index) returning an object because i only
    > need the same object no matter how often i call it on the same object.
    > how about this?
    >
    > shared_ptr<Write_view> view(unsigned int index){
    > if(count[index]==0){
    > views[index]=shared_ptr<Write_view>(new
    > Write_view(*this,index));
    > };
    > return views[index];// map<unsigned int, shared_ptr<Write_view> >
    > views
    > };
    >
    > or this?
    >
    > Write_view& view(unsigned int index){
    > return *shared_ptr<Write_view>(new Write_view(*this,index));
    > };
    >
    > c) is there a better way altogether?


    well actually i found out that this approach is convoluted. what i do
    now instead is write an impl class that does all the operations and
    has operator[] return a class with a limited interface that forwards
    to ptr<impl>.
     
    er, Sep 14, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Buchan
    Replies:
    2
    Views:
    730
    Kevin Buchan
    Dec 1, 2003
  2. Christian H
    Replies:
    0
    Views:
    294
    Christian H
    Aug 8, 2003
  3. ani
    Replies:
    7
    Views:
    2,109
    Alphonse Giambrone
    Jan 5, 2004
  4. Jeremy Chapman

    Reading portions of the web.config file

    Jeremy Chapman, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    John Timney \(ASP.NET MVP\)
    Jun 22, 2005
  5. Jason Heydasch
    Replies:
    1
    Views:
    307
Loading...

Share This Page