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++.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a