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

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

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.

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.

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

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.

Related

Is there a reason to differentiate between static and non-static data members in terms of prohibition of the usage of abstract types?

In class.abstract we can see in the Note 3 that:
An abstract class can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it ([basic.def], [class.mem]).
This rules out the usage of abstract classes as subobjects and it makes sense to me (though this is just a note, which is non-normative, IIRC).
However, in class.mem we can read that:
The type of a non-static data member shall not be an incomplete type ([basic.types]), an abstract class type ([class.abstract]), or a (possibly multi-dimensional) array thereof.
[Note 5: In particular, a class C cannot contain a non-static member of class C, but it can contain a pointer or reference to an object of class C. — end note]
(emphasis mine)
What seems strange to me is the specific wording: "non-static". Why is it explicitly stated that this refers to non-static members? I don't believe we're allowed to have static declarations or definitions of objects of abstract types. Does the standard actually allow static data members to be abstract and no sane compiler implements that? Or it does prohibit such uses (in that case why the distinction in static vs non-static data in the aforementioned paragraph)?
Whether or not a class is abstract is not known until the class is defined.
It has always been allowed to declare a static data member with an incomplete class type, as long as the type is complete by the time the static data member is defined. Since the type of a static data member may be incomplete on its declaration, it also follows that it might be an abstract class that the compiler doesn't yet know is abstract. For this reason, it is appropriate to defer checking of the abstractness until the static data member's definition. At the time of definition, if the type of the static data member is found to be an abstract class, then the compiler should issue a diagnostic. This was the reasoning in P0929, which added the current wording in C++20.
With non-static data members, their types are required to be complete at the time of declaration, and the declaration of the non-static data member serves as a definition. So the abstractness must be checked at that point.
Why is it explicitly stated that this refers to non-static members?
Because that allows declaration of static members with incomplete type. Here is a minimal example that is allowed, but wouldn't be allowed if "non-static" wasn't explicitly stated in that rule:
struct S {
// array of unknown bound is an incomplete type
static int arr[];
//int arr2[]; // is not allowed because of the rule
};
int S::arr[3]; // definition of the static member
Does the standard actually allow static data members to be abstract
No, that would contradict the rule quoted by Vlad unless there was a more specific rule overriding it (and there isn't to my knowledge). Specifying the rule to non-static seems to be relevant to incomplete types only; and not to abstract types. As such, listing "abstract type" in the rule explicitly seems redundant and thus confusing (but not contradictory) to me Edit: Brian's answer clarifies why it makes sense.
In the first quote there is an answer to your question
An abstract class can be used only as a base class of some other
class; no objects of an abstract class can be created except as
subobjects of a class derived from it ([basic.def], [class.mem]).
So as a static data member is a separate object relative to object of the class type where it is declared it can not be created as an object of an abstract class. On the other hand, when we are speaking about creating an object of a class type we mean its non-static data members that are sub-objects of the object of the class type. Static data members are instantiated independently of the instantiation of objects of the class where they are declared.

Friend function in c++

Why we always say that friend function are not member function even though they are declared in the class?
I have found in many books and on internet but i am not getting proper explanation.
friend (C++)
If you declare a friend function that was not previously declared,
that function is exported to the enclosing nonclass scope.
In [class.mfct] the C++ standard says (or said around the time of C++11. The link's bit out of date at this point)
Functions declared in the definition of a class, excluding those declared with a friend specifier ([class.friend]), are called member functions of that class.
I'm having difficulty finding similar wording in later drafts of the standard.
That said, I consider this one self-evident.
[class.friend] states:
A friend of a class is a function or class that is given permission to use the private and protected member names from the class. A class specifies its friends, if any, by way of friend declarations. Such declarations give special access rights to the friends, but they do not make the nominated friends members of the befriending class.
A friend is something outside the class that has been granted access by the class to the protected and private members of the class. This implies that a friend is not a member itself.
Note also that the friend function does not have to be implemented within the class. A declaration is sufficient.
Conceptually, a member function is of type Ret(Class::*)(Args...), is called on an instance of the class: instance.member_function(), and has access to the called instance via this. A friend function doesn't fit with "member functions". The function is defined in a separate scope and not in the class even though it looks similar to a member function declaration.

data member with the class name

The standard says,
"A member of a class T cannot use T as its name if the member is a static data member, a member function, a member type, a member template, an enumerator of an unscoped enumeration, a member of a member anonymous union. However, a non-static data member may use the name T as long as there are no user-declared constructors."
However if I create this class, it gives an compile error.
class G
{
int G;
};
I am using VS2013. Is it not allowed in Microsoft or ?
If VC++ doesn't allow this, it's a bug.
However, this language "feature" is for the purpose of C compatibility, and Microsoft has decided not to emphasize C. For example, C99 features are unavailable as a rule until adopted by C++. You should never purposely declare such a member in C++.
(It's allowed in C simply by default: there are no restrictions on the naming of members, and all members are nonstatic data members.)

Are Local class, Inner class and Nested class are the same things in C++?

Are Local class, Inner class and Nested class mean same things in C++?
Local Class and Nested class are different beasts.
A Nested class is a class declared within the scope of another class.
A Local class is declared within a function definition.
Inner class is a non standard C++ term, So I am not sure how to define it.
Nested Classes:
IBM Documentation as a nice documentation here.
To Summarize:
The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.
Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class
Local Classes:
This answer of mine here documents the subtle points associated with local classes.
Quoting draft of C++11 (N3290):
9.7 Nested class declarations [class.nest]
1 A class can be declared within another class. A class declared within another is called a
nested class.
9.8 Local class declarations [class.local]
1 A class can be declared within a function definition; such a class is called a local class.
There is no concept of inner class specified in C++ standard.
Inner classes capture the enclosing class's this reference. A hierarchy of inner class instances form a tree structure. This is unique for Java.

what is the 1 thing you can have in a class declaration that you can never encounter in a union declaration?

I saw this interview question online. Sadly, I can not figure out such a thing... functions, constructor, destructors
btw, I think that struct and class are nearly the same in C++ except that members of a class are private by default, whereas members of a struct are public by default. Inheritance between classes is also private by default, and inheritance between structs is public by default.
And union is different from struct because it all the members at the same spot.
thanks
A union cannot have base classes.
A union also cannot have any data members with nontrivial special member functions, virtual member functions, static data members, or reference data members, but these would appear in the definition of the union. A union cannot be used as a base class, but this applies only to other class declarations, not to the declaration of the union itself.