5

Could you ensure me, if all access specifiers (including inheritance) in struct are public ?

In other words: are those equal?

class C: public B, public A { public:
    C():A(1),B(2){}
    //...
};

and

struct C: B, A {
    C():A(1),B(2){}
    //...
};
3
  • You could have tested this yourself with a very small program. Commented Jun 7, 2012 at 10:38
  • 3
    Check this thread out about the differences between classes and structs in c++ stackoverflow.com/questions/92859/… Commented Jun 7, 2012 at 10:38
  • 2
    @Nobody: But then he'd rely on his compiler not having bugs :) Commented Jun 7, 2012 at 10:42

3 Answers 3

7

Yes, they all are public.

struct A : B {
  C c;
  void foo() const {}
}

is equivalent to

struct A : public B {
 public:
  C c;
  void foo() const {}
}

For members, it is specified in §11:

Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.

and for for base classes in §11.2:

In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.

where the references are to the C++11 standard.

Sign up to request clarification or add additional context in comments.

Comments

2

From C++ standard, 11.2.2, page 208:

In the absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

So yes, you are correct: when the derived class is a struct, it inherits other classes as public unless you specify otherwise.

Comments

2

From the C++11 Standard (N3242 draft)

11.2 Accessibility of base classes and base class members

2 In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.