0

C# has a tool called StyleCop written by Microsoft which gives you some default rules that I find very useful to end any bickering among teams about code style. The default rules state that all class variables must use the following syntax to refer to class variables:

this.classVariable  // C# Default StyleCop Naming Convention

The main alternative in C# is to use a leading underscore instead:

_classVariable      // C# Alternative StyleCop Naming Convention

C++ seems to have a large number of naming restrictions which rule out the leading underscore as this is reserved for libraries and operating systems. What about using the 'this' keyword like this, is this a common syntax?

this->classVariable // C++

I don't want this post to descend into a fight about style. I am not asking your opinion on coding style. I just want to know a short concise list of what the most common styles are and if the above example is used commonly, so I can pick one and move on.

UPDATE

I'm not sure why this question was put on hold. Specific questions were asked and we got one very good answer. No opinion was expressed anywhere. Admittedly, this is a touchy subject but I think all went well in the end.

Community
  • 1
  • 1
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • `this` is generally not used unless for clarity, anyway this question is off-topic as it will elicit opinions. – EdChum Jul 30 '14 at 09:26
  • There are many contradicting conventions on C++ naming style. – Neil Kirk Jul 30 '14 at 10:01
  • I am not asking for opinions as stated in my question. I am asking a specific question about a C# coding style and if it can be translated to C++. I am also asking for a short list of styles and **no opinions**. – Muhammad Rehan Saeed Jul 30 '14 at 10:21
  • The answer is to get a copy of the Framework Design Guidelines. It holds the "correct" way to "style" your code for C# developers. A survey of users here is not as authoritative or as thorough as this document. –  Jul 30 '14 at 14:50

1 Answers1

1

There are 3 styles, as far as I know.

First, use underscore:

class Person
{
private:
    int _age;
    ...

You're worried about naming restrictions, but don't worry - first underscore + non-uppercase on non-global names is not illegal.

(first underscore + uppercase is illegal; you must not use names like this: _Age.)

Second, use m_. (m means member.)

class Person
{
private:
    int m_age;
    ...

This style is used in MFC, and I also prefer this.

Third, use underscore, on the last part of name.

class Person
{
private:
    int age_;
    ...

It isn't seen many times, but I've seen it before >o<


Well, you can also name your all class variable normally and use this->.

class Person
{
private:
    int age;
    ...
public:
    void SetAge(int age) { this->age = age; }
    ...

However, this isn't used widely; above all, it's so inconvenient. (Also I have hardly seen this.var style in C# except tool-generated source - is it really widely-used in C#??)

As you see, this-> syntax is used to avoid such a ambiguous situation:

void Person::SetAge(int age)
{
    age = age; // what does it mean?
}

(In addition, in the situation like this, age means the parameter; because it's more local than Person::age, so the local age hides Person::age.)

But it isn't used widely as I said. In this situation, It's much better to edit the name of one of ages, than to use this->.

this pointer just means myself; like any other languages which has class, including C#, It is used like this:

void SomeUtilFunction(Person *pPerson);

void Person::Foo()
{
    ...
    SomeUtilFunction(this); // refer to myself
    ...
}
ikh
  • 10,119
  • 1
  • 31
  • 70
  • Excellent, thank you. exactly what I asked for. Have an up-vote. I'd still be interested to see if anyone has any info about the use of the 'this->' syntax. – Muhammad Rehan Saeed Jul 30 '14 at 11:13
  • 1
    @RehanSaeed please see edit >o – ikh Jul 30 '14 at 11:26
  • Thanks. I have seen the 'this' keyword more often of late. Probably due to take up of the StyleCop tool which uses it as the default style. I switched from underscores to 'this' in C#. There are advantages and disadvantages to both, just a matter of preference. Coming from C# 'this->' just looked easier on my eye, so that is the only reason I asked. I think I'll stick with underscores in C++. – Muhammad Rehan Saeed Jul 30 '14 at 11:43