1

I need some quick clarification about the using keyword regarding classes since I'm not sure I understand it correctly.

Let's say I have the following example:

class B {
public:
    int var;
    int f(void);

};

class C : public B {protected: using B::var; };

Does that mean that instead of inheriting the variable var as public from class B, class C instead inherits this variable as protected and the only public variable left will be the int f(void);?

Also, could the class C inherit the variable as private by having private: using B::var; inside its body?

And is there any point of writing public: using B::var; since the variable var is already public inside class B?

Thanks!

user
  • 934
  • 6
  • 17
Daeto
  • 440
  • 1
  • 6
  • 19
  • `class C` doesn't inherit publicly from `B` in the first place, right? – qxz Mar 03 '17 at 17:59
  • You are probably right. I should have written the keyword public there. I will edit it. Anyways, if the keyword is missing, at what lvl is class C inheriting from class B then? – Daeto Mar 03 '17 at 18:01
  • Ok good, I thought so but I wasnt sure. – Daeto Mar 03 '17 at 18:04
  • @qxz you were initially correct. `class C : B {}` is PRIVATE inheritance by default, defined by the standard, so you MUST use the `public` keyword if you want public inheritance: `class C : public B {}` – franji1 Mar 03 '17 at 18:15
  • So it is private inheritance by default in c++ 11 ??? – Daeto Mar 03 '17 at 18:18
  • 1
    classes have always been private by default in C++. structs have public by default. This has not changed with the newer standards. – NathanOliver Mar 03 '17 at 18:18
  • 1
    You'd get answers to all your questions faster if you were to try them out by yourself in a c++ training project. – user Mar 03 '17 at 18:20

1 Answers1

5

Does that mean that instead of inheriting the variable var as public from class B, class C instead inherits this variable as protected and the only public variable left will be the int f(void);?

Yes, C::var is now a protected member.

You can test this by trying to compile the following:

class B 
{
public:

    B() : var(0) { }

    int var;

protected:

private:

};


class C : public B 
{ 
public:

    C() : B() { }

protected: 

    using B::var;

private:

};

void main()
{
    B b;
    b.var = 3;    // <-- OK

    C c;
    c.var = 3;    // <-- error C2248
}

Also, could the class C inherit the variable as private by having private: using B::var; inside its body?

Again, yes you can inherit it as private. Though it may be circumvented by accessing the member via B.

class B 
{
public:

    B() : var(0) { }

    int var;

protected:

private:

};


class C : public B
{ 
public:

    C() : B() { }

protected: 

private:

    using B::var;

};

class D : public C
{
public:

    D() : C() 
    { 
        B::var = 3;     // <-- OK
        C::var = 3;     // <-- error C2248
    };

protected:

private:

};

And is there any point of writing public: using B::var; since the variable var is already public inside class B?

No, there is no point. It is redundant.

See Using-declaration: In class definition for more information.

ssell
  • 6,429
  • 2
  • 34
  • 49
  • Thank you very much for this comment! Could you please confirm whether class C : B {protected: using B::var; }; inherits from B as public or private since the keyword is missing? – Daeto Mar 03 '17 at 19:22
  • 2
    @Daeto If you do not specify the type of inheritance, then classes inherit by default to `private`. In your example `B::var` is defined as `public`, but since the inheritance is `private` (by default), `C::var` is private. But due to your `using`, `C::var` is made `protected`. See [Default class inheritance access](http://stackoverflow.com/a/3811480/735425) and [Difference between private, public, and protected inheritance](http://stackoverflow.com/a/1372858/735425). – ssell Mar 03 '17 at 20:54