473,659 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding methods with lower permission

Hello there,
I have the following code:

class Base {
public:
virtual void f() {cout << "Base::f()" << endl;}
virtual void f(int) {cout << "Base::f(in t)" << endl;}
};
class Derived : public Base {
void f() {cout << "Derived::f ()" << endl;}
};

int main() {
Base *ptr = new Derived();
ptr->f();
ptr->f(1);

return 0;
}

As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.
Jul 28 '08 #1
12 1546
On Jul 28, 1:53*pm, dani...@mail.ru wrote:
Hello there,
I have the following code:

class Base {
public:
* virtual void f() {cout << "Base::f()" << endl;}
* virtual void f(int) {cout << "Base::f(in t)" << endl;}

};

class Derived : public Base {
* void f() {cout << "Derived::f ()" << endl;}

};

int main() {
* Base *ptr = new Derived();
* ptr->f();
* ptr->f(1);

* return 0;

}

As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.
That's allowed in C++. However, you're not allowed to provide more
accessibility than provided by super class, in other words making it a
private member a public in subclass is prohibited under the current
standard.
Jul 28 '08 #2
On 2008-07-28 13:53:41 -0400, da*****@mail.ru said:
>
As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.
Java creates many misapprehension s.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.
Neither. Base::f is public, and Derived::f is private, just like you
said they should be. Access is checked statically. That is, ptr->f()
can call f because f is public in Base. Try it with a pointer to
Derived.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 28 '08 #3
On 2008-07-28 14:35:25 -0400, puzzlecracker <ir*********@gm ail.comsaid:
On Jul 28, 1:53Â*pm, dani...@mail.ru wrote:
>Hello there,
I have the following code:

class Base {
public:
Â* virtual void f() {cout << "Base::f()" << endl;}
Â* virtual void f(int) {cout << "Base::f(in t)" << endl;}

};

class Derived : public Base {
Â* void f() {cout << "Derived::f ()" << endl;}

};

int main() {
Â* Base *ptr = new Derived();
Â* ptr->f();
Â* ptr->f(1);

Â* return 0;

}

As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.

That's allowed in C++. However, you're not allowed to provide more
accessibility than provided by super class, in other words making it a
private member a public in subclass is prohibited under the current
standard.
No, it's not. I think you're confusing this with the rule about using
directives, which are not allowed to increase access.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 28 '08 #4
On Jul 28, 2:37*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-07-28 13:53:41 -0400, dani...@mail.ru said:
As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.

Java creates many misapprehension s.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.

Neither. Base::f is public, and Derived::f is private, just like you
said they should be. Access is checked statically. That is, ptr->f()
can call f because f is public in Base. Try it with a pointer to
Derived.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Well, that's obvious, access is checked only at compile-time. Compiler
would have no idea which object is being used at run-time. Using
pointer to Derived would produce an error - as expected. I understand
all of that.

But the fact is - I am able to call a private function! Compiler lets
me override the function and give it lower permissions, which,
logically shouldn't happen (Java compiler would choke on that piece of
code).
Jul 28 '08 #5
On 2008-07-28 15:04:41 -0400, da*****@mail.ru said:
>
But the fact is - I am able to call a private function! Compiler lets
me override the function and give it lower permissions, which,
logically shouldn't happen (Java compiler would choke on that piece of
code).
Why shouldn't it happen? You said you wanted to override f, and you
said you don't want outsiders to be able to call Derived::f directly.
The two aren't inconsistent from a language perspective. If you don't
want that, don't do it. <g.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 28 '08 #6
On Jul 28, 2:42*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-07-28 14:35:25 -0400, puzzlecracker <ironsel2...@gm ail.comsaid:
On Jul 28, 1:53*pm, dani...@mail.ru wrote:
Hello there,
I have the following code:
class Base {
public:
* virtual void f() {cout << "Base::f()" << endl;}
* virtual void f(int) {cout << "Base::f(in t)" << endl;}
};
class Derived : public Base {
* void f() {cout << "Derived::f ()" << endl;}
};
int main() {
* Base *ptr = new Derived();
* ptr->f();
* ptr->f(1);
* return 0;
}
As you can see, I overridden Base::f() from public, to private access
permission. Being the Java guy that I am, I expected a compiler error.
But to my surprise, the program ran fine, and called the private
version? Is this a compiler bug (I use g++), or is the function
explicitly converted to public? Thank you.
That's allowed in C++. However, you're not allowed to provide more
accessibility than provided by super class, in other words making it a
private member a public in subclass is prohibited under the current
standard.

No, it's not. I think you're confusing this with the rule about using
directives, which are not allowed to increase access.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Argh, I see. Actually I wasn't. Would you explain the rule about
using
directives?

Thanks
Jul 28 '08 #7
On 2008-07-28 16:55:48 -0400, puzzlecracker <ir*********@gm ail.comsaid:
Would you explain the rule about
using
directives?
class Base
{
void f();
public:
void g();
};

class Derived: public Base
{
void f(int); // hides Base::f
void g(int); // hides Base::g
// make base version visible (ignore duplication):
using Base::f; // OK: same access
using Base::g; // OK: more restricted access
protected:
using Base::f; // error: less restricted access
using Base::g; // OK: more restricted access
public:
using Base::f; // error: less restricted access
using Base::g; // OK: same access
};

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 28 '08 #8
Hi,

da*****@mail.ru schrieb:
>Neither. Base::f is public, and Derived::f is private, just like you
said they should be. Access is checked statically. That is, ptr->f()
can call f because f is public in Base. Try it with a pointer to
Derived.

Well, that's obvious, access is checked only at compile-time. Compiler
would have no idea which object is being used at run-time. Using
pointer to Derived would produce an error - as expected. I understand
all of that.

But the fact is - I am able to call a private function! Compiler lets
me override the function and give it lower permissions, which,
logically shouldn't happen (Java compiler would choke on that piece of
code).
it is quite common that an implementation of an interface (in fact a
pure function) should not be called directly. The standard gives you a
chance to restrict this.
Marcel
Jul 28 '08 #9
Pete Becker wrote:
On 2008-07-28 16:55:48 -0400, puzzlecracker <ir*********@gm ail.comsaid:
>Would you explain the rule about
using
directives?

class Base
{
void f();
public:
void g();
};

class Derived: public Base
{
void f(int); // hides Base::f
void g(int); // hides Base::g
// make base version visible (ignore duplication):
using Base::f; // OK: same access
using Base::g; // OK: more restricted access
protected:
using Base::f; // error: less restricted access
using Base::g; // OK: more restricted access
public:
using Base::f; // error: less restricted access
using Base::g; // OK: same access
};
Where did you get this from? First, f isn't accessible in Derived, and
that's an error. All the rest is OK; aren't using declarations (and
not "directives ") intended to allow what was previously made with
access declarations?

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Jul 29 '08 #10

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

Similar topics

2
3768
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
3
3788
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for most operators, they are conspicuously absent for the logical "or" and "and". I'm guessing that the reason for this is that these operators short-circuit if their first operand answers the whole question? Would it be possible to allow...
5
2724
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For example, class base {
5
2780
by: zero | last post by:
I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code: class BaseClass { protected: void method2(); public: void method1();
1
1605
by: Burkhard Perkens-Golomb | last post by:
Hi, I've a question about XML documentation of overridding methods and methods implementing an interface declaration: These methods are normally documented in the base class or interface. So I don't want to write new documentation when overriding or implementing. But if I leave out documentation and compile my assembly with "/doc" the compiler warns me about missing documentation. How can I tell the compiler that these methods don't...
4
1927
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
2
3599
by: ESPNSTI | last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month. My experience has been mainly with Delphi 5 (not .Net). What I'm looking for is for a shortcut way to override a property without actually having to reimplement the get and set methods. In other words, override the the property without changing the functionality of the base property and without explicitly having to reimplement the get and set methods to make it do...
17
2909
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
10
105177
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
8332
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
8851
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...
1
8525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
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
4175
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...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1737
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.