473,503 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

protected members

Hi,

I'm curious about best practice for protected members.

Consider this code fragment:-
class Patch
{
public:
Patch ();
virtual void draw (Page, int x, int y) = 0;
protected:
unsigned size () const { return size_; }
void set_size (unsigned size) { size_ = size; }
private:
unsigned size_;
};

class Plain_patch: public Patch
{
public:
Plain_Patch ();
virtual void draw (Page, int x, int y);
};

Plain_Patch::draw (Page page, int x, int y)
{
// uses size() function
}
In order for the derived class to implement its draw() method it needs
to know the size. Protected data seems to be frowned on. So is my only
choice protected get() and set() methods? Or public?

--
Nick Keighley

Jul 23 '05 #1
7 1784
* Nick Keighley:

In order for the derived class to implement its draw() method it needs
to know the size.
Make the size value available to the derived class, e.g. a member function
size().

Protected data seems to be frowned on.
No.

It has its uses.

E.g., std::queue::c is a protected data member.

So is my only choice protected get() and set() methods?
Don't provide a setter member function unless it's required.

I'd call the getter "size".

In C++ there's advantage to using a "get" prefix.

Or public?


Why shouldn't size() be public? It's naturally public. It seems you
haven't included the constraints on your design, yet ask how to solve
something within those constraints -- we're not telepaths.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach wrote:
* Nick Keighley:

In order for the derived class to implement its draw() method it needs
to know the size.
Make the size value available to the derived class, e.g. a member function
size().
Protected data seems to be frowned on.


No.

It has its uses.

E.g., std::queue::c is a protected data member.

So is my only choice protected get() and set() methods?


Don't provide a setter member function unless it's required.


there are other methods that use the set. Hmm... re-examining the code
the set method shouldn't be necessary. The objects are given a size
when they are created and shouldn't change.
I'd call the getter "size".

In C++ there's advantage to using a "get" prefix.


"[no] advantage"?
Or public?


Why shouldn't size() be public? It's naturally public. It seems you
haven't included the constraints on your design, yet ask how to solve
something within those constraints -- we're not telepaths.


it seemed to be exporting infomation unnecessarily. The only users of
the size of the object are the draw() methods. Perhaps I'm
unnecessarily constraining the design.
--
Nick Keighley

Jul 23 '05 #3
"Nick Keighley" <ni******************@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Alf P. Steinbach wrote:
* Nick Keighley:
>
> In order for the derived class to implement its draw() method it needs
> to know the size.
Make the size value available to the derived class, e.g. a member
function
size().
> Protected data seems to be frowned on.


No.

It has its uses.

E.g., std::queue::c is a protected data member.
Well, std::pair has public data members, but that doesn't mean public data
members are a great idea. I think protected data members are a generally bad
idea in agreement with the phrase "frowned on".

> So is my only choice protected get() and set() methods?


Don't provide a setter member function unless it's required.


there are other methods that use the set. Hmm... re-examining the code
the set method shouldn't be necessary. The objects are given a size
when they are created and shouldn't change.
I'd call the getter "size".

In C++ there's advantage to using a "get" prefix.


"[no] advantage"?
> Or public?


Why shouldn't size() be public? It's naturally public. It seems you
haven't included the constraints on your design, yet ask how to solve
something within those constraints -- we're not telepaths.


it seemed to be exporting infomation unnecessarily. The only users of
the size of the object are the draw() methods. Perhaps I'm
unnecessarily constraining the design.


Your size() function should be either protected or private depending on
factors we can't judge from what you've told us. I agree that the end user's
interface should be as simple as possible. On the other hand if your usage
scenario isn't completely clear it might be advisable to make size() public.
It wouldn't be a serious mistake to do so in any case.


--
Nick Keighley

Jul 23 '05 #4
* Cy Edmunds:

Well, std::pair has public data members, but that doesn't mean public data
members are a great idea. I think protected data members are a generally bad
idea in agreement with the phrase "frowned on".


If you're going to argue, at least make some kind of connection, even the
vaguest, touchy-feely kind, from premise to conclusion.

Having a pet "that's bad" or "that's good" idea is unfortunately very
common.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
Alf P. Steinbach:
If you're going to argue, at least make some kind of connection, > even the vaguest, touchy-feely kind, from premise to conclusion.

Having a pet "that's bad" or "that's good" idea is unfortunately very
common.

To my perception Cy Edmunds might be correct at this point.
Protected data is frowned upon for the same reason as public data.
(It's even worse,there are in fact some possible uses of public data,
aka. bag of bits or std::pair).
Derived classes are as coupled to the protected data/interface of their
base classes as the rest of the world to the public data/interface.
as the rest of the world.
Still,if the application is reasonably small or you can control the
changes,you can just stick to protected data to avoid complications.

Jul 23 '05 #6
On 17 Jul 2005 07:09:41 -0700, "Nick Keighley"
<ni******************@hotmail.com> wrote:

[snip]
In order for the derived class to implement its draw() method it needs
to know the size. Protected data seems to be frowned on. So is my only
choice protected get() and set() methods? Or public?


I would implement some virtual "getSize()" function. You never know if
this needs to be extended in the future. Perhaps you will want to take
into consideration some scaling factor for certain derived classes?

Bjarne Stroustrup, in "The C++ Programming Language" (p. 405 of the
3rd edition) states that "declaring data members protected is usually
a design error". Later, he goes on to say that this creates a
"software maintenance problem".

To me, it never became obvious what he meant until I started working
on one of my first fairly large projects. It became painfully obvious
when the member (a pointer) was in the base class, and the derived
class was supposed to initialize it as well as call delete on this
pointer in the derived destructor. Wasn't well documented, either. Now
consider having to check 30 or 40 different projects to make sure that
this was done correctly, and you'll see what I mean.

For invariants implemeneted as const members, though, I think there is
no danger of declaring them protected. In practice, though, I think
one finds very few instances of this compared to very many instances
of the abuse of protected members.

--
Bob Hairgrove
No**********@Home.com
Aug 5 '05 #7
* Bob Hairgrove:
On 17 Jul 2005 07:09:41 -0700, "Nick Keighley"
<ni******************@hotmail.com> wrote:

[snip]
In order for the derived class to implement its draw() method it needs
to know the size. Protected data seems to be frowned on. So is my only
choice protected get() and set() methods? Or public?
I would implement some virtual "getSize()" function. You never know if
this needs to be extended in the future. Perhaps you will want to take
into consideration some scaling factor for certain derived classes?

Bjarne Stroustrup, in "The C++ Programming Language" (p. 405 of the
3rd edition) states that "declaring data members protected is usually
a design error". Later, he goes on to say that this creates a
"software maintenance problem".

To me, it never became obvious what he meant until I started working
on one of my first fairly large projects. It became painfully obvious
when the member (a pointer) was in the base class, and the derived
class was supposed to initialize it as well as call delete on this
pointer in the derived destructor.


Deferring initialization responsibility to derived classes is generally bad,
yes.

It can be done with or without 'protected', and so is an orthogonal issue.

Any understanding you thought you gained about 'protected' from that is
therefore probably something you should forget at earliest opportunity.

Wasn't well documented, either.


That's generally bad, yes.

It can be done with or without 'protected', and so is an orthogonal issue.

Any understanding you thought you gained about 'protected' from that is
therefore probably something you should forget at earliest opportunity.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2268
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
2197
by: away | last post by:
Why some classes are seen to has their constructors declared as "protected"? Thanks!
13
7670
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
11
3795
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
16345
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
86
4551
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
14
2448
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
6
4127
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
16
3598
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
2
1924
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
0
7205
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
7093
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
7287
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
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
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
5592
agi2029
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,...
1
5021
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...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.