Can private methods be overriding?

H

Hongzheng Wang

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;
}
 
H

Hongzheng Wang

I'm very very sorry for spell error.
I mean pobj->ff();
not pobj->f(); for accessiblity.
 
V

Victor Bazarov

Hongzheng Wang said:
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
 
N

Norbert Riedlin

// 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
 
D

Dan W.

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!
 
H

Hongzheng Wang

Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top