473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private access in an implementation still allows public access in an abstract interface

Please consider the following code:

class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};

class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};

void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;

abstract.Method ();
}
The implementation for Abstract::Metho d provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer might
find this surprising and puzzling. I, myself, am not sure what to
think of it.

The end effect is that the implementation for the Abstract class works
as expected. An instance of Concrete can be used as an Abstract
instance and it works as expected with respect to the Abstract
interface. Method is public for Abstract and can be accessed just
fine, even though it is private with respect to its Concrete
implementation.

However, it seems that there is something wrong, or at least worrisome
about the fact that this can be done.

What can people say about the correctness of this code?
If it is correct, what are the reasons classes are allow to behave this
way?

Feb 26 '06 #1
7 1919
<tr*********@ve rizon.net> wrote in message
news:11******** **************@ t39g2000cwt.goo glegroups.com
Please consider the following code:

class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};

class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};

void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;

abstract.Method ();
}
The implementation for Abstract::Metho d provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer
might find this surprising and puzzling. I, myself, am not sure what
to think of it.


What object a reference refers to cannot always be known at compile time but
may instead only be known at run time. Access rules are enforced at compile
time, so access rules cannot depend on the object referred to. Instead, they
depend on the type of the reference, since this is known at compile time.
The type of abstract is "reference to Abstract" and Method() is public in
Abstract. Accordingly, access must be allowed.

--
John Carson
Feb 26 '06 #2
John Carson wrote:

What object a reference refers to cannot always be known at compile time but
may instead only be known at run time. Access rules are enforced at compile
time, so access rules cannot depend on the object referred to. Instead, they
depend on the type of the reference, since this is known at compile time.
The type of abstract is "reference to Abstract" and Method() is public in
Abstract. Accordingly, access must be allowed.

--
John Carson


Thanks John. That makes sense.

Feb 26 '06 #3
On 2006-02-26, tr*********@ver izon.net <tr*********@ve rizon.net> wrote:
Please consider the following code:

class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};

class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};

void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;

abstract.Method ();
}
The implementation for Abstract::Metho d provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer might
find this surprising and puzzling. I, myself, am not sure what to
think of it.


It is this way because the alternative seemed worse. The C++ designers
deemed it would be horrible if changing the access specification of a
member function somewhere in your class hierarchy could alter the
resolution of a virtual function call, or cause your program to no
longer compile. Consider multiple inheritance:

class B {
public:
virtual void foo();
};

class X: public B {
private:
void foo();
};

class Y: public B {
public:
void foo();
};

class Fubar: public X, Y {
public:
void foo();
};

In this hierarchy, under a rule that made the private foo in X
inaccessible, there would be no ambiguity in the following call in
function fnagn. It would have to resolve to the public override in B.

void fnagn(Fubar* f)
{
f->foo();
}

But if the access specification of foo in X were then changed to
public, suddenly your code would not even compile.

So the actual rules make your code more robust, in theory. The call to
foo in fnagn is ambiguous, despite foo in X being private.

--
Neil Cerutti
Feb 27 '06 #4
Neil Cerutti wrote:
But if the access specification of foo in X were then changed to
public, suddenly your code would not even compile.


The other odious alternative enforces access at runtime. That would add
extra opcodes to all the virtual functions that don't need them.

The OP should research Design Patterns. Sometimes a method's access is a
valid technique.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 27 '06 #5
Phlip wrote:
Neil Cerutti wrote:
But if the access specification of foo in X were then changed to
public, suddenly your code would not even compile.
The other odious alternative enforces access at runtime. That would add
extra opcodes to all the virtual functions that don't need them.

The OP should research Design Patterns. Sometimes


changing!
a method's access is a
valid technique.


--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 27 '06 #6
On 2006-02-27, Neil Cerutti <le*******@emai l.com> wrote:
On 2006-02-26, tr*********@ver izon.net <tr*********@ve rizon.net> wrote:
It is this way because the alternative seemed worse. The C++ designers
deemed it would be horrible if changing the access specification of a
member function somewhere in your class hierarchy could alter the
resolution of a virtual function call, or cause your program to no
longer compile. Consider multiple inheritance:

class B {
public:
virtual void foo();
};

class X: public B {
private:
void foo();
};

class Y: public B {
public:
void foo();
};

class Fubar: public X, Y {
public:
void foo();
};

In this hierarchy, under a rule that made the private foo in X
inaccessible, there would be no ambiguity in the following call in
function fnagn. It would have to resolve to the public override in B.

void fnagn(Fubar* f)
{
f->foo();
}

It seems people got the point, but there were errors in my example code.

Fubar should not declare a function foo, and fnagn should receive
pointer to B.
But if the access specification of foo in X were then changed to
public, suddenly your code would not even compile.

So the actual rules make your code more robust, in theory. The call
to foo in fnagn is ambiguous, despite foo in X being private.

--
Neil Cerutti
You can't give him that cutback lane. He's so fast, and he sees
it so well. He can also run away from you if he gets a little
bit of crack. --Dick Lebeau
Mar 1 '06 #7

tr*********@ver izon.net wrote:
Please consider the following code:

class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};

class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};

void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;

abstract.Method ();
}
The implementation for Abstract::Metho d provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer might
find this surprising and puzzling. I, myself, am not sure what to
think of it.

The end effect is that the implementation for the Abstract class works
as expected. An instance of Concrete can be used as an Abstract
instance and it works as expected with respect to the Abstract
interface. Method is public for Abstract and can be accessed just
fine, even though it is private with respect to its Concrete
implementation.

However, it seems that there is something wrong, or at least worrisome
about the fact that this can be done.

What can people say about the correctness of this code?
If it is correct, what are the reasons classes are allow to behave this
way?


The rule of thumb is that only the names of things can be made private,
not the things themselves. I think the main reason for this is that
it's
not possible at (least currently) to have ironclad protection of
members
that could not be defeated with casts, and have no run-time overhead.

In the particular example you give, I think there are cases where it
could actually be useful. The purpose of private is for the designer
of the class to prevent class users from creating undesired
dependencies
on cretain specifics of the class definition. What the above code is
saying is "don't write any code for Concrete instances using 'method()'
that would not also work for all instances of Abstract".

Mar 1 '06 #8

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

Similar topics

6
3171
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
8
6834
by: Dave | last post by:
Hello all, Suppose that derived inherits privately from base. A base pointer may not be made to point at a derived object in this case. I understand that is exactly what is supposed to happen and I understand that this is explicitly dictated by the Standard. I'm trying to understand the diagnostic my compiler generates in this case though:
9
4653
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
12
2681
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a ) {cout<<"a.value="<<a.value<<endl; } private: int value;
11
2169
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
1
3708
by: Joel | last post by:
Why does this work: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args)
86
4660
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 method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
10
4810
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
9687
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, 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...
0
9543
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,...
0
10488
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, 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...
0
10257
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 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...
0
10029
tracyyun
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...
0
6808
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();...
0
5467
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...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
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...

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.