devxlogo

Member and Argument Variables Having the Same Name

Member and Argument Variables Having the Same Name

Question:
I’ve done a lot of programming in Java, and am relatively new to C++. One of the things that annoys me (just personally), is using a prefix such as an underscore to mark instance variables, e.g.:

class A {  int _size;public:  Vector (int size) : _size (size) { }};

I find this hard on the eyesight and awkward to type, so in Java I write:

class A {  private int size;  public A (int size) { this.size = size; }}

How do I do this in C++? When I use g++, it seems to let me do all of the following:

class A {  int size;public:  A (int size);};1. A::A (int size) { this->size = size; }2. A::A (int size) { A::size = size; }3. A::A (int size) : size (size) { }

Are all these correct, portable? If so, which one is the cleanest?

As a sideline, are there any resources on the web that show Java programmers how to write good C++ code using the abstractions that they’re used to designing with (packages, interfaces etc). It would give me a more comfortable starting point before learning some of C++’s more exotic features.

Answer:
Prepending an underscore to a data member’s name is not a standard feature of C++ but merely a coding convention of a certain programming school. In this regard, it doesn’t differ from using a lowercase verb followed by a noun starting with an uppercase letter in Java, e.g., displayName(). If you’re used to the Java convention of:

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

You may find that the form:

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

is the closest equivalent in C++. However, I personally prefer to use a member initialization list whenever possible:

A::A (int size) : size (size) { }

to explicitly indicate that the member is initialized rather than being assigned a value. In certain conditions, this form is also more efficient than in-constructor assignment.

The following form:

A::A (int size) { A::size = size; }

is the least readable one because a qualified name might suggest that size is a static member or perhaps it’s a member of a base class. However, all three forms are valid and portable.

I’ve seen several books targeted at Java programmers who are making the transition to C++. However, my advice is simply to get a good, authoritative C++ book and read it as if you didn’t know Java. Although some Java features seemingly have C++ equivalents, e.g., namespaces vs. packages, abstract base classes vs. interfaces, C++ has many fundamental concepts and features that do not exist in Java (templates, STL, operator overloading, enum types, pointers, pointers to members) that you should familiarize yourself with right from the start. For example, skipping operator overloading and using named functions instead is bad design practice in C++.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist