-7

This question asserts that this is valid C++:

class MyClass {
    public:
        MyClass(int n);
    private:
        AnotherClass another(100); // this constructs AnotherClass right away!
};

I'm not a C++ expert, but I don't believe this is true. My compiler demands I use the syntax AnotherClass another = AnotherClass(100);. Another user in the comments under the question had the same problem.

This is a popular question (the first result on Google for "c++ object field constructor") and I lost a good few minutes trying to figure out why I couldn't get this working. Is this information indeed wrong? If so, maybe it should be edited to avoid creating confusion.

4
  • 1
    I'm a bit confused. Are you concerned that code or information in a question is wrong? That's pretty much how it works on SO.
    – cigien
    Dec 19, 2020 at 16:45
  • 1
    @cigien The code isn't presented as "Guys help, this code doesn't work" - the snippet is presented as just an example of correct code before the asker actually poses the question. There are no comments underneath or even anything in the answers to contradict the wrong (?) information, which you would naturally expect for a +83 question. I'm not saying people coming from Google have zero responsibility to be skeptical of code in questions, but for highly popular, 8 year old questions a bit of curation may make the internet a net better place.
    – Jack M
    Dec 19, 2020 at 17:06
  • 1
    @JeanneDark Well, sort of - although the accepted answer there sort of contradicts the response I'm getting to this question (since it basically says "sure, sometimes editing a question is good"). So should I just go edit the question myself?
    – Jack M
    Dec 19, 2020 at 17:08

1 Answer 1

1

Questions often contain wrong information. Askers are typically the least likely to get details right, since they are almost by definition the least knowledgeable about the topic. When researching solutions, you should not blindly trust code that appears in questions.

It is generally not appropriate to suggest edits that fix mistakes in code that appears in questions, since wrong code and wrong assumptions are essentially the whole point of a question. Furthermore, one or more of the answers may have discussed that wrong code/assumption, so modifying it in the question will invalidate those answers, thus confusing everyone even more.

When researching a problem, I recommend reading and trying what is recommended in the answers, not what is recommended in the question.

For what it's worth, that code is definitely wrong. I don't know where they got the idea that would work—I hope it wasn't presented as a solution on Stack Overflow! It wouldn't be allowed by the grammar; it would be interpreted as a function declaration, not a constructor call for the purposes of initialization. You could make it compile by moving to the uniform initialization syntax introduced with C++11; e.g.:

class MyClass {
    public:
        MyClass(int n);
    private:
        AnotherClass another{100}; // construct AnotherClass right away!
};

Note that the parentheses surrounding the constructor arguments have been replaced with braces, which prevents this from being parsed as a function declaration.

But editing wrong code in questions is not going to reduce any confusion. In particular, if I were to change it to that now, it could be taken by future viewers to imply that the asker is targeting C++11 or later, which could change the entire nature and scope of the question.

I've tried to carefully edit the explanation preceding that code block in order to clarify that it may not be working code, without changing the meaning of the question or potentially invalidating any of the answers. Luckily, that leading contextual example was not material to the question proper.

You must log in to answer this question.

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