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