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

private virtual functions - accessibility legality

P: n/a
Hi,
Private functions can be virtual in C++ ( unlike Java) ? What is the
use of private virtual functions ? Below is the code -

#include <iostream>

class Foo {
public:
virtual void test() { std::cout << "Foo::test()" << std::endl; }
};

class Bar : public Foo {
private:
void test() { std::cout << "Bar::test()" << std::endl; }
friend void friendToBar(Bar *);
};

void notFriendToBar (Foo *pFoo) {
pFoo->test();
}

void friendToBar (Bar *pBar) {
pBar->test();
}

int main(int argc, char *argv[]) {
Bar b;
friendToBar(&b);
notFriendToBar(&b);
}

Function Bar::test() is private to Bar. but stil we can access it
outside Bar in notFriendToBar function. Is it legal ? Is not private
functions are meant to be either accessible to friend or to its class
only ?
Oct 19 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Vijay Meena wrote:
Hi,
Private functions can be virtual in C++ ( unlike Java) ? What is the
use of private virtual functions ? Below is the code -

#include <iostream>

class Foo {
public:
virtual void test() { std::cout << "Foo::test()" << std::endl; }
};

class Bar : public Foo {
private:
void test() { std::cout << "Bar::test()" << std::endl; }
friend void friendToBar(Bar *);
};

void notFriendToBar (Foo *pFoo) {
pFoo->test();
}

void friendToBar (Bar *pBar) {
pBar->test();
}

int main(int argc, char *argv[]) {
Bar b;
friendToBar(&b);
notFriendToBar(&b);
}

Function Bar::test() is private to Bar. but stil we can access it
outside Bar in notFriendToBar function. Is it legal ? Is not private
functions are meant to be either accessible to friend or to its class
only ?
Your function is public in Foo and notFriendToBar() takes a Foo* as an
argument. I see no reason, why accessibility should kick in.
Best

Kai-Uwe Bux
Oct 19 '08 #2

P: n/a
But notFriendToBar() ultimately executes Bar::test() by using
polymorphism, which is a private function to Bar. Also, does C++
allows private function to be virtual (Unlike Java) ? I got the query
when I started learning Java recently.

Thanks,

Vijay
Oct 19 '08 #3

P: n/a
Vijay Meena wrote:
But notFriendToBar() ultimately executes Bar::test() by using
polymorphism, which is a private function to Bar.
a) Please quote the context of your reply (and do not top-post). The reason
is the news protocol which does _not_ ensure that the context of your
posting is visible otherwise.

To restore context, here is the code in question:

class Foo {
public:
virtual void test() { std::cout << "Foo::test()" << std::endl; }
};

class Bar : public Foo {
private:
void test() { std::cout << "Bar::test()" << std::endl; }
friend void friendToBar(Bar *);
};

void notFriendToBar (Foo *pFoo) {
pFoo->test();
}

int main(int argc, char *argv[]) {
Bar b;
notFriendToBar(&b);
}
b) What is _ultimately_ executed is of no concern. Accessibility in C++ is a
compile time concept that can be checked locally, i.e., when compiling
notFriendToBar().

Also, does C++ allows private function to be virtual (Unlike Java) ?
I don't know about Java, but C++ does allow private functions to be virtual.
It just so happens that test() is public in Foo() and notFriendToBar()
takes a Foo* as its argument.

I got the query when I started learning Java recently.
Huh? From whom? Why would anybody ask about C++ when teaching Java?
Best

Kai-Uwe Bux
Oct 19 '08 #4

P: n/a
On Oct 19, 6:48*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
a) Please quote the context of your reply (and do not top-post). The reason
is the news protocol which does _not_ ensure that the context of your
posting is visible otherwise.
Thanks, I will take care of it.
I don't know about Java, but C++ does allow private functions to be virtual.
It just so happens that test() is public in Foo() and notFriendToBar()
takes a Foo* as its argument.
Thanks again for the clarification.
I got the query when I started learning Java recently.

Huh? From whom? Why would anybody ask about C++ when teaching Java?
I am learning by self.
Oct 19 '08 #5

P: n/a
On Oct 18, 9:30 pm, Vijay Meena <vijay.me...@gmail.comwrote:
But notFriendToBar() ultimately executes Bar::test() by using
polymorphism, which is a private function to Bar. Also, does C++
allows private function to be virtual (Unlike Java) ? I got the query
when I started learning Java recently.

Thanks,

Vijay
accessibility of Bar::test() is not involved. Polymorphism offers a
different interface based on accesibilty offered in base class. There
are a number of Idioms that use private virtual member functions with
public non-virtual accessors, namely Non-Virtual Function Idiom.

#include <iostream>

class Foo
{
virtual void test() const { std::cout << "Foo::test()" <<
std::endl; }
public:
void doit() const
{
test();
}
};

class Bar : public Foo
{
void test() const { std::cout << "Bar::test()" << std::endl; }
friend void friendToBar(const Bar&);
};

void notFriendToBar (const Foo& foo)
{
// foo.test(); // error
foo.doit();
}

void friendToBar (const Bar& bar)
{
bar.test();
}

int main()
{
Bar b;
friendToBar(b);
notFriendToBar(b);
}

Oct 19 '08 #6

P: n/a
On Oct 19, 2:34*am, Vijay Meena <vijay.me...@gmail.comwrote:
Private functions can be virtual in C++ ( unlike Java) ? *What
is the use of private virtual functions ?
The usual pattern is to define the contractual interface in
public functions, then implement it in private functions and
data. The contractual interface remains the same (or should
remain the same) in all derived classes; you shouldn't be able
to override it. So any virtual functions are usually private
(although there are a number of exceptions---e.g. if there is an
inversion of the call sequence, and the interface doesn't define
any contract).
Below is the code -
#include <iostream>
class Foo {
public:
* * * * virtual void test() { std::cout << "Foo::test()" << std::endl; }
};
class Bar : public Foo {
private:
* * * * void test() { std::cout << "Bar::test()" << std::endl; }
* * * * friend void friendToBar(Bar *);
};
void notFriendToBar (Foo *pFoo) {
* * * * pFoo->test();
}
void friendToBar (Bar *pBar) {
* * * * pBar->test();
}
int main(int argc, char *argv[]) {
* * * * Bar b;
* * * * friendToBar(&b);
* * * * notFriendToBar(&b);
}
Function Bar::test() is private to Bar. but stil we can access
it outside Bar in notFriendToBar function. Is it legal ?
It's legal. It's a consequence of the other rules; this
particular use is not useful. (To date, I've found no useful
reason for restricting the overriding function more than the
function in the base class, but there's no real reason to ban it
either. In the end, most virtual functions are private to begin
with.)
>*Is not private functions are meant to be either accessible to
friend or to its class only ?
Yes, but there are ways of subverting this.

--
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
Oct 19 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.