0

Consider the following code:

Class A
{
   int a;
   somepointer* b;
}
Class B : A
{
   void func()
}
main()
{
   A* a1 = new A();
   a1->a = 5;
   a1->b = new somepointer();
   B* b1 = new B()
}

Now when B is created it contains A, I want to reuse the a1 created earlier, that is b1->a should give the value 5.How can this be done?

1
  • You have a number of syntax problems.... Commented Oct 29, 2013 at 2:09

2 Answers 2

1

Every instance has its own variables. You can't access a1's members from b1.

Your code have many problems, and the general approach of using pointers like you do does not fit C++ (it looks more like Java).

I suggest you get a good book about C++ and start over. Learning C++ by trial and error is not the best approach.

Sign up to request clarification or add additional context in comments.

7 Comments

That was just a prototype to explain my problem
So there is noway i can reuse a1 in b1??cant i assign the pointer of a1 so that the b1 has same values as in a1's variables?
B* b1 = static_cast<B*>(a1); Since b1 is pointing to a1 now can i also access b1's variable?
@HumayaraNazneen You can do B* b1 = dynamic_cast<B*>(a1);. This will make b1 point to the same object as a1. But for this to work the class A must be polymorphic (it must have at least one virtual function).
@HumayaraNazneen If A were polymorphic and you would perform a dynamic_cast and point b1 to same object as a1, then you could access a1's members via dereferencing (->) from b1.
|
1

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.

5 Comments

Here since the b is pointer to something it could also be class.Just copying the values like b=my_a->, gives me only shallow copy ryt? I mean if i change the value of b in class B, that will also change the value of b in my_a ryt?
Pointer assignment does not implicitly copy the underlying object being pointed to. In other words, b = my_a->b in this answer merely specifies that new B object being created will directly use the value of b from the previously created A object. If you want a copy, you will need to manually copy it (i.e. somepointer* copy_b = new somepointer() ...).
Thats exactly what i want.. If i change b here i want the b present in previously created A also to change. With this does that work?
Yes, it should do what you want. Keep in mind that as soon as the previously created A object is deallocated (i.e. delete call), attempting to use any members of A from your new B object will be invalid.
Deleting B wil delete previously created A's object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.