Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the class keyword before a function argument?

Tags:

c++

c++11

Why does this code work? See the class keyword in front on the f function argument? What does it change if I add it?

struct A
{
    int i;
};

void f(class A pA) // why 'class' here?
{
    cout << pA.i << endl;
}

int main() 
{
    A obj{7};
    f(obj);
    return 0;
}
like image 621
Narek Avatar asked Mar 12 '16 15:03

Narek


People also ask

What is the class keyword in C++?

The class keyword is used to create a class called MyClass . The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class.

What is an argument in a function call?

Arguments are the values passed from a function call (i.e., they are the values appearing inside the parentheses of the call) and are sent into the function). The following example is based on pass-by-value, the most common and familiar argument passing technique.

What is function argument programming?

Said differently, when you create a function, you can pass in data in the form of an argument, also called a parameter. Arguments are variables used only in that specific function. You specify the value of an argument when you call the function. Function arguments allow your programs to utilize more information.

Does the order matter when passing arguments to a function?

Yes, it matters. The arguments must be given in the order the function expects them. C passes arguments by value. It has no way of associating a value with an argument other than by position.


1 Answers

If a function or a variable exists in scope with the name identical to the name of a class type, class can be prepended to the name for disambiguation, resulting in an elaborated type specifier.

You are always allowed to use an elaborated type specifier. Its major use case, however, is when you have a function or variable with an identical name.

Example from cppreference.com:

class T {
public:
    class U;
private:
    int U;
};

int main()
{
    int T;
    T t; // error: the local variable T is found
    class T t; // OK: finds ::T, the local variable T is ignored
    T::U* u; // error: lookup of T::U finds the private data member
    class T::U* u; // OK: the data member is ignored
}
like image 115
wally Avatar answered Oct 11 '22 21:10

wally