473,385 Members | 1,465 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,385 software developers and data experts.

Two questions for help!

1. It is showed in a book that a class has all member variables declared as
protected. What's the reason for doing this?

2. Unnamed namespace and global scope, are they equivalent to each other?

Thanks for your help!
Jul 22 '05 #1
14 1148
"away" <Gu*******@spambs.com> wrote:
1. It is showed in a book that a class has all member variables declared as
protected. What's the reason for doing this?
Making a variable protected ensures that only the class and sub-classes
can modify the variable. The assumption is that sub-class writers will
be careful enough.

Personally, I feel that all variables should be declared private.
2. Unnamed namespace and global scope, are they equivalent to each other?


No. A variable declared in an unnamed namespace can only be accessed
from the cpp file that contains it. A variable in global scope can be
accessed from anywhere in the program.
Jul 22 '05 #2

"away" <Gu*******@spambs.com> wrote in message
news:rv*********************@bgtnsc05-news.ops.worldnet.att.net...
1. It is showed in a book that a class has all member variables declared as protected. What's the reason for doing this?
So that derived classes may have access to those members.
This is a design decision that depends upon the nature
of the problem being solved.

2. Unnamed namespace and global scope, are they equivalent to each other?


No.

-Mike
Jul 22 '05 #3
Daniel T. wrote:
2. Unnamed namespace and global scope, are they equivalent to each
other?

Are these from a test?
No. A variable declared in an unnamed namespace can only be accessed
from the cpp file that contains it. A variable in global scope can be
accessed from anywhere in the program.


A better name for the unnamed namespace would be "the namespace whose name
cannot be written". Otherwise the unnamed namespace would not be unique to
each translation unit; the global one would be unnamed.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #4

"Daniel T." <po********@earthlink.net> wrote in message
news:po******************************@news2.east.e arthlink.net...
"away" <Gu*******@spambs.com> wrote:
1. It is showed in a book that a class has all member variables declared as protected. What's the reason for doing this?


Making a variable protected ensures that only the class and sub-classes
can modify the variable. The assumption is that sub-class writers will
be careful enough.

Personally, I feel that all variables should be declared private.


Can you enumerate why all variables should be private ? I've written
classes a a number
of times where I've made all variables private and had to write numerous
member functions
to allow access to the variables by the derived classes, it didn't seem that
there was much
advantage and a lot of extra code was needed to wrap the data member . It
seems
to me that there are good reasons for making variables protected :-

1. if the derived classes would access them
2. modifying these variables does not require side effects.

Jul 22 '05 #5
Dave Townsend wrote:
Can you enumerate why all variables should be private ? I've written
classes a a number
of times where I've made all variables private and had to write numerous
member functions
to allow access to the variables by the derived classes, it didn't seem that
there was much
advantage and a lot of extra code was needed to wrap the data member . It
seems
to me that there are good reasons for making variables protected :-

1. if the derived classes would access them
2. modifying these variables does not require side effects.

One value of the data-hiding is that it isolates the implementation from
the functionality.

So for example if you have
// Silly example

class someClass
{
string fullName;
// ...

public:

//...

string getName() const;
void setName(const string &newName);

// ...
};
class fullClass: public someClass
{
string address;
// ...

public:

// ...

string getAddress() const;
void setAddress(const string &);

// ...
};

Now imagine that one day for some reason, you decide to change fullName
to char *. All you have to do is modify someClass::getName() and
someClass::setName().
Had you reimplemented these member functions and in general allowed
access to fullName in all derived classes, you should modify all of them.
However there are cases when it is good to have this access. There is no
"silver bullet".
Have a look here about "silver bullets":

http://www.itworld.com/AppDev/710/lw...up/page_1.html
And here about "separate concerns":

http://www23.brinkster.com/noicys/docs/blk.htm

As TC++PL 3 says at the end of chapter 12:

"Keep the representations of distinct concepts distinct; §12.4.1.1.".

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
"Dave Townsend" <da********@comcast.net> wrote in message
news:g4********************@comcast.com...

"Daniel T." <po********@earthlink.net> wrote in message
news:po******************************@news2.east.e arthlink.net...
"away" <Gu*******@spambs.com> wrote:
1. It is showed in a book that a class has all member variables
declared
as protected. What's the reason for doing this?
Making a variable protected ensures that only the class and sub-classes
can modify the variable. The assumption is that sub-class writers will
be careful enough.

Personally, I feel that all variables should be declared private.


Can you enumerate why all variables should be private ? I've written
classes a a number
of times where I've made all variables private and had to write numerous
member functions
to allow access to the variables by the derived classes, it didn't seem

that there was much
advantage and a lot of extra code was needed to wrap the data member . It seems
to me that there are good reasons for making variables protected :-

1. if the derived classes would access them
2. modifying these variables does not require side effects.


It's generally a philosophical design issue. However, you must admit that a
protected getter/setter member function pair provides a little more
flexibility than a protected member variable. If you ever wanted the base
class to perform some sort of action (e.g. validation of the member's value)
when the member was changed, then the getter/setter approach would be very
beneficial. The cost is that there are a couple of extra member functions,
which may or may not be a problem for you, depending on your environment.

--
David Hilsee
Jul 22 '05 #7

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:cj***********@ulysses.noc.ntua.gr...
Dave Townsend wrote:
Can you enumerate why all variables should be private ? I've written
classes a a number
of times where I've made all variables private and had to write numerous
member functions
to allow access to the variables by the derived classes, it didn't seem that there was much
advantage and a lot of extra code was needed to wrap the data member . It seems
to me that there are good reasons for making variables protected :-

1. if the derived classes would access them
2. modifying these variables does not require side effects.

One value of the data-hiding is that it isolates the implementation from
the functionality.

So for example if you have
// Silly example

class someClass
{
string fullName;
// ...

public:

//...

string getName() const;
void setName(const string &newName);

// ...
};
class fullClass: public someClass
{
string address;
// ...

public:

// ...

string getAddress() const;
void setAddress(const string &);

// ...
};

Now imagine that one day for some reason, you decide to change fullName
to char *. All you have to do is modify someClass::getName() and
someClass::setName().
Had you reimplemented these member functions and in general allowed
access to fullName in all derived classes, you should modify all of them.
However there are cases when it is good to have this access. There is no
"silver bullet".
Have a look here about "silver bullets":

http://www.itworld.com/AppDev/710/lw...up/page_1.html
And here about "separate concerns":

http://www23.brinkster.com/noicys/docs/blk.htm

As TC++PL 3 says at the end of chapter 12:

"Keep the representations of distinct concepts distinct; §12.4.1.1.".

--
Ioannis Vranos

http://www23.brinkster.com/noicys


Yes, I agree, this would be a consideration to keep the members private.
But I
often come across the same pattern in GUI programming, the base class has a
bunch
of members, ( for instance the parent pointer to the parent widget of this
component,
or the "container" of this component). I find I seem to be mindlessly
writing accessor
functions which don't seem to add much value. Just a thought.

dave

Jul 22 '05 #8
Dave Townsend wrote:
Yes, I agree, this would be a consideration to keep the members private.
But I
often come across the same pattern in GUI programming, the base class has a
bunch
of members, ( for instance the parent pointer to the parent widget of this
component,
or the "container" of this component). I find I seem to be mindlessly
writing accessor
functions which don't seem to add much value. Just a thought.

So do you mean that those data members should be made public? What if
the base class changes some day?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

"David Hilsee" <da*************@yahoo.com> wrote in message
news:Ga********************@comcast.com...
"Dave Townsend" <da********@comcast.net> wrote in message
news:g4********************@comcast.com...

"Daniel T." <po********@earthlink.net> wrote in message
news:po******************************@news2.east.e arthlink.net... --
It's generally a philosophical design issue. However, you must admit that

aprotected getter/setter member function pair provides a little more
flexibility than a protected member variable. If you ever wanted the base
class to perform some sort of action (e.g. validation of the member's value)when the member was changed, then the getter/setter approach would be very
beneficial. The cost is that there are a couple of extra member functions,
which may or may not be a problem for you, depending on your environment.

--
David Hilsee

I think the keypoint is that if invalidation is required when accessing
member variables, then setters/getters would be the way to go, otherwise its
reasonable to consider accessing the
members directly in the derived class. A better example came to my mind, in
the project
I'm working on, we have a lot of interface pointers passed around.
Typically, an GUI
object will have a member variable which holds an interface pointer, it
doesn't seem to
reduce complexity by providing an access function in this case for the
derived classes,
all you can do is call methods on the interface, you cannot change the
pointer value.

Jul 22 '05 #10
Ioannis Vranos wrote:
So do you mean that those data members should be made
protected?
What if
the base class changes some day?

I am writing GUI applications in .NET,and all .NET classes come with
public, protected and private methods and properties(=member functions
again).

So if the internal representation (data members) some day change, there
is a good possibility the member functions will not.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:cj**********@ulysses.noc.ntua.gr...
Ioannis Vranos wrote:
So do you mean that those data members should be made


protected?
What if
the base class changes some day?

I am writing GUI applications in .NET,and all .NET classes come with
public, protected and private methods and properties(=member functions
again).

So if the internal representation (data members) some day change, there
is a good possibility the member functions will not.

--
Ioannis Vranos

http://www23.brinkster.com/noicys


No, that is not what I'm saying. If the data members are likely to change
or changing
their values could generate side effects, then having member functions is
the way to go.
In other situations it depends on the circumstances. I never said the data
members should
be public.

The pattern that keeps popping up in my line of work is a class contains a
member
variable which is an interface pointer (com like). In this situation, you
can only
call methods of the interface, not change the interface pointer value. So I
think in this
type of situation its ok to access this as a protected variable. ( or lets
be weaker, to
consider this for being a protected variable).

dave


Jul 22 '05 #12
Dave Townsend wrote:
No, that is not what I'm saying. If the data members are likely to change
or changing
their values could generate side effects, then having member functions is
the way to go.
In other situations it depends on the circumstances. I never said the data
members should
be public.

The pattern that keeps popping up in my line of work is a class contains a
member
variable which is an interface pointer (com like). In this situation, you
can only
call methods of the interface, not change the interface pointer value. So I
think in this
type of situation its ok to access this as a protected variable. ( or lets
be weaker, to
consider this for being a protected variable).

I suppose you mean that it looks messy to type member function calls for
this particular task? Well, hasn't your platform the notion of property?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #13
"Dave Townsend" <da********@comcast.net> wrote in message
news:RZ********************@comcast.com...

"David Hilsee" <da*************@yahoo.com> wrote in message
news:Ga********************@comcast.com...
"Dave Townsend" <da********@comcast.net> wrote in message
news:g4********************@comcast.com...

"Daniel T." <po********@earthlink.net> wrote in message
news:po******************************@news2.east.e arthlink.net... > --
It's generally a philosophical design issue. However, you must admit that a
protected getter/setter member function pair provides a little more
flexibility than a protected member variable. If you ever wanted the
baseclass to perform some sort of action (e.g. validation of the member's

value)
when the member was changed, then the getter/setter approach would be verybeneficial. The cost is that there are a couple of extra member functions,which may or may not be a problem for you, depending on your environment.

--
David Hilsee

I think the keypoint is that if invalidation is required when accessing
member variables, then setters/getters would be the way to go, otherwise

its reasonable to consider accessing the
members directly in the derived class. A better example came to my mind, in the project
I'm working on, we have a lot of interface pointers passed around.
Typically, an GUI
object will have a member variable which holds an interface pointer, it
doesn't seem to
reduce complexity by providing an access function in this case for the
derived classes,
all you can do is call methods on the interface, you cannot change the
pointer value.


I'm not sure that I understand what you mean. Do you mean

class Foo {
// ...
protected:
Interface * p_;
};

or

class InterfaceHolder {
// ...
private:
Interface * p_;
};

class Foo {
protected:
InterfaceHolder other_;
};

At any rate, I think that a class should try to hide information from
derived classes and other code. In order to hide information from derived
classes, I use protected sparingly and don't place anything other than
member functions in the "extended interface" presented to the derived
classes. If you don't treat derived classes as "special" when it comes to
information hiding, then the same reasoning that keeps you from making
something public will also keep you from making it protected. Of course,
occasionally, it's impossible to avoid "protected".

--
David Hilsee
Jul 22 '05 #14

"Daniel T." <po********@earthlink.net> wrote in message news:postmaster-
Making a variable protected ensures that only the class and sub-classes
can modify the variable.
Or can even read it.
Personally, I feel that all variables should be declared private.
This is correct. People who make things protected by default are only slightly less
sloppy than those who leave them public. There's good reason why the default access
control on classes is private.
2. Unnamed namespace and global scope, are they equivalent to each other?


No. A variable declared in an unnamed namespace can only be accessed
from the cpp file that contains it.


Actually, in the TRANSLATION UNIT in which it is defined. By and large C++
cares nothing about files, but it's the stream of the base file and all it's includes
together that matters.

Jul 22 '05 #15

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

Similar topics

4
by: Skip Montanaro | last post by:
(moving over from webmaster mailbox) scott> I'm sorry for bothering you, but I've tried to post to the Python scott> Tutor Mail List, tried to get someone from Bay PIggies to scott> respond, but...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
1
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
0
by: Jobs | last post by:
All answers to the below interview questions are at http://www.geocities.com/dotnetinterviews/ or you can download the complete answer zip file from...
24
by: tom c | last post by:
I have gotten great help in this forum. I am working by my self, developing with a new technology, and it would be extremely time consuming and frustrating without this forum. So who are the...
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Viviana Vc | last post by:
Hi all, I've read the WindowsVistaUACDevReqs.doc documentation and I have done different small tests on Vista to understand the bahaviour and now I have a few questions. 1) If I create a...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
16
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window to dispaly a set of questions with their answers. The users should be able to move to the next question by clicking on next button. I am going to use only one panel...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.