tags:

views:

147

answers:

6

In Java, usually, I can have my constructor's parameters same name as member variables.

public A(int x)
{
    this.x = x;
}

private int x;

In C++, I can't. Usually, I have to do it this way.

public:
    A(int x_) : x(x_) 
    {
    }

private:
    int x;

Is there any better way? As the constructor parameters name look ugly, when IDE IntelliSense pop up the constructor parameters windows.

A: 

One way (and some may argue that the this is the C++ way) would be to prefix your class's fields with m_ to disambiguate between the field and the constructor argument name.

Andrew Hare
A: 

Google style is to make the members have the trailing underscore:

public:
  A(int x) : x_(x) {
  }

private:
  int x_;

Much prettier.

jeffamaphone
Google's needs are often very different from others', and their C++ style guide reflects this. If you're writing code for Google to consume, I'd recommend it; in all other cases, take a careful look at their reasoning first.
Roger Pate
@Roger: Good point, and by no means am I suggesting willy-nilly adoption of the entire Google style guide. However, I've found in general their style guide for C++ is reasonably all-purpose. The Microsoft style, in this case, would be int m_x, or int _x. In the scope of our discussion, everyone is going to be in agreement that member functions should be differentiated somehow. I merely point out one opinion that I happen to like.
jeffamaphone
I did think you understand my point, but I don't think it's obvious in your example, and I think others are likely to misunderstand and apply Google's choices inappropriately. (So that's why the downvote.)
Roger Pate
+2  A: 

You can actually do this the Java way in C++:

public:
    A(int x)
    {
        this->x = x;
    }

But then it's also possible to just say x(x):

public:
    A(int x) : x(x) { }
Eli Courtwright
You *can* do this with the ctor initializer.
Roger Pate
Thanks, I just tested and updated my answer, though I also upvoted yours.
Eli Courtwright
+1  A: 

I don't think this is possible. However, I would highly recommend using a consistent naming convention for member variables to differentiate them from parameters and local variables.

In my company we usually denote member variables with an 'm' prefix, e.g.:

int mMyMemberVariable;

or

int m_MyMemberVariable;

This is just an example of a style - consistency is the key.

LeopardSkinPillBoxHat
+5  A: 

In C++, you can, if you want:

struct A {
  int x;
  A(int x) : x(x) {
    foo(this->x);
    // if you want the member instead of the parameter here
  }
};

Though I also commonly use stylistic names for members (e.g. _x), I do it for non-public members. If x is public as in this example, I would do it like this, and look at renaming the ctor's parameter if I thought it would be more readable.

Edit: Since people seem to be getting sidetracked, I'll clarify on _x. The standard reserves some identifier names:

  • any name with two adjacent underscores, in any namespace
  • any name with a leading underscore followed by an uppercase letter, in any namespace
  • any name with a leading underscore at global scope

Since members are scoped to the class, they do not fall in the third category. That said, it would be nice to not continue getting sidetracked. :) Feel free to ask a question about reserved identifiers in C++ and post a link to it in the comments if you want.

Roger Pate
You shouldn't be naming things with a leading underscore
Evän Vrooksövich
Evan: Why? It's not a reserved name here.
Roger Pate
Something about the standard saying that things that start with _ should be interpreted in some special way...
jeffamaphone
jeff: Yes, there are reserved names. `_x` is not one of them here. I find a prefix much more readable and obvious than a suffix. (My personal second choice is m_ for the same reason; even though I've never quite understood what the 'm' stands for, it is popular and likely to be understood. :P
Roger Pate
-1. C++ Standard 17.4.3.1.2/1: "**Each name** that begins with an underscore is reserved to the implementation for use as a name in the global namespace."
Kirill V. Lyadvinsky
Data members are not in the global namespace.
Roger Pate
Data members could hide members from global namespace. That is could be unwanted behavior.
Kirill V. Lyadvinsky
For example, implementation could use `_x` in the `operator new`. That means you will get undefined value in `A::_x` each time that you call global `operator new` in class implementation.
Kirill V. Lyadvinsky
@Roger Pate: I always that the `m` in `m_` was for "member". There is also the same convention without the underscore (camelCase style), like `mX`.
Matthieu M.
Kirill: You are confused. Data members do not interfere with local variables in completely unrelated functions.
Roger Pate
Matthieu: Yes, but if we're using it to indicate members, why not use it on *all* members, even public ones, including functions? In reality it means something else, closer to 'non-public data member', and I'm sure varies slightly by user.
Roger Pate
+2  A: 

C++ is smart enough to figure out which x you mean, you can write:

class A {
  int x;
  A( int x ) : x(x) {};
};
Evän Vrooksövich