Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional semicolon in C++

Tags:

c++

The following code compiled fine (without semicolon after each line). Why are semicolons not needed at the end of each lines under the public section?

Note: putting a semicolon after each lines is fine also, so it seems like using semicolon here is optional.

template<typename T>

class Accessor {

    public:

        explicit Accessor(const T& data) : value(data) {}

        Accessor& operator=(const T& data) { value = data; return *this; }
        Accessor& operator=(const Accessor& other) { this->value = other.value; return *this; }
        operator T() const { return value; }
        operator T&() { return value; }

    private:

        Accessor(const Accessor&);
        T value;
};
like image 914
Zzz Zz Avatar asked Jul 07 '12 14:07

Zzz Zz


1 Answers

You don't need semicolons after a method definition.

The reason a semicolon is needed after a class definition is that you can declare instances of the class right after the definition:

class X
{

} x;

//x is an object of type X

For method, this argument obviously doesn't hold, so a semicolon would be redundant (although legal).

like image 125
Luchian Grigore Avatar answered Oct 27 '22 07:10

Luchian Grigore