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;
};
5
  • 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? Commented Nov 6, 2019 at 2:20
  • 3
    Luckily, structs in C++ support all the features that classes do, so whatever class solution you're thinking of will work equally well for a struct. Commented Nov 6, 2019 at 2: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. Commented Nov 6, 2019 at 2: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<bar> a; }; Are you yet to discover templates? template <typename T> struct foo { T a; };. Commented Nov 6, 2019 at 4:11
  • @StephenNewell See my comment on Ricardo Silvia's answer. Commented Nov 6, 2019 at 14:45

2 Answers 2

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!

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

Comments

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.

2 Comments

Turns out my assumption was incorrect, you can't do this with classes: pastebin.com/YYBRMZQL
ah, for that you need to use either raw pointers, references, or smart pointers since you can't do polymorphism with simple values

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.