3

first here is the example Code:

cPerson.h:

#pragma once
class cPerson
{
public:
    cPerson();
    ~cPerson();
    int Age;
};

cPerson.cpp

#include "cPerson.h"

cPerson::cPerson()
{
    this->Age = 3; // Way 1
    cPerson::Age = 4; // Way 2
}

cPerson::~cPerson() { }

Ok now my Question:

If we are defining a new Class in C++ there are two ways to set the initial Values. There is (Way 1) by using the "this"-pointer, or (Way 2) using the scope operator ( :: ). In school I learned it using "this->". Now, years after not using C++, I startet to get into it again and found this second way, using the scope operator. Both way work fine BUT what's the exact difference between them and what's the "faster"/"better" way? I'm that kind of guy who likes to know what exactly is going on in my ram/cpu if I'm programming.

So I hope someone can help me out, and thanks in advance.

Phillip
  • 789
  • 4
  • 22
  • You could simply write `Age = 3;` or better use a member initializer list as mentioned by Bathsheba. Most of the time, you should not qualify a member of your class, so you should be using `Age` directly instead of `this->Age` or `cPerson::Age`. – Holt Jan 10 '18 at 10:29
  • In your particular example, there is no difference. Typically, the compiler will handle them both the same way. There are other ways too. – Peter Jan 10 '18 at 10:47
  • Prefixing a class name with `c` isn't the best idea – Passer By Jan 10 '18 at 11:09
  • @PasserBy: Why? – Phillip Jan 10 '18 at 11:39
  • It is redundant information, and gets in the way. The reader will be able to see that a name is a type when they see the surrounding context. You are _not_ going to mistake `Person jonskeet;` as something else – Passer By Jan 10 '18 at 11:52
  • @Passer By: oh ok yea thats not my usual style neither. But I was hoping for an more interresting answet to this question :D I don't know what I expected. – Phillip Jan 10 '18 at 12:00
  • @Peter: your answer was the most interresting one thanks :) – Phillip Jan 10 '18 at 12:00

1 Answers1

3

A better way to write the constructor as

cPerson::cPerson() : Age(3)
{
}

since then you can construct a const instance of your object. Consider starting Age with a lower case letter: this would be more conventional.

You could refine your first way by writing the more succinct Age = 3;: sometimes initialising members in the constructor body is unavoidable if they depend on the result of complex calculations.

Using :: is idiosyncratic: using the scope resolution operator will fail if the member is defined in a base class. But it does have occasional usage, particularly if you need to disambiguate a shadowed base class member.

Finally, from C++11 onwards you could simplify your class to

struct cPerson
{
    int Age = 3;
};

See C++11 allows in-class initialization of non-static and non-const members. What changed?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Nice tanks for the answer. I was expecting C++11 was involved in some way. I just couldn't find any information. Your link was a good information source, tanks. And yea simplifying would be a good thing in this case. Althoug it's just ment to be an example. – Phillip Jan 10 '18 at 12:04