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 c++ 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.
this
, or not tothis
? 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?