Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you write "public virtual" or "virtual public" in virtual inheritance?

Based on http://en.wikipedia.org/wiki/Virtual_inheritance

class Animal 
{
...
};

// Two classes virtually inheriting Animal:
class Mammal : public virtual Animal 
{
...
};

I also saw books use the following syntax,

class Mammal : virtual public Animal 
{
...
};

Question> Which is one the C++ standard?

Thank you

like image 750
q0987 Avatar asked Mar 29 '11 02:03

q0987


2 Answers

From ISO/IEC 14882:2003(E) - 10.1

A list of base classes can be specified in a class definition using the notation:

base-clause:
    : base-specifier-list

base-specifier-list:
    base-specifier
    base-specifier-list , base-specifier

base-specifier:
    ::opt nested-name-specifieropt class-name
    virtual access-specifier opt ::opt nested-name-specifieropt class-name
    access-specifier virtual opt ::opt nested-name-specifieropt class-name

access-specifier:
    private
    protected
    public

Notice that either is recommended.

like image 111
Mahesh Avatar answered Oct 19 '22 13:10

Mahesh


Both are standard. Use whichever the local coding conventions require.

like image 39
James Kanze Avatar answered Oct 19 '22 14:10

James Kanze