How to assign content to the superclass ?

I have a struct struct_A, basically it has some vectors and props, no functions.

I have the classB, that has an internal instance of struct_A, created on the heap.
I have tha classC. I want this class extends the struct_A. So I have :
1
2
3
4
5
6
7
8
9
10
11
12
13
 ClassC : public struct_A 
  { public :
   ClassC();
  }


  ClassC::ClassC () : struct_A() {
  classB classb_instance;
 //   Here is my problem I dont know how to get the instance_of_struct_A and 
 // asociate it to my struct_A 
   this->struc_A = classb_instance.instance_of_structA ; ?????

  }


Any idea ? Thanks



Last edited on
You can't assign a child object to a parent object, you can only assign a child* to a parent* (where * means a pointer).
¿ what about this->struct_A::operator=( classb_instance.instance_of_structA ); ?

your constructor is weird. ¿care to share more context?
Last edited on
Oh. I get what you wanted now.. Sorry.
You could try
1
2
structA* ptr = this;
*ptr = ...


Though this is probably something you should only do in the initializer list. If you can't, it could be that your program is poorly structured.
Last edited on
By now I only have been able to do a deep copy of every member.
my_classA.data??? = classb_instance.instanca_of_structA.data????:

ne555.... this is what I'd have to write for operator = isnt't ?
The Hamsterman proposal ... I can't compile nothing like that.... (or I dont know how to )

But, ok, by now a deep copy can be a solution. But the question is ' Is not possible to copy by pointer ? Some kind of casting ?

Thanks





I probably didn't explain myself clearly. Try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

struct A{
   int something;
};

struct B : A{
   void setA(A a){
      A* ptr = this;
      *ptr = a;
   }
};

int main(){
   A a;
   a.something = 7;
   B b;
   b.something = 2;
   b.setA(a);
   std::cout << b.something;
}


Unless I'm misunderstanding you and overwriting the inherited part is not what you want..
I think he's trying to extract the 'A' portion of a 'B' object, B being a subclass of A.
Topic archived. No new replies allowed.