I asked a question today that was closed as duplicate by someone who has a dup-hammer (I think), but I believe it's quite different from the linked questions.
In the question body, I asked if there is a way around the error I encountered, so overall, the post is not just about the 'why' but also about a workaround for the problem.
After some thinking on my own, I actually figured out an easy solution using an anonymous struct. That allows me to utilize aggregate initialization and not have to resort to manual assignments. Here's the solution code:
#include <iostream>
struct Point_1 {
int x;
int y;
void foo() {
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
}
};
struct Point_2 {
// anonymous struct
struct {
int x;
int y;
} attr;
virtual void foo() {}
};
struct Point_3 : Point_2 {
void foo() override {
std::cout << "x: " << attr.x << std::endl; // attr.x instead of x
std::cout << "y: " << attr.y << std::endl; // attr.y instead of y
}
};
int main() {
Point_1 A = { 100, 100 };
A.foo();
Point_3 B;
B.attr = { 200, 200 }; // aggregate initialization
B.foo();
return 0;
}
This could help someone encountering the same problem so do you think my question is re-openable?