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?

yizzlez
  • 8,757
  • 4
  • 29
  • 44

2 Answers2

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.

Felix Glas
  • 15,065
  • 7
  • 53
  • 82
  • That was just a prototype to explain my problem –  Oct 29 '13 at 02:28
  • 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? –  Oct 29 '13 at 02:30
  • B* b1 = static_cast(a1); Since b1 is pointing to a1 now can i also access b1's variable? –  Oct 29 '13 at 02:36
  • @HumayaraNazneen You can do `B* b1 = dynamic_cast(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). – Felix Glas Oct 29 '13 at 02:39
  • @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`. – Felix Glas Oct 29 '13 at 02:43
  • But wil i also be able to acces B's members with b1 with dynamic cast? –  Oct 29 '13 at 02:46
  • @HumayaraNazneen Hmm, actually I was wrong about the `dynamic_cast`. In your case `a1` points to an `A` object so the `dynamic_cast` will fail. When you know the dynamic type of the object then using `static_cast` is ok. So you would have to do a `B* b1 = static_cast(a1)`. – Felix Glas Oct 29 '13 at 02:57
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.

Community
  • 1
  • 1
Krypton
  • 3,337
  • 5
  • 32
  • 52
  • 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? –  Oct 29 '13 at 02:28
  • 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()` ...). – miqh Oct 29 '13 at 02:37
  • 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? –  Oct 29 '13 at 02:42
  • 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. – miqh Oct 29 '13 at 02:47
  • Deleting B wil delete previously created A's object? –  Oct 29 '13 at 02:57