I'm having a little trouble understanding why a derivative class cannot
access a protected member of the base class in the following code:
#include <stdio.h>
class CBase
{
protected:
int x;
public:
CBase () {x = 123;}
public:
operator int () {return x;}
};
class CDerived : public CBase
{
public:
CDerived ()
{
CBase* p = this;
p->x = 456; // error: cannot access protected member
}
};
void main ()
{
printf ("CBase=%d\nCDe rived=%d\n", CBase (), CDerived ());
}
I don't see why CDerived shouldn't be able to access the integer "x" in
CBase without having CBase to declare CDerived as a friend. Can someone
explain to me why this is? Thank you.
Jul 22 '05
12 1532
Matthias Käppler wrote: Dietmar Kuehl wrote: The above program will compile with several different C++ library implementations - but not with a standard conforming one. That doesn't really make sense.
It doesn't? Well, I think it really does: the replacement headers were
a nice idea which did not work out in practise. The reality is that
the C++ library implementer has no control over the ".h" versions but
is required to make sure that certain definitions from these headers
are made in namespace 'std'. The only available approach short of
keeping both versions in sync (which is not viable at all) is
something like this:
| // <cstdio>
| namespace std {
| #include "/usr/include/stdio.h"
| }
| // <stdio.h>
| #include <cstdio>
| using std::printf;
| // ...
(assuming the C version can be included from the file
/usr/include/stdio.h). However, this does not work because there are
loads of things defined in the standard C headers which are unknown
to either the C or the C++ standard and which other headers, e.g.
<unistd.h> rely upon being available in the global namespace.
The intention of putting away the declarations of the C library into
namespace 'std' was all noble - but it simply is not workable under
realistic conditions. Effectively, most implementations actually use
an approach like this:
| // <cfile>
| #include <stdio.h>
| namespace std {
| using ::printf;
| // ...
| }
.... and leave <stdio.h> unchanged (well, not really: it is still
necessary to add a few overloads to some of the headers e.g. for
const correctness but these declarations can easily be bolted on).
From what I know, the <cxyz> headers are the ISO-C++ counterparts to
the classic xyz.h headers and were introduced in the standardization
process to make clear which are C headers and which are not. It's actually
highly encouraged to use them whenever you want to include C headers.
Yup, I know. Actually, I have spread this bad recommendation myself
in the past. At this time I was convinced that I can implement the C++
headers in a conforming way - and I can, as long as you don't want to
include any non-standard headers like <unistd.h>, too (well, actually,
<ctime> really gave me a headache when trying to encapsulate glibc's
<time.h> for some reason). Since the non-standard headers are
generally necessary in real live in some translation units, you can
only implement conforming C++ <c...> headers if you also have
control over the ".h" headers or at least their contents. I'm not
aware of any C++ library vendor who really has.
This is directly copied from <cstdio>:
// // ISO C++ 14882: 27.8.2 C Library files //
/** @file cstdio * This is a Standard C++ Library file. You should @c #include this
file * in your programs, rather than any of the "*.h" implementation
files. * * This is the C++ version of the Standard C Library header @c
stdio.h, * and its contents are (mostly) the same as that header, but are
all * contained in the namespace @c std. */
I know that not all C++ library implementations are able to tag
all C definitions into namespace 'std': that's an easy one for
someone who has implemented most of the standard C++ library and
whose implementation does not do it for practical reasons. I'm
sure that my implementation is not the only one. Actually, there
is an open issue concerning this exact stuff (see
<http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#456 >).
Although I recommended differently in the past, I recommend that
you use the ".h" version for the reasons given before.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
I wasn't aware of this problem. Thanks.
Dietmar Kuehl wrote: Matthias Käppler wrote: Dietmar Kuehl wrote: > The above program will compile with several different C++ library > implementations - but not with a standard conforming one.
That doesn't really make sense.
It doesn't? Well, I think it really does: the replacement headers were a nice idea which did not work out in practise. The reality is that the C++ library implementer has no control over the ".h" versions but is required to make sure that certain definitions from these headers are made in namespace 'std'. The only available approach short of keeping both versions in sync (which is not viable at all) is something like this:
| // <cstdio> | namespace std { | #include "/usr/include/stdio.h" | }
| // <stdio.h> | #include <cstdio> | using std::printf; | // ...
(assuming the C version can be included from the file /usr/include/stdio.h). However, this does not work because there are loads of things defined in the standard C headers which are unknown to either the C or the C++ standard and which other headers, e.g. <unistd.h> rely upon being available in the global namespace.
The intention of putting away the declarations of the C library into namespace 'std' was all noble - but it simply is not workable under realistic conditions. Effectively, most implementations actually use an approach like this:
| // <cfile> | #include <stdio.h> | namespace std { | using ::printf; | // ... | }
... and leave <stdio.h> unchanged (well, not really: it is still necessary to add a few overloads to some of the headers e.g. for const correctness but these declarations can easily be bolted on).
From what I know, the <cxyz> headers are the ISO-C++ counterparts to the classic xyz.h headers and were introduced in the standardization process to make clear which are C headers and which are not. It's actually highly encouraged to use them whenever you want to include C headers.
Yup, I know. Actually, I have spread this bad recommendation myself in the past. At this time I was convinced that I can implement the C++ headers in a conforming way - and I can, as long as you don't want to include any non-standard headers like <unistd.h>, too (well, actually, <ctime> really gave me a headache when trying to encapsulate glibc's <time.h> for some reason). Since the non-standard headers are generally necessary in real live in some translation units, you can only implement conforming C++ <c...> headers if you also have control over the ".h" headers or at least their contents. I'm not aware of any C++ library vendor who really has.
This is directly copied from <cstdio>:
// // ISO C++ 14882: 27.8.2 C Library files //
/** @file cstdio * This is a Standard C++ Library file. You should @c #include this file * in your programs, rather than any of the "*.h" implementation files. * * This is the C++ version of the Standard C Library header @c stdio.h, * and its contents are (mostly) the same as that header, but are all * contained in the namespace @c std. */
I know that not all C++ library implementations are able to tag all C definitions into namespace 'std': that's an easy one for someone who has implemented most of the standard C++ library and whose implementation does not do it for practical reasons. I'm sure that my implementation is not the only one. Actually, there is an open issue concerning this exact stuff (see <http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#456 >).
Although I recommended differently in the past, I recommend that you use the ".h" version for the reasons given before. -- <mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/> <http://www.contendix.c om> - Software Development & Consulting
xxx wrote: The intention was to have access to CBase's protected member from CDerived without having to write a bunch of public accessors in CBase--it would defeat the purpose to be protected.
A member function of a derived class has access to the protected members
of objects it knows to be derived. It does not have access to protected
members of sibling classes. I think this is a reasonable approach.
Otherwise, the protection would be rather thin: a could easily derive
from base and access protected members from static functions of his
derived class without ever even creating an object! This would be
practically useless. If you need to access base members of sibling
classes, you effectively need public access.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ultra |
last post by:
Hi Export,
I want to write a class to deal with all db tier with different platform,
but when I using a DbAdapter (superclass) pointer to point a OleDBAdapter
(subclasse) instance, it said the selectCommand is protected.
How can I do that polymorphism?
below is my class:
public class db
{
|
by: Fao |
last post by:
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
Here is the code:
#include <iostream>
|
by: Jack |
last post by:
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.
I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.
I pass an array of "Base" classes...
|
by: n355a |
last post by:
hi everyone....if anyone has time...
can anyone help me with virtual functions...
I have class student, Graduate, Undergraduate. There's a printInfo() in each class, and the one in student is made pure virtual. I had to create a vector of students that had a combination of Graduates and Undergraduates in main. Then I had to pass the vector to a function. In that function theres a loop that goes through each students and prints their...
|
by: maxgross |
last post by:
Hi everyone,
ive got a problem with polymorphism. When i compile the code an error
occures:
error: 'class QtElement' has no member named 'DisplayQtElement'
I asked my friends and nobody could help me, all said it has to work.
I work with wxWidgets 2.6.3 under Linux.
| |
by: lorenzon |
last post by:
I've run into a problem in some code involving two class hierarchies
that I can't figure out: I have an event hierachy topped with an
interface, let's say-
IEvent,
EventA : IEvent,
EventB : IEvent
And some handlers-
|
by: TamusJRoyce |
last post by:
Hello. This is my first thread here.
My problem has probably been came across by a lot of people, but tutorials and things I've seen don't address it (usually too basic).
My problem is that I would like to use Abstraction for a "plug-in" like interface to classes.
class ThreadHandle
{
/* stuff here not yet dealing with threads */
|
by: weaknessforcats |
last post by:
Introduction
Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions.
This article uses a simple example hierarchy which you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will seriously limit your ability to use hierarchies like the example in a real program. Then, the article...
|
by: feel |
last post by:
Hi,All
I am sure it's an old question. But I just find a interesting design
about this: Polymorphism without virtual function in a C++ class.
My solution is for some special case, trust me, very special.
1 single root class tree
2 the leaf(lowest level) classes are sealed which means we should not
inherite class from them.
3 PImpl idiom. There is only one data mumber in root class and there
is no any other data mumber in child class and...
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |