Hello there,
I have the following code:
class Base {
public:
virtual void f() {cout << "Base::f()" << endl;}
virtual void f(int) {cout << "Base::f(int)" << 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. 12 1514
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(int)" << 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.
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 misapprehensions.
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)
On 2008-07-28 14:35:25 -0400, puzzlecracker <ir*********@gmail.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(int)" << 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)
On Jul 28, 2:37*pm, Pete Becker <p...@versatilecoding.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 misapprehensions.
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).
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)
On Jul 28, 2:42*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-07-28 14:35:25 -0400, puzzlecracker <ironsel2...@gmail.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(int)" << 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
On 2008-07-28 16:55:48 -0400, puzzlecracker <ir*********@gmail.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)
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
Pete Becker wrote:
On 2008-07-28 16:55:48 -0400, puzzlecracker <ir*********@gmail.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.
In article
<4e**********************************@y38g2000hsy. googlegroups.com>, da*****@mail.ru wrote:
On Jul 28, 2:37=A0pm, Pete Becker <p...@versatilecoding.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.
>
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.
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).
C++ gives the programmer choice, Java makes many choices for him. The
important question in this case is whether the choice C++ gives in this
case might lead to undiagnosed errors. Personally I'd like an "override"
keyword that must be present on overrides, as described here: http://slack.net/~ant/cpp/override.html
On Jul 28, 7:53 pm, dani...@mail.ru wrote:
I have the following code:
class Base {
public:
virtual void f() {cout << "Base::f()" << endl;}
virtual void f(int) {cout << "Base::f(int)" << 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.
Are you sure? I always thought that in Java, private meant
private; that since Derived::f() was private, it had nothing to
do with Base::f() (except, of course, that its presence would
prevent you from overriding Base::f()). (But I'm far from
sure.)
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?
Not exactly. You're calling Base::f(), which is public. It's
not considered relevant to access control that the function
which actually gets executed is Derived::f(). (It can't be
since access control is fully resolved at compile time, and the
compiler can't know which function will actually get executed.)
In other words, access control is always applied to the static
type.
In general, it's probably not a good idea to tighten access
controls in the derived class, because it's confusing; client
code can still call the function through an interface to the
base. On the other hand, in certain rare cases, the opposite
might be appropriate: overriding a private virtual function with
a public function in the derived class.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On 2008-07-28 20:41:12 -0400, Gennaro Prota <gennaro/pr***@yahoo.comsaid:
Pete Becker wrote:
>On 2008-07-28 16:55:48 -0400, puzzlecracker <ir*********@gmail.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?
Yup, hasty example. Sorry.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) {
|
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...
|
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...
|
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();
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
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...
| |