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

2

5

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

Flirt answered 14/12, 2023 at 10:55 Comment(5)
Same thing. See en.wikipedia.org/wiki/Field_(computer_science)Disjuncture
A "field" is an informal name for a "non-static data member" (there are also static data members, those are not fields). Don't omit "non-static" to avoid confusion.Shoulders
On the suitability of the question: The "terminology" tag exists, so questions of terminology must be ok.Costive
BTW in C++ 'data elements' within structs and classes also can be declared as base class instead of as non-static data member (but the effect is very similar). C++ even supports multiple inheritance and different access specifiers for base classes, both of which most languages do not. Nevertheless, probably one would not call the base classes as fields.Norenenorfleet
Depending on frameworks and libraries used for programming, there also could exist a separately programmed higher level abstraction in C++, where specific classes represent higher-level objects with added functionality, e.g. user access permissions, data validation, storing of the data to disk or transfer over network. Those higher-level objects could store the data in a dynamic runtime-defined way, and therefore call the data storage as 'fields', with added functionality compared to C++ non-static data members. So there could exist a class called 'Field' with other classes deriving from it.Norenenorfleet
P
7

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.

Piece answered 14/12, 2023 at 11:0 Comment(1)
To add to this answer, if field was a thing, you'd find it in the index.Misgive
W
1

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.

Welford answered 14/12, 2023 at 17:41 Comment(2)
There are more differences regarding member functions. Even non-static ones do not occupy memory for each object instance, because the function is fixed for the class type. But e.g. a non-static member variable of type function pointer inside the class would occupy memory, as it can be reassigned. On the other hand the syntax for accessing member variables and member functions is the same and you can have a pointer to member to member variables and member functions.Norenenorfleet
@Norenenorfleet well, a fuction isn't an object. But if you declare a function type using func = void(int), you can declare a member function of class T visually looking like a variable class T { func foo; };Welford

© 2022 - 2024 — McMap. All rights reserved.