Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a syntax error in C++ to end a function inside a class definition with a };?

This code was provided to me by my teacher, as an example of functions in a C++ class. It seems strange to me. I've always been used to ending a function with a }. I think that only the class definition ends with };. Here the function definitions end with };. How is a function in a class, besides being public or private, different from a stand alone function? Is this just a quirk or peculiarity of C++?

class GenericItem {
public:
   void SetName(string newName) {
      itemName = newName;
   };

   void SetQuantity(int newQty) {
      itemQuantity = newQty;
   };

   void PrintItem() {
      cout << itemName << " " << itemQuantity << endl;
   };

private:
   string itemName;
   int itemQuantity;
};
like image 595
Galaxy Avatar asked Dec 02 '16 23:12

Galaxy


People also ask

Can I define a function inside a class?

Functions should be declared inside the class to bound it to the class and indicate it as it's member but they can be defined outside of the class. To define a function outside of a class, scope resolution operator :: is used.

What is class definition give its syntax and example?

A class definition is a process of naming a class and data variables and interface operations of the class. A class is a structured datatype in C++ which is a collection variables and functions.

What is a class explain about its syntax?

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

Do you need semicolon after function in C++?

C++ semicolon is used to tell the compiler where a statement ends. The body of a function or any scope block isn't a statement in and of itself so it's pretty much only needed after } when defining an object.


2 Answers

It is legal and has always been legal (since C++98), but syntactic meaning of that ; has changed from C++11 to C++14.

In C++11 (and before) that ; is allowed, and it is an optional part of member function definition.

member-declaration:
    decl-specifier-seqopt member-declarator-listopt ;
    function-definition ;opt
    ::opt nested-name-specifier templateopt unqualified-id ;
    using-declaration
    template-declaration

It is not an empty declaration. (C++11 does not support empty declarations in class scope. Empty declarations are allowed in namespace scope only.) So, in case of C++11 (and before) that extra ; is indeed just a quirk of language grammar. Why it has always been allowed is not entirely clear to me.

In C++14 (per n4296) this ; is no longer a part of member function definition. It is a standalone empty declaration, which is legal in class scope since C++14.

member-declaration:
    attribute-specifier-seqopt decl-specifier-seqopt member-declarator-listopt;
    function-definition
    using-declaration
    static_assert-declaration
    template-declaration
    alias-declaration
    empty-declaration

This basically means that the following definition is invalid in C++11 (and before), but is valid in C++14

class C
{
  void foo() {};;
};

(Curiously enough, GCC rejects it in C++14 mode.)

like image 121
AnT Avatar answered Sep 20 '22 08:09

AnT


It's completely valid. The extra semicolon delimits an "empty" declaration.

It's not really a "quirk", since it's valid after non-member functions too.

It is strange to use a ; when you don't have to, though, so I don't know why your teacher showed it to you this way. It's up to you, but I would generally recommend not using the extra ; (and your compiler, as seen in the warnings it emits for the above code, agrees with me).

Of course, you could simply have tried it to find out.

like image 21
Lightness Races in Orbit Avatar answered Sep 21 '22 08:09

Lightness Races in Orbit