473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

private virtual functions - accessibility legality

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
6 3205
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
6
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" <<...
20
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
3
by: Brian Cryer | last post by:
I posted this question recently to microsoft.public.dotnet.languages.vb but didn't get any answer. I'm hoping that someone here will be able to help me. I'm working on a project using VB.NET...
7
by: Alex Vinokur | last post by:
I tried to build program with partial implementation. However a linker generates errors. Is there any approach that enables to use partial implementation for similar purposes? ------ foo.cpp...
3
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.