3

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.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • 1
    The question makes no sense, since it is based on an incorrect premise: you assert that you *can't* do it in C++. Well, you *can*. Exactly as you do it in Java. So, there's no question. Where did you get that strange idea that you can't do it in C++? – AnT stands with Russia Nov 23 '09 at 06:29

6 Answers6

5

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.

  • 3
    You shouldn't be naming things with a leading underscore – lyricat Nov 23 '09 at 03:26
  • Evan: Why? It's not a reserved name here. –  Nov 23 '09 at 03:28
  • Something about the standard saying that things that start with _ should be interpreted in some special way... – i_am_jorf Nov 23 '09 at 03:40
  • 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 –  Nov 23 '09 at 03:57
  • 3
    -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 Nov 23 '09 at 05:54
  • 4
    Data members are not in the global namespace. –  Nov 23 '09 at 06:16
  • 2
    Data members could hide members from global namespace. That is could be unwanted behavior. – Kirill V. Lyadvinsky Nov 23 '09 at 06:30
  • 2
    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 Nov 23 '09 at 06:32
  • 1
    @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. Nov 23 '09 at 07:49
  • 1
    Kirill: You are confused. Data members do not interfere with local variables in completely unrelated functions. –  Nov 23 '09 at 18:19
  • 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. –  Nov 23 '09 at 18:26
3

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
  • 186,300
  • 67
  • 213
  • 256
3

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

class A {
  int x;
  A( int x ) : x(x) {};
};
lyricat
  • 1,988
  • 2
  • 12
  • 20
1

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

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

private:
  int x_;

Much prettier.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 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. –  Nov 23 '09 at 03:33
  • @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. – i_am_jorf Nov 23 '09 at 03:39
  • 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.) –  Nov 23 '09 at 03:49
1

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
  • 28,915
  • 15
  • 75
  • 111
0

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
  • 344,730
  • 71
  • 640
  • 635