473,624 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can private methods be overriding?

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 {
private:
virtual void f() {
//some codes
}
public:
void ff() {
f();
//invoke the f()
}
};

class derived : public base {
public:
virtual void f() {
//some other codes
}
};

int main()
{
base * pobj = new derived;
pobj->f();
//which method will be invoked here?
//the base's f() or derived's one?
return 0;
}

--
Hongzheng Wang
Department of Electronics Engineering
Tsinghua University
wa******@mails. tsinghua.edu.cn

Jul 22 '05 #1
5 2721
"Hongzheng Wang" <wa******@mails .tsinghua.edu.c n> wrote in message
news:bq******** **@news.yaako.c om...
I have a problem about the overriding of private methods of base
class.
What problem?
That is, if a method f() of base class is private, can the derived
class overriding f() be overriding?
Sure can.

For example,

class base {
private:
virtual void f() {
//some codes
}
public:
void ff() {
f();
//invoke the f()
}
};

class derived : public base {
public:
virtual void f() {
//some other codes
}
};

int main()
{
base * pobj = new derived;
pobj->f();
//which method will be invoked here?
//the base's f() or derived's one?
None. base::f is private, the program won't compile. However,
if you write

pobj->ff();

then derived::f() will be invoked from base::ff.
return 0;
}

Victor
Jul 22 '05 #2
I'm very very sorry for spell error.
I mean pobj->ff();
not pobj->f(); for accessiblity.

Hongzheng Wang wrote:
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 {
private:
virtual void f() {
//some codes
}
public:
void ff() {
f();
//invoke the f()
}
};

class derived : public base {
public:
virtual void f() {
//some other codes
}
};

int main()
{
base * pobj = new derived;
pobj->f(); pobj->ff(); //which method will be invoked here?
//the base's f() or derived's one?
return 0;
}

--
Hongzheng Wang
Department of Electronics Engineering
Tsinghua University
wa******@mails. tsinghua.edu.cn

Jul 22 '05 #3
"Hongzheng Wang" <wa******@mails .tsinghua.edu.c n> schrieb im Newsbeitrag
news:bq******** **@news.yaako.c om...
// snip
For example,

class base {
private:
virtual void f() {
//some codes
}
public:
void ff() {
f();
//invoke the f()
}
};

class derived : public base {
public:
virtual void f() {
//some other codes
}
};

int main()
{
base * pobj = new derived;
pobj->f(); I suppose you mean pobj->ff() here
//which method will be invoked here?
//the base's f() or derived's one?
return 0;
}

Indeed you discovered a well known pattern to establish pre and
postcondistions . Your function
base::ff() could then look like this:
void base::ff()
{
check_precondit ions();
f();
check_postcondi tions();
}

Some say most or even every virtual function should be made private,
optionally accompanied by a
non virtual public function in the base class.

HTH

Norbert
Jul 22 '05 #4
Whether a function is public, private or protected is orthogonal to
its being virtual.

In fact, many believe there should be no public virtual functions:
That virtual functions should be private, and in some rare cases
protected (only when the overriding function in a derived class needs
to call the overridden function in its parent class); and that
non-virtual public functions should be provided, calling the private
virtual functions, where private virtual functions need to be
publically accessible. I can't remember exactly the rationale for
this discipline, but I've come to apply it regularly, anyhow. It just
seems cleaner to me.
Cheers!
Jul 22 '05 #5
Dan W. wrote:
Whether a function is public, private or protected is orthogonal to
its being virtual.

In fact, many believe there should be no public virtual functions:
That virtual functions should be private, and in some rare cases
protected (only when the overriding function in a derived class needs
to call the overridden function in its parent class); and that
non-virtual public functions should be provided, calling the private
virtual functions, where private virtual functions need to be
publically accessible. I can't remember exactly the rationale for
this discipline, but I've come to apply it regularly, anyhow. It just
seems cleaner to me.
Cheers!

Thank you all! Your help are very helpful to me :-)

In fact, this problem is due to a similar one in my friend's
problem about Java. I found his java program does not run as
I imagine (of course think in C++).

Thank you.

--
Hongzheng Wang
Department of Electronics Engineering
Tsinghua University
wa******@mails. tsinghua.edu.cn

Jul 22 '05 #6

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

Similar topics

4
10641
by: Paul MG | last post by:
Hi I have been in the habit, when extracting a private "helper" method from a public method, of marking it "static" wherever possible -- that is, when it does not reference instance variables or other non-static methods. My rationale is that when browsing the source, such methods can be initially ignored, as they can have no side-effect on the instance. However, i have noticed that it does not appear conventional in the
6
3154
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 {
5
11310
by: David Rubin | last post by:
Is there ever a good reason to declare private non-virtual member functions in a class definition? As far as private virtual function are concerned, my understanding is that, if you have a (public) function which calls an private virtual function in the base class, the behavior can be changed by overriding the private virtual function in the derived class (but leaving the calling function unchanged). This at least seems useful. /david
1
448
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when having private inheritance then when you have public inheritance. I just want to have you opinion about this matter.
32
4503
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
9
2062
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
7
3726
by: Alex Vinokur | last post by:
I tried to build program with partial implementation. However a linker generates errors. Is there any approach that enables to use partial implementation for similar purposes? ------ foo.cpp --- struct Base { virtual void foo1() = 0; virtual void foo2() = 0;
6
4403
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans, Karthik Balaguru
23
2629
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class Add_Molecule_Req: public Action_Request{ // ...... };
0
8242
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
8681
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
8341
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
7170
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...
1
6112
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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
4084
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
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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 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.