private virtual functions - accessibility legality

V

Vijay Meena

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 ?
 
K

Kai-Uwe Bux

Vijay said:
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
 
V

Vijay Meena

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
 
K

Kai-Uwe Bux

Vijay said:
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
 
V

Vijay Meena

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.
Huh? From whom? Why would anybody ask about C++ when teaching Java?

I am learning by self.
 
S

Salt_Peter

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);
}
 
J

James Kanze

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top