C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Encapsulation of variables

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Apr 18, 2006 5:06 pm    Post subject: Encapsulation of variables Reply with quote



I' migrating some code to gcc 4, and I've had to put in rather a lot
of "using" statements into classes that derive from a base class. This
is a pain in the butt, for as far as I can tell no advantage, but what
has scared me more is that I seem to be able to make my private
variables public using this mechanism. That is:

class A {
private: int _fred; /// no-one can see fred directly
};

class B : public A {
public : using A::_fred; /// but this magically turns A::_fred to be
public!
}

int main(int argc, char** argv)
{
B b;
b._fred = 10; // you can do this
return 0;
}

Is this really part of the standard? From reading it, I think it is,
but I can't quite believe what I'm reading! This seems insane!

(You might be able to tell from my madness that we've had a minor
nightmare migrating to gcc 4, and even now it compiles, it's producing
much slower code. I'm not a happy bunny even though it's easter, but I
guess that's not relevant here.)

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Hyman Rosen
Guest





PostPosted: Tue Apr 18, 2006 7:06 pm    Post subject: Re: Encapsulation of variables Reply with quote



philparsonage (AT) hotmail (DOT) com wrote:
Quote:
Is this really part of the standard?

No. Perhaps you could post the code that made
you think this was necessary?

(I'm thinking that two-phase template name lookup
is going to be involved, but let's wait and see.)

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Wed Apr 19, 2006 12:06 am    Post subject: Re: Encapsulation of variables Reply with quote



philparsonage (AT) hotmail (DOT) com ha scritto:
Quote:

Is this really part of the standard? From reading it, I think it is,
but I can't quite believe what I'm reading! This seems insane!


No, it isn't part of the standard and my copy of gcc 4.0.2 get it right,
by producing the error message "error: ‘int A::_fred’ is private". If
the code you posted compiles as-is (something I really doubt) there's
something very wrong with your configuration. Could you double-check,
please?

Ganesh

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Guest






PostPosted: Wed Apr 19, 2006 4:06 am    Post subject: Re: Encapsulation of variables Reply with quote

philparsonage (AT) hotmail (DOT) com wrote:

Quote:
class A {
private: int _fred; /// no-one can see fred directly
};

class B : public A {
public : using A::_fred; /// but this magically turns A::_fred to be
public!
}

int main(int argc, char** argv)
{
B b;
b._fred = 10; // you can do this
return 0;
}

Is this really part of the standard?

No, the using declaration in B should not compile, since B has no
access to private members of A. It would be legal if _fred were
protected rather than private, of course.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Guest






PostPosted: Wed Apr 19, 2006 5:06 pm    Post subject: Re: Encapsulation of variables Reply with quote

OK, so maybe my example wasn't the best, I typed a private instead of a
protected. The actual code (which compiles using 4.0.2, or 3.3.6) is:

#include <stdlib.h>

class A {
protected: int _fred;
/// no-one can see fred directly
};

class B : public A {
public : using A::_fred;
/// but this magically turns A::_fred to be public!
};

int main(int argc, char** argv)
{
B b;
b._fred = 10; // you can do this
return 0;
}

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
SuperKoko
Guest





PostPosted: Wed Apr 19, 2006 6:06 pm    Post subject: Re: Encapsulation of variables Reply with quote

philparsonage (AT) hotmail (DOT) com wrote:
Quote:
OK, so maybe my example wasn't the best, I typed a private instead of a
protected. The actual code (which compiles using 4.0.2, or 3.3.6) is:

#include <stdlib.h

class A {
protected: int _fred;
/// no-one can see fred directly
};

class B : public A {
public : using A::_fred;
/// but this magically turns A::_fred to be public!
};

int main(int argc, char** argv)
{
B b;
b._fred = 10; // you can do this
return 0;
}

This code is fine.

class B derives from class A, and so, is perfectly aware of all
protected members of A.
Thus, it can expose them to everybody.
That's not much different from:
class B : public A {
public : int& fred() {return _fred;}
};

Similarly, using directives can be used to expose privately inherited
members.
class B : private A
public: using A::_fred;
}

Of course, having a public data member in a non-aggregate class, is
usually a sign of bad design... But the point here was the correctness
of data hiding.

The morale here, is that "protected" is nearly equivalent, in terms of
contract, to "public".
A "protected" member is NOT private!
If you write a class with protected members, you make the contract that
you'll not change these members in future...

That is also why, protected data members are a bad idea.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Thu Apr 20, 2006 4:06 am    Post subject: Re: Encapsulation of variables Reply with quote

philparsonage (AT) hotmail (DOT) com ha scritto:
Quote:
OK, so maybe my example wasn't the best, I typed a private instead of a
protected. The actual code (which compiles using 4.0.2, or 3.3.6) is:

#include <stdlib.h

class A {
protected: int _fred;
/// no-one can see fred directly
};

AHAH! That's *completely* different from the previous example! Notice
that your comment "no-one can see _fred directly" is wrong: classes
derived from A *can* see _fred directly. That's exactly the difference
between private and protected.

Quote:
class B : public A {
public : using A::_fred;
/// but this magically turns A::_fred to be public!
};

As B can see _fred directly, it can do whatever it wants, including
making it public. The code is perfectly fine and shows a seldom used but
legal idiom of the using declaration at class scope.

Ganesh

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.