0

In my code I have two almost identical structs, which I have here simplified:

struct foo {
    struct bar {
        int b;
    } a;
};

struct foo2 {
    struct qux {
        int b;
        int c;
    } a;
};

(The only difference here is that qux has a member c while bar does not.)

My question is, Is there a way to let qux inherit from bar, without having to create two foo? This can easily be done with classes, but I want to know if it can be achieved with structs. In my imagination it would look something like this:

struct bar {
    int b;
};

struct qux : bar {
    int c;
};

struct foo {
    bar_or_qux a;
};
Mossmyr
  • 909
  • 2
  • 10
  • 26
  • 1
    Suppose you somehow managed to achieve what you want. How do you plan to use this `foo`? When you then write `foo f;`, should it mean foo-with-c or foo-without-c? – Igor Tandetnik Nov 06 '19 at 02:20
  • 3
    Luckily, `struct`s in C++ support all the features that `class`es do, so whatever `class` solution you're thinking of will work equally well for a `struct`. – Stephen Newell Nov 06 '19 at 02:23
  • 2
    Do not use inheritance as a means to reduce code writing. Only if it makes logical sense to inherit one class from another should you do this. If a `bar` and a `qux` are totally different in terms of their purpose in your program, then inheritance shouldn't be used. – PaulMcKenzie Nov 06 '19 at 02:23
  • I don't understand the question. What are you trying to achieve? Why is there a `foo` and `foo2`? Are you yet to discover polymorphism? `struct foo { std::unique_ptr a; };` Are you yet to discover templates? `template struct foo { T a; };`. – Indiana Kernick Nov 06 '19 at 04:11
  • @StephenNewell See my comment on Ricardo Silvia's answer. – Mossmyr Nov 06 '19 at 14:45

2 Answers2

1

Here's the snippet posted in a comment to Ricardo's answer.

struct bar {
    int b;
};

struct qux : bar {
    int c;
};

struct foo {
    bar a;
};

int main() {
    qux q;
    foo f;
    f.a = q;
    f.a.b = 7;
    // f.a.c = 3;
}

This sort of thing might work in Python or JavaScript but this is C++. Assigning a qux to a bar doesn't magically turn the bar into a qux. This f.a = q is called slicing. I'm not sure exactly what you want but I'm gonna make a few guesses and maybe I'll stumble upon it.


You could use templates. This would mean that you can have a "foo with a qux" type and a "foo with a bar" type.

struct bar {
    int b;
};

struct qux : bar {
    int c;
};

template <typename T>
struct foo {
    T a;
};

int main() {
    qux q;
    foo<qux> f;
    f.a = q;
    f.a.b = 7;
    f.a.c = 3;
}

You could use polymorphism. This would mean that you can have a single foo type that can store a subclass of bar (e.g. qux).

#include <memory>

struct bar {
    int b;

    virtual void setC(int) = 0;
};

struct qux : bar {
    int c;

    void setC(const int value) override {
        c = value;
    }
};

struct foo {
    std::unique_ptr<bar> a;
};

int main() {
    qux q;
    foo f;
    f.a = std::make_unique<qux>(q);
    f.a->b = 7;
    f.a->setC(3);
}

I hope this helps!

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
0

You can use inheritance with structs in the same way as classes.

Structs in C++ have all the same capabilities as classes, with the only differential being that their members (declarations) and bases (inheritance) are public by default, while for classes, they're private by default.

  • Turns out my assumption was incorrect, you can't do this with classes: https://pastebin.com/YYBRMZQL – Mossmyr Nov 06 '19 at 14:43
  • ah, for that you need to use either raw pointers, references, or smart pointers since you can't do polymorphism with simple values – Ricardo Silva Nov 07 '19 at 02:01