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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes