开发者

C++ casting to derived and parent structs

开发者 https://www.devze.com 2023-02-09 11:24 出处:网络
I was wondering how to do something in C++. I want to be able to create an instance of this struct struct ComplexInstruction : simple_instr

I was wondering how to do something in C++. I want to be able to create an instance of this struct

struct ComplexInstruction : simple_instr
{
    bool isHead;
    bool isTail;
};

that copies all the data from the simple_instr instance. So essentially, I want to do something like this

ComplexInstruction cInstr = instr; // <- instance of simple_instr

and have cInstr have a copy of all the data in instr without having to copy over every field (since there's alot of them). I'm not sure how do this, and I don't think simple casting will work. Additionally, is i开发者_StackOverflow社区t possible to do the reverse? I.e. have an instance of ComplexInstruction and turn it into an instance of simple_instr. I assume this can be done using casting, but I don;t have alot of experience with c++

Thanks in advance


Create a consctructor in the derived class to initialize from a base class.

class Base
{
  int x;
public:
  Base(int a) : x(a){}
};

class Derived : public Base
{
public:
  Derived(const Base & B) : Base(B){}
};

Note that if you have a derived object of Base, you actually have a base object and you can safely use the base copy ctor like so.

Derived d;
Base b(d);//the parts of Base that are in Derived are now copied from d to b.
          //Rest is ignored.  

If you want to be more verbose, you write an operator= in your derived class like

void operator=(const Base & b)
{
  Base::operator=(b);
  //don't forget to initialize the rest of the derived members after this, though.
}

It all depends on what you want to do, really. The important thing is: be explicit. Don't leave uninitialized members of your class.


You need to provide a constructor that takes an argument that is convertible to const simple_instr&:

struct simple_instr {
  int i;
  simple_instr(): i(0) { }
  explicit simple_instr(int i): i(i) { }
};

struct ComplexInstruction: simple_instr {
  explicit ComplexInstruction(const simple_instr& simple):
    simple_instr(simple), isHead(false), isTail(false) { }
  bool isHead;
  bool isTail;
};

int main() {
  simple_instr instr;
  ComplexInstruction cInstr(instr);
}

Here I chose an explicit constructor, but depending on the semantics, an implicit one could also be appropriate. Only if the constructor is implicit, the =-style initialization works without casting.


Edit: This is not the best way to accomplish this, please look at the other answers.


This will do what you are looking for.

struct ComplexInstruction : simple_instr
{
    ComplexInstruction(const simple_instr &simple)
    {
       *((simple_instr*)this) = simple;
    }

    bool isHead;
    bool isTail;
};

Then ComplexInstruciton complex = simple; will call the conversion constructor. ComplexInstruction's copy construct casts this to its base class and the = will call simple_instr's copy constructor, which by default is a bitwise copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号