473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2696
"Hongzheng Wang" <wa******@mails.tsinghua.edu.cn> wrote in message
news:bq**********@news.yaako.com...
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.cn> schrieb im Newsbeitrag
news:bq**********@news.yaako.com...
// 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_preconditions();
f();
check_postconditions();
}

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
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...
6
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
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...
1
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...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
9
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. --...
7
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...
6
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,...
23
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 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.