473,386 Members | 1,820 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,386 software developers and data experts.

private virtual

1
Hi,
How does private virtual works?

For example if I define:
Expand|Select|Wrap|Line Numbers
  1. class Base{
  2.   private:
  3.      virtual void print(){cout << "Base";}
  4. public:
  5.     void printRes() { print();}
  6. };
  7.  
  8. class Derived:public base{
  9.    void print(){cout << "Derived";}
  10.  
  11. } ;
  12. int main(){
  13. Base * b = new Derived();
  14. b->printRes()();
  15. }
  16.  
will print "Derived"
Sep 5 '09 #1
1 1318
weaknessforcats
9,208 Expert Mod 8TB
The thing to remember with virtual functions is that the access specifiers like public, private and protected are ignored.

That allows a derived function to override a base function based solely on the function prototype.

In your case a private virtual function can be called only by a member function of that class. Therefore, your private virtual base class method can be called only by a base class method.

However, if you have created a derived object and are accessing it using a base class pointer, then a public base class method can call a private base class method and if your derived class has overridden that private base class method what actually gets called is the derived class method. This is how you call private derived methods using a base class pointer.

The common usage is to implement a hook. Suppose a base method is to return a pointer to something but the something is not created by the base class but by a derived class. What you do is write a private virtual method in the base class that returns zero for the pointer. Therefore, if you create a base object and call a public method that calls this private virtual method you will get a zero indicating "not supported". However, if a derived class overrides this virtual base class method and you create a derived object and access it using a base class pointer, when the public base class method calls this private virtual base class method the call actually goes to the derived class method where the correct pointer is returned (remember it's a derived object).

Try it out. Done correctly, the derived class need only have private virtual methods forcing everyone to use the public base class methods. This is good design since the public base class methods are the interface to the polymorphic hierarchy.
Sep 5 '09 #2

Sign in to post your reply or Sign up for a free account.

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 ;
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
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();}
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?
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
6
by: Vijay Meena | last post by:
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()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.