473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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?

Sep 26 '06 #1
3 1346
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
3
by: Florida Coder | last post by:
I have the need to store some application specific configuration data to be used by a class library and or a windows service. I would like to do this in a fashion similar to the way we do with...
35
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.