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