-9

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?

5
  • 5
    Doesn't the second duplicate link (Brace Initialize struct with virtual functions) answer your question about a workaround? One answer states that you can't use brace initialization in your case. The second answer states that you can use a constructor with parameters instead.
    – BDL
    Commented Dec 21, 2023 at 22:54
  • @BDL yeah but that is different from aggregate initialization and it's not convenient for the actual code am working with which is different from the example code i provided.
    – machine_1
    Commented Dec 21, 2023 at 22:55
  • 9
    That's pretty much the definition of a workaround though isn't it? I.e. something similar that achieves the same result. Commented Dec 21, 2023 at 23:00
  • 2
    Why & a workaround are 2 different questions. The 1st one is about your wrong notions or reasoning. Ask 1 question. For a workaround code for working parts is relevant, but bad code is not. And observe that the closer linked it to 2 different duplicates, one for each of yours. But it could have been closed as "lacks focus" or "unclear" (which are you asking? And when you decide, get rid of the other, irrelevant, question. Having 1 ultimate goal doesn't make 2 questions into 1 question.
    – philipxy
    Commented Dec 22, 2023 at 6:50
  • 1
    “but that is different from aggregate initialization and it's not convenient for the actual code am working with which is different from the example code i provided.” - So did you edit your question, and provided a logical explanation that the workaround in the duplicate, doesn’t answer your question. Just saying “the answer to the duplicate doesn’t answer my question”, isn’t enough of a reason not to close your question as a duplicate. Users with more experience are likely to disagree with your conclusion that the duplicate does not answer your question. We are not a help center. Commented Dec 22, 2023 at 12:34

1 Answer 1

16

Close voter here, thought I would explain my vote. You asked why virtual stops aggregate initialization. I dupe closed to C++ aggregates have no virtual functions? which explains why your class with a virtual function is no longer an aggregate, which means no aggregate initialization.

You ask for a workaround, and I closed it as a dupe of Brace Initialize struct with virtual functions which has a clear workaround for the problem, give the class a constructor.

Do note that your workaround isn't really a workaround.

B.attr = { 200, 200 }; // aggregate initialization

is not aggregate initialization, it is assignment of the attr member. attr was initialized in the line prior with Point_3 B; where the default constructor is used.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .