What exactly is a "member" of a class in C++?

829 views Asked by At

I am new to C++, and have experience in Java and Python. I tried to search this question on Stack for a while, but did not find any questions that resembled this (although maybe that is because of my cursory knowledge of C++).

I was reading the C++ Primer book until I stumbled upon "members" of classes in C++. I can understand the concept of a class from Java, but I am unsure of what a "member" is.

Is a member simply an instance of a class? If so, how come it seems like the variables in a class are also considered members (in the Primer, the ISBN number of a class for a book is considered a member)?

Could anyone give a general definition of a "member" in C++?

4

There are 4 answers

0
David Haim On BEST ANSWER

A member is some entity that belongs to a class.

If a class has a function, this is a member function - you might know it as "a method".
If a class has a variable, this is a member variable - you might know it as "a property".

int a;
void f () {};

class A{
 int m_A;
  void m_F(){}
}

a is a global variable.
f is a global function.
m_A is a member variable or "property" of the class A.
m_F is a member function or "method" of the class A.

3
therainmaker On

A member is defined as the variables and functions within a class.

Variables defined within a class are sometimes referred to as member variables. Similarly functions can be called member functions. Besides this, there isn't much to it.

0
LBes On

I guess a google search would have worked cause I just searched for 5 seconds:

Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.

You can check a more complete definition here: http://www.cplusplus.com/doc/tutorial/classes/

In a nutshell both the data members (i.e variable of the class) and the functions are members of a class.

As far as I remember, java members are exactly the same.

0
Vlad from Moscow On

According to the C++ Standard (9.2 Class members)

1 The member-specification in a class definition declares the full set of members of the class; no member can be added elsewhere. Members of a class are data members, member functions (9.3), nested types, and enumerators. Data members and member functions are static or non-static; see 9.4. Nested types are classes (9.1, 9.7) and enumerations (7.2) defined in the class, and arbitrary types declared as members by use of a typedef declaration (7.1.3). The enumerators of an unscoped enumeration (7.2) defined in the class are members of the class. Except when used to declare friends (11.3) or to introduce the name of a member of a base class into a derived class (7.3.3), member-declarations declare members of the class, and each such member-declaration shall declare at least one member name of the class. A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined, and except that an enumeration can be introduced with an opaque-enum-declaration and later redeclared with an enum-specifier.

Also class members are

using-declaration
static_assert-declaration
template-declaration
alias-declaration