class data usage scenarios

Discussion in 'C++' started by earthwormgaz, Sep 26, 2006.

  1. earthwormgaz

    earthwormgaz Guest

    hello folks,

    i keep hitting the situation where i have a class that naturally has
    some data, but has two modes of use.

    say for example we have ...

    class ProblemClass {
    int m_Data;
    };

    now, i want to provide a public interface to the user, so i add a get
    method.

    what happens when i have another class that wants to use ProblemClass,
    behind the scenes, unbeknownst to the user? I can't make the api that
    class wants to use public as well, because then the user can play with
    it.

    i end up with a public api, and a protected api, and the behind the
    scenes class gets declared a friend, or i have friend methods on some
    class.

    is there a clean way to say, here is some data, A can access it via api
    A, and B and access it by api B?
    earthwormgaz, Sep 26, 2006
    #1
    1. Advertising

  2. earthwormgaz

    Daniel T. Guest

    "earthwormgaz" <> wrote:

    > hello folks,
    >
    > i keep hitting the situation where i have a class that naturally has
    > some data, but has two modes of use.
    >
    > say for example we have ...
    >
    > class ProblemClass {
    > int m_Data;
    > };
    >
    > now, i want to provide a public interface to the user, so i add a get
    > method.
    >
    > what happens when i have another class that wants to use ProblemClass,
    > behind the scenes, unbeknownst to the user? I can't make the api that
    > class wants to use public as well, because then the user can play with
    > it.


    How is the user going to access that object and "play with it" if you
    don't provide a public api to allow it? IE:

    class AnotherClass {
    ProblemClass pc;
    };

    Any users of AnotherClass, can't get to 'pc' because there is no public
    accessor into 'pc'.

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Sep 26, 2006
    #2
    1. Advertising

  3. earthwormgaz

    earthwormgaz Guest

    Let me try to explain better.

    class A { };
    class B { }; // this class will work with A, and wants a certain set of
    operations

    class A { }; // the user writes this, he wants a differnt set of
    operations

    If I make both sets of operations public, then A sees operations meant
    for B (or similar classes).

    I am after a way to tidy up this situation.
    earthwormgaz, Sep 26, 2006
    #3
  4. earthwormgaz

    Daniel T. Guest

    "earthwormgaz" <> wrote:

    > Let me try to explain better.
    >
    > class A { };
    > class B { }; // this class will work with A, and wants a certain set of
    > operations
    >
    > class A { }; // the user writes this, he wants a differnt set of
    > operations
    >
    > If I make both sets of operations public, then A sees operations meant
    > for B (or similar classes).
    >
    > I am after a way to tidy up this situation.


    class InterfaceForB {
    // operations that B wants to use as public pure virtuals
    };

    class InterfaceForOthers {
    // operations that others want to use as public pure virtuals
    };

    class A : public InterfaceForB, public InterfaceForOthers { };

    If you really want to go overboard, you can even make A's constructors
    private and provide two functions, one which returns an InterfaceforB
    and the other returning an InterfaceForOthers, but both actually
    returning an A object.

    Personally though, I think you are worrying too much. If all of A's
    member-functions obey the invariant for A, it doesn't matter who uses
    them.

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Sep 26, 2006
    #4
    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. Jeff
    Replies:
    0
    Views:
    294
  2. Michael Herman \(Parallelspace\)
    Replies:
    5
    Views:
    3,005
    Juan T. Llibre
    Jul 5, 2005
  3. mandy
    Replies:
    8
    Views:
    701
    Andrew McDonagh
    Mar 25, 2006
  4. Joe

    best scenarios of using Perl

    Joe, Nov 13, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    152
    Eric J. Roode
    Nov 17, 2005
  5. John Oksz
    Replies:
    0
    Views:
    343
    John Oksz
    Apr 12, 2012
Loading...

Share This Page