I guess you are providing just prototype of what you want. So I will ignore about the syntax problems.
You can create a constructor in class B that take a variable of type A as parameter:
class B:A {
public:
B(A *my_a) {
a = my_a->a;
b = my_a->b;
}
void func() {
//......
}
}
main() {
A *a1 = new A();
a1->a = 5;
a1->b = new somepointer();
b = new B(a);
}
You may think about dynamic casting as well: C++ cast to derived class . But unfortunately that doesn't work, so best way here is to have a constructor so that you can manually set the variables you need for reusing.
Edit: Since Humayara Nazneen wants to have effects that changing a1->b will not affect b->b, then A::b shouldn't be declared as pointer, which means that:
class A {
int a;
somepointer b;
}
When you assign b = my_a->b; it will be copied by value.