-20

The recent question

C++ Classes should I use this-> in the .cpp file

was closed as opinion-based -- but both alternatives OP provided are objectively wrong, given the code provided.

The (clearly stylistic and opinion-based) question of whether to use this-> or not is irrelevant, because the example given is a constructor.

Nonstatic data member variables in constructors, especially the kind used in the example, should be initialized with member initializers

A a(): i(1)
{
   // initializers do everything
}

or better yet, a default member initializer in the class definition:

class A
{
   int i = 1;
public:
   A() noexcept = default;
};

OP didn't provide a more complicated data member that might require initialization in the constructor body, so the more general case is beside the point.

It wouldn't surprise me if someone found a duplicate of this question, but it's not opinion-based. The answer to this question (as asked in the title) with this code is no.

6
  • 11
    "was closed as opinion-based -- but both alternatives OP provided are objectively wrong, given the c++ code provided." - so? It doesn't matter the way it's phrased. It asks for a comparison of "which is better". If a third, fourth, or Nth option is introduced, it's still opinion based, because it asks for preference. Jan 9 at 20:35
  • 1
    The same question could have been asked with a regular member function as the example instead of a constructor. So it doesn’t even matter that there’s a third option. Jan 9 at 21:46
  • 5
    To this, or not to this? That is the question—Whether 'tis nobler in the mind to suffer The slings and arrows of in-body assignment, Or to take arms against a sea of troubles, And, with an intitializer list, end them? Jan 10 at 0:20
  • 3
    A Tale of two Styles ) Jan 10 at 1:48
  • 2
    @user4581301 Off with your overhead.
    – Gimby
    Jan 10 at 10:58
  • There doesn't seem to be any clear effort to make it a constructive subjective question. Though it's really hard to blame them, since someone who doesn't know the language very well probably doesn't know enough to ask about specific pros and cons. Maybe an SME could ask it better or edit it to improve it.
    – user
    Jan 11 at 1:43

1 Answer 1

3

From two options: OP knew exactly what they wanted to ask and worked to have simplest possible example vs. OP did not even know what they are asking about and randomly selected title, I'd pick the first interpretation. As a result, to me this is an opinion-based question with a carefully crafted MRE (for "should I use this ->"), but unfortunately the MRE happen to also demonstrate the unrelated question "should I unconditionally use field initialization syntax?".

Note that there is nothing wrong with some opinion-based close cases—sometimes (like "use this-> or not in member methods") they just happen to be equally valid without any glaring drawbacks, thus ending up a style choice. It is hard for a novice in a given language/tech to know that in advance (possibly worth a downvote for lack of research in some cases, but this one at least shows a sensible MRE).

If one feels strongly, there are two options to improve the question:

  • update the MRE to use a method instead of constructor
  • rewrite the post to ask about field initialization
0

You must log in to answer this question.

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