473,473 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Private method exposed?

I was wondering why something like this is allowed by the standard.

--- code ---

// Implementation ommitted.

class Base
{
public:
virtual void someFunction ( void ) ;
}

class Derived
{
private:
void someFunction ( void ) ;
}
int main ( void )
{
Derived d ;
Base* pb ;

pb = &d ;
pb->someFunction( ) ; // This calls Derived :: someFunction( )!

}
Now, Derived's private function is made available to the world through a
pointer to the base!

Jul 22 '05 #1
1 1538
On Sat, 3 Apr 2004 01:38:51 +0800, "Maluk" <ma***@fromru.com> wrote:
I was wondering why something like this is allowed by the standard.

--- code ---

// Implementation ommitted.

class Base
{
public:
virtual void someFunction ( void ) ;
}

class Derived
{
private:
void someFunction ( void ) ;
}
int main ( void )
{
Derived d ;
Base* pb ;

pb = &d ;
pb->someFunction( ) ; // This calls Derived :: someFunction( )!

}
Now, Derived's private function is made available to the world through a
pointer to the base!


Yes, but your base class specification is misrepresenting the nature of
your hierarchy. You can't really say a Derived "is a" Base if it doesn't
provide Base functionality directly; i.e., you can't say:

Derived d;
d.someFunction();

So if you want to provide a hidden implementation of something, use a
virtual dispatch scheme:

#include <iostream>
using namespace std;

class Base
{
public:
void someFunction()
{
someFnImpl();
}

protected:
virtual void someFnImpl() = 0; // or it may contain
// common functionality
};

class Derived1 : public Base
{
private:
void someFnImpl ()
{
cout << "Derived1::someFnImpl()" << endl;
}
};

class Derived2 : public Base
{
private:
void someFnImpl ()
{
cout << "Derived2::someFnImpl()" << endl;
}
};
int main ()
{
Derived1 d1 ;
Derived2 d2;

Base* pb;

pb = &d1;
pb->someFunction( ) ;
pb = &d2;
pb->someFunction( ) ;

return 0;
}
Output:
Derived1::someFnImpl()
Derived2::someFnImpl()

-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Yin | last post by:
I am an avid user of unittest and write tests for everything. Every so often, I write a class that has some private methods that I only want to be called from within the class. I usually write...
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
8
by: Dave A | last post by:
I have a class called 'PrimaryKey' that represents the primary key of a table. PrimaryKeys can only be created and the class only implements .ToString(). The PrimaryKey class internally stores...
2
by: Jon Paal | last post by:
In a 2.0 vb code class, I have a private property and a shared public function The property value has already been passed into the class, now I am trying to use a public finction which also needs...
7
by: Dick | last post by:
I have some simple (private) types... Friend Enum Types Value1 Value2 End Enum Friend Structure Detail Dim Type As Types End Structure
6
by: WXS | last post by:
I know this sounds contrary to the idea of an interface, but read this and see what you think. ----------------------------------------------------------------------------------------- It would be...
4
by: rognon | last post by:
Hi there, I'm trying to do something, but I don't know if it's possible. Basically, I want to have a public static class method that could access a private object's method. I would like to be able...
11
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
5
by: Stodge | last post by:
I've exposed a C++ class to Python using Boost Python. The class, let's say it's called Entity, contains private static data, which is an array of strings. Though I think it implements it using...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.