470,835 Members | 1,811 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,835 developers. It's quick & easy.

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 1388

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Thomas Philips | last post: by
8 posts views Thread by Marco | last post: by
2 posts views Thread by MR | last post: by
5 posts views Thread by Charles Krug | last post: by
9 posts views Thread by Ulrich Hobelmann | last post: by
7 posts views Thread by Daniel Nogradi | last post: by
5 posts views Thread by ryanoasis | last post: by
10 posts views Thread by Karlo Lozovina | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.