class data usage scenarios

E

earthwormgaz

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?
 
D

Daniel T.

earthwormgaz said:
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'.
 
E

earthwormgaz

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.
 
D

Daniel T.

earthwormgaz said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top