473,386 Members | 1,743 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.

How to hide subclasses?

There is a baseclass A. B is its subclass.
I want to hide class B. All operations to B are all by means of virtual
functions in A.
Class users can only see class A. B is transparent to them.

How can I do this?

Apr 7 '06 #1
6 1495

Let's see if I follow correctly. . .
There is a baseclass A.
class A {};

B is its subclass.

class B : public A {};

I want to hide class B. All operations to B are all by means of virtual
functions in A.
Class users can only see class A. B is transparent to them.


You've lost me.
-Tomás
Apr 7 '06 #2
Guch Wu wrote:
There is a baseclass A. B is its subclass.
I want to hide class B. All operations to B are all by means of virtual
functions in A.
Class users can only see class A. B is transparent to them.


It's much easier to see what you are talking about when you say it in code.

Are you talking about the PIMPL idiom?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 7 '06 #3
Ben Pope wrote:
Guch Wu wrote:
There is a baseclass A. B is its subclass.
I want to hide class B. All operations to B are all by means of
virtual functions in A.
Class users can only see class A. B is transparent to them.


It's much easier to see what you are talking about when you say it in
code.
Are you talking about the PIMPL idiom?


I am surprised to read your and Tomas' replies. Isn't it a normal course
of business of plug-ins, for example? They usually conform to some kind
of interface (base class) and then the module that contains the plug-in
provides a factory function that returns a pointer to the base class:

----------------------- this is the code in user's program
class A { public: virtual ~A() {} }; // probably from a header

A* getA(); // provided by Guch Wu's code (usually from a header)

int main() {
A *pa = getA();
// use pa somehow, invoking its virtual functions
delete pa;
}
------------------------- in another module, like a dynamic library
// include relevant headers here
class B : public A {};
A* getA() {
return new B;
}
------------------------------

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 7 '06 #4
Victor Bazarov wrote:
Ben Pope wrote:
Guch Wu wrote:
There is a baseclass A. B is its subclass.
I want to hide class B. All operations to B are all by means of
virtual functions in A.
Class users can only see class A. B is transparent to them.

It's much easier to see what you are talking about when you say it in
code.
Are you talking about the PIMPL idiom?


I am surprised to read your and Tomas' replies. Isn't it a normal course
of business of plug-ins, for example? They usually conform to some kind
of interface (base class) and then the module that contains the plug-in
provides a factory function that returns a pointer to the base class:


I think that is an alternative definition/implementation of "hide".

It's difficult to know whether he requires the implementation of class A
to be hidden, or the entire class B to be hidden, and whether the client
is the programmer or the code interacting with class A.

In retrospect, I think you have picked up on his requirements better than I.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 7 '06 #5

Guch Wu wrote in message
<11*********************@i39g2000cwa.googlegroups. com>...
There is a baseclass A. B is its subclass.
I want to hide class B. All operations to B are all by means of virtual
functions in A.
Class users can only see class A. B is transparent to them.
How can I do this?


Sounds like you want an “Cheshire cat” (or 'handle class'). Note, in the
example below, how the 'car' struct is hidden from public view (it's in the
object or library file). All the user will see is the pointer in the 'Car'
class in 'auto.h'. [ this is a crude example]

// --- auto.h ---
// put include guards here

class Car{
public:
void initialize(unsigned);
void cleanup();
private:
struct car;
car *theCar; // this is all the user sees
};

// --- auto.cpp ---
struct engine{
double weight;
double cubic_capacity;
};
struct transmission{
double weight;
unsigned type;
};

struct Car::car{
engine eng;
transmission trans;
unsigned serial_no;
};

void Car::initialize(unsigned Serial){
theCar = new car;
theCar->serial_no = Serial;
theCar->eng.weight = 0;
theCar->eng.cubic_capacity = 0;
theCar->trans.weight = 0;
theCar->trans.type = 0;
}

void Car::cleanup(){ delete theCar; }

// --- main ---
#include "auto.h"

int main() {
Car myCar;
myCar.initialize(4032005);
// myCar.read();
// myCar.change(1); // etc.
myCar.cleanup();
}

Did that help?
--
Bob R
POVrookie
Apr 7 '06 #6
English is not my mother tongue. I'm sorry to have confused you.
I think Victor Bazarov has well understood what I want to express.

Apr 8 '06 #7

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

Similar topics

5
by: Thomas Philips | last post by:
I'm teaching myself programming using Python, and have a question about subclasses. My game has two classes, Player and Alien, with identical functions, and I want to make Player a base class and...
0
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real...
8
by: Marco | last post by:
Hi all, I have a base class and some subclasses; I need to define an array of objects from these various subclasses. What I have is something like: { //I have a base class, something like:...
2
by: MR | last post by:
I'm hoping that someone here will be able to direct me to some litrature on how to get a list of a classes subclasses. I.e. need the inherited class to know about subclasses that are inherited...
5
by: Charles Krug | last post by:
List: I have this: # classC.py class C(object): pass class D(C): pass
9
by: Ulrich Hobelmann | last post by:
Hi, slowly transitioning from C to C++, I decided to remodel a struct/union (i.e. type identifier as first field, union of variant types) as a class + subclasses. Switching functions are replaced...
7
by: Daniel Nogradi | last post by:
What is the simplest way to instantiate all classes that are subclasses of a given class in a module? More precisely I have a module m with some content: # m.py class A: pass class x( A ):...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
10
by: Karlo Lozovina | last post by:
Hi, what's the best way to keep track of user-made subclasses, and instances of those subclasses? I just need a pointer in a right direction... thanks. -- Karlo Lozovina -- Mosor
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: 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: 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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.