Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   class data usage scenarios (http://www.velocityreviews.com/forums/t457194-class-data-usage-scenarios.html)

earthwormgaz 09-26-2006 09:48 AM

class data usage scenarios
 
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?


Daniel T. 09-26-2006 10:35 AM

Re: class data usage scenarios
 
"earthwormgaz" <earthwormgaz@googlemail.com> 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.

earthwormgaz 09-26-2006 01:11 PM

Re: class data usage scenarios
 
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.


Daniel T. 09-26-2006 01:58 PM

Re: class data usage scenarios
 
"earthwormgaz" <earthwormgaz@googlemail.com> 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.


All times are GMT. The time now is 05:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.