How bad is it to include a non const accessor to a container of
objects? For instance
template <class WheelType>
class CarBase
{
typedef std::list<Wheel Type*> WheelList;
private:
//for the sake of this example, assume there can be any number
//of wheels
WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()cons t{return m_Wheels;}
WheelList& GetWheels(){ret urn m_Wheels;}
//other car related methods
};
I think this doesn't look good because I may as well make the
WheelList public... but I hate making member variables public unless
the class is a simple data structure (which this isn't, the real-world
examples are complex)
But if I only have the const accessor how should a client gain access
to the wheels so they can change their properties? (If I create an
iterator class to iterate through the objects how is that any
different to allowing public access)
This thing keeps going around and around my head and I think I'm
starting to obsess on the problem so any help will be greatly
appreciated. What do *you* do in these circumstances? 5 1477
On Wed, 05 May 2004 13:51:50 +0100, TheFerryman <fe***@onthenet .com> wrote: How bad is it to include a non const accessor to a container of objects? For instance
template <class WheelType> class CarBase { typedef std::list<Wheel Type*> WheelList;
private:
//for the sake of this example, assume there can be any number //of wheels WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()cons t{return m_Wheels;} WheelList& GetWheels(){ret urn m_Wheels;}
//other car related methods };
I think this doesn't look good because I may as well make the WheelList public... but I hate making member variables public unless the class is a simple data structure (which this isn't, the real-world examples are complex)
But if I only have the const accessor how should a client gain access to the wheels so they can change their properties? (If I create an iterator class to iterate through the objects how is that any different to allowing public access)
This thing keeps going around and around my head and I think I'm starting to obsess on the problem so any help will be greatly appreciated. What do *you* do in these circumstances?
I think you're having difficulty because of the interface you've chosen. In
a typical collection of some kind, the "get" and "set" functionality isn't
going to be abstracted in terms of getting and putting "the entire
collection", but rather individual elements of the collection. If you were
to separate out "getting" from "setting", then the result of getting could
be a value (rather than a ref), and setting could be sanity-checked. The
way you treat the wheel list doesn't take advantage of any of that; so for
all intents and purposes, you may as well have m_Wheels be public if that's
all the functionality you want.
I think you need to decide whether CarBase is going to concern itself with
individual Wheels or not. If so, then "get" and "set" functionality should
be provided for individual Wheels if you really want CarBase to have some
added value. If not, then WheelList should be a user-defined class in its
own right, perhaps implemented in terms of a list (or not), so that after
obtaining a handle to it from CarBase via GetWheels, the client would still
have to go thorough WheelLists's (presumably safe) interface to munge with
the Wheel List. Make any sense?
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
TheFerryman wrote: How bad is it to include a non const accessor to a container of objects? For instance
template <class WheelType> class CarBase { typedef std::list<Wheel Type*> WheelList;
private:
//for the sake of this example, assume there can be any number //of wheels WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()cons t{return m_Wheels;} WheelList& GetWheels(){ret urn m_Wheels;}
//other car related methods };
I think this doesn't look good because I may as well make the WheelList public... but I hate making member variables public unless the class is a simple data structure (which this isn't, the real-world examples are complex)
In general would avoid that approach, it exposes the implementation of
the CarBase class too much. If you would decide to use another
container, or decide not to store pointers, it would break code the uses
the CarBase class.
But if I only have the const accessor how should a client gain access to the wheels so they can change their properties? (If I create an iterator class to iterate through the objects how is that any different to allowing public access)
Iterators do a better job of hidding implementation details and give the
CarBase class more control what clients can do and what not. If the
CarBase class returns a reference to m_Wheels, clients can do what event
they like with WheelList, including insert and removing elements. If
CarBase has a (non-const) iterator clients can only manipulate WheelType
objects. The iterator could also control which members of the WheelType
objects can be accessed.
This thing keeps going around and around my head and I think I'm starting to obsess on the problem so any help will be greatly appreciated. What do *you* do in these circumstances?
Think carefully about the interface of the CarBase class, and consider
carefully what the clients should be able to do with it. Don't expose
more than you have to, this makes it easier to change the implementation
later.
--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
TheFerryman <fe***@onthenet .com> wrote in
news:67******** *************** *********@4ax.c om: How bad is it to include a non const accessor to a container of objects? For instance
(...)
When you want to be able to modify the elements held by the container, then
there's no other way than make them exposed. Why do you think it is bad?
But if I only have the const accessor how should a client gain access to the wheels so they can change their properties? (If I create an iterator class to iterate through the objects how is that any different to allowing public access)
This is how containers work. They *hold* objects, not *hide* them. Unless
of course you want it otherwise in your implementation.
--
:: bartekd [at] o2 [dot] pl
"TheFerryma n" <fe***@onthenet .com> wrote How bad is it to include a non const accessor to a container of objects? For instance
template <class WheelType> class CarBase { typedef std::list<Wheel Type*> WheelList;
private:
//for the sake of this example, assume there can be any number //of wheels WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()cons t{return m_Wheels;} WheelList& GetWheels(){ret urn m_Wheels;}
//other car related methods };
I think this doesn't look good because I may as well make the WheelList public... but I hate making member variables public unless the class is a simple data structure (which this isn't, the real-world examples are complex)
But if I only have the const accessor how should a client gain access to the wheels so they can change their properties? (If I create an iterator class to iterate through the objects how is that any different to allowing public access)
This thing keeps going around and around my head and I think I'm starting to obsess on the problem so any help will be greatly appreciated. What do *you* do in these circumstances?
I think it's a question of logical design. By just exposing the underlying
WheelList, you're saying that it's perfectly valid for any outside entity to
remove every wheel, or to add 30000 wheels, or to make every wheel a different
size, or to make some elements null pointers but leave them in the list, or make
some of the pointers point to shared objects, and so on. In other words, it's a
free-for-all.
In a clean design, if the WheelList is the responsibility of CarBase, then the
interface should restrict access to only the operations that CarBase allows. For
example, CarBase might enforce the same WheelType on a given axel, or even the
same WheelType for the entire car (though this might be overly restrictive).
CarBase should probably restrict the number of wheels that can be set (the exact
number being provided by a virtual function that's overridden in derived
classes). CarBase should DEFINITELY manage the lifetimes of the objects pointed
to by your WheelType pointers and not allow outside entities to meddle. As a
general rule, a class should take responsibility for all of its data members and
enforce its internal consistency unless it's a mere "holder" like std::pair<>.
Claudio Puviani
"TheFerryma n" <fe***@onthenet .com> wrote in message template <class WheelType> class CarBase { typedef std::list<Wheel Type*> WheelList;
private:
//for the sake of this example, assume there can be any number //of wheels WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()cons t{return m_Wheels;} WheelList& GetWheels(){ret urn m_Wheels;}
//other car related methods };
But if I only have the const accessor how should a client gain access to the wheels so they can change their properties? (If I create an iterator class to iterate through the objects how is that any different to allowing public access)
As others point out, returning iterators don't allow you to add or delete
wheels. But there's mode. You can have the iterator's operator* return a
smart reference. This is a user defined class that behaves like a builtin
reference for most practical purposes. It typically has a protected
constructor, and overrides operator= as in
WheelList::refe rence::operator =(const Wheel&) which can do additional
checking, such as throwing an exception if you try to put a wrong type of
wheel on a certain car.
This thing keeps going around and around my head and I think I'm starting to obsess on the problem so any help will be greatly appreciated. What do *you* do in these circumstances?
Remember, it's often a tradeoff between perfect design, performance, and
time + budget. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nick Moore |
last post by:
This may be the wrong newsgroup for this question. I'm
trying to develop a container control inherited from a
panel that I can 'Roll Up' and 'Roll down' on a button
click. The component is to be available from the
toolbox. The first time I dragged the control from the
toolbox onto a form and added other toolbox components to
it in design mode, it obviously wasn't a container, the
other components were not associated with my control. I...
|
by: duoduo |
last post by:
I want to know one thing, what is best design for tablespace
containers, of course, it is DB2 EE 7.2 on AIX 4.3.3.
For example, I have RAID 0+1 storage (8 disks) on EMC Symmetrix. Now I
need to create a 64GB tablespace on this EMC RAID. Which of the
following is the best way from performance perspective?
(1) 1 64GB container
(2) 4 * 16GB containers
|
by: Yannick Turgeon |
last post by:
Hello,
We are in the process of examining our current main application. We have to
do some major changes and, in the process, are questionning/validating the
use of MS Access as front-end. The application is relatively big: around 200
tables, 200 forms and sub-forms, 150 queries and 150 repports, 5GB of data
(SQL Server 2000), 40 users.
I'm wondering what are the disadvantages of using Access as front-end? Other
that it's not...
|
by: David Kistner |
last post by:
I'm fairly new to Access (I've worked with Oracle and MySQL in the
past). I was asked to build an application for a small office and told
that I had to use Access 2002. I was VERY uncomfortable with this, but
went ahead anyway. So two weeks ago we rolled out the new system and
I'm still wondering how robust Access is......I know it's not in the
same league as Oracle, but at the same time this isn't a real big
application. But I can't...
|
by: pradeep |
last post by:
Hi,
I amnew to this group and lucky to have found this group. i have a
master table which has different types of application say Desktop,
Mainframes, etc.
I have an individual table for each of these application types.... one
for Desktop, one for mainframes, etc
How do i synchrinize data between these two tables, say i update the
| |
by: agins |
last post by:
I have used nearly every version of ACT for more than 15-years.
Currently have 5,000 records in one database and the program seems
unable to smoothly handle that many. Am starting a new business that
will have perhaps 20,000 records and considering ACCESS (although I
dread the learning curve).
Does anyone have experience comparing the two? Does anyone know how
VISTA will interface w ith each?
Steve Agins
|
by: Peter Olcott |
last post by:
So far the only way that I found to do this was by making a
single global instance of the container class and providing
access to the contained class, through this single global
instance. Are there any other no-overhead ways that a
contained class can access its container?
The obvious choice of passing (a pointer or a reference to
the container) to the contained class is not a no-overhead
solution, it requires both memory and time. I am...
|
by: Rob McDonald |
last post by:
I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).
In my application, I will need to add/remove items from arbitrary
points in the container.
I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.
Fortunately, in my application, I don't need to do both of these
|
by: gerry |
last post by:
I am trying to create a custom container control that will only ever contain
a specific type of control.
At design time, when a control of a different type is added to the container
I would like to wrap the control in the proper control type - which is also
a container.
At design time I want to be able to turn this :
<my:container>
<asp:textbox />
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |