What is the difference between C++ "data member" and "field"?

356 Views Asked by At

I am translator in company and often we have problem to understand the difference between the terms "data member" and "field".

Could you please describe what they are and how they differ.

For example, "It can also denote the access to the data members of the object."

2

There are 2 best solutions below

1
On

A data member in C++ is a non-function member of a class (class, struct, or union).

The term field is not a thing in C++. Languages like Java or Kotlin refer to members of classes as field, and it's the same concept. Field may also refer to a field in a form, e.g. a text input on a website. Similarly, there are member functions in C++ and methods in Java. Both terms refer to the same concept, but only member function appears in the C++ standard.

class C {
    // In C++, this is a "data member".
    // A Java developer likely knows it as "field".
    int x;
};

Developers coming from other languages are used to different terminology. In the end, it's multiple terms for the same thing.

2
On

It's actually a triad - field, data member and member variable.

First comes from database record, field and table hierarchy. Today it is either used in that context or in context of any language which operates with record-like data structures. Data member is inter-changeable with the latter.

C++ uses term "member variable". It's both a wider and more specific term. A member variable is a non-function member of class. On some level a difference between member function and member variable is only in type because in C++ a function is a type.

In C++ a member variable is a named object in context of concrete class. It can be shared between all instances of class when it was declared as static. Member variable belongs to scope of particular class and is not a member variable of derived class, although it can be accessed (usually) through derived classes.

Non-static member variable's storage (memory it occupies) is part of object's storage, i.e it is a sub-object. An object of base class is also a sub-object of instance of derived class, therefore a member variable of base class is naming a subobject of derived class's instance.

"Field" in C++ context is a common malopropism denoting a non-static member variable of class or of any of its base classes, i.e. a sub-object.