By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

class data usage scenarios

P: n/a
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?

Sep 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"earthwormgaz" <ea**********@googlemail.comwrote:
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.
Sep 26 '06 #2

P: n/a
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.

Sep 26 '06 #3

P: n/a
"earthwormgaz" <ea**********@googlemail.comwrote:
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.
Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.