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.

1
  • 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++? Commented Nov 23, 2009 at 6:29

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

You shouldn't be naming things with a leading underscore
-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."
Data members are not in the global namespace.
Data members could hide members from global namespace. That is could be unwanted behavior.
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.
|
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) { }

2 Comments

You can do this with the ctor initializer.
Thanks, I just tested and updated my answer, though I also upvoted yours.
3

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

class A {
  int x;
  A( int x ) : x(x) {};
};

Comments

1

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

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

private:
  int x_;

Much prettier.

3 Comments

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: 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 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.)
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.