By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Can private methods be overriding?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.