Topic: inheritence/protection questions


Author: l.jonsson@abalon.se (Lars Jonsson)
Date: 1995/05/18
Raw View
Hi!

Can a static member function access protected members of a base
class? Does a derived class that does not have any non-static
member functions or attributes need a constructor if the base class
has one?

The following code compiles under gnu but MS Visual C++ (2.0) says:

line 16: class 'B' can never be instantiated; user-defined constructor is required
line 20: 'setValue' : cannot access protected member declared in class 'A'

class A
{
  private:
    int value;
  protected:
    void setValue(int x) { value = x; }
  public:
    A(int x)             { value = x; }
    int getValue()       { return value; }
};

class B : public A
{
  public:
    static void doubleValue(A* a);
};                                 // *** line 16

void B::doubleValue(A* a)
{
  a->setValue(2 * a->getValue());   // *** line 20
}



Regards,


Lars Jonsson
Abalon AB, Sweden
larsj@abalon.se





Author: kocher@lts.sel.alcatel.de (Hartmut Kocher US/ESA 60/3/140 #45629)
Date: 1995/05/18
Raw View
In article <1995May18.090227.14449@abalon.se>, l.jonsson@abalon.se (Lars Jonsson) writes:
|> Hi!
|>
|> Can a static member function access protected members of a base
|> class? Does a derived class that does not have any non-static
|> member functions or attributes need a constructor if the base class
|> has one?
|>
|> The following code compiles under gnu but MS Visual C++ (2.0) says:
|>
|> line 16: class 'B' can never be instantiated; user-defined constructor is required
|> line 20: 'setValue' : cannot access protected member declared in class 'A'
|>
|> class A
|> {
|>   private:
|>     int value;
|>   protected:
|>     void setValue(int x) { value = x; }
|>   public:
|>     A(int x)             { value = x; }
|>     int getValue()       { return value; }
|> };
|>
|> class B : public A
|> {
|>   public:
|>     static void doubleValue(A* a);
|> };                                 // *** line 16
|>
|> void B::doubleValue(A* a)
|> {
|>   a->setValue(2 * a->getValue());   // *** line 20
|> }

MS is right.

B needs a constructor, because as part of its initialization, it has
to call the constructor of its base class A. Because A doesn't
have a default constructor, B must explicitly call A's constructor.
Therefore, you cannot create an object of B. That's what MS says.
However, because you never instantiate a B object, this should be
a warning and not an error message. I think, it should be legal to
to use B's static functions even if no constructor is provided...

Protected members can only be accessed thru a pointer to the *derived* class.
You try to access it with a pointer to base class.
Therefore, line 20 is clearly in error!

--
+==============================|==============================+
| Dr. Hartmut Kocher           |                              |
| Technical Consultant         | All opinions expressed here  |
| Rational GmbH                | are my own.                  |
| Rosenstrasse 7               |                              |
| 82049 Pullach im Isartal     | I know you guessed it,       |
| Germany                      | but it keeps my lawyer happy.|
| Email: kocher@lts.sel.alcatel.de                            |
+==============================|==============================+





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/18
Raw View
l.jonsson@abalon.se (Lars Jonsson) writes:

>Can a static member function access protected members of a base
>class?

Only via a pointer or reference to the class containing the
static member or one of its derived classes - not via a pointer
or reference to the base class.

>Does a derived class that does not have any non-static
>member functions or attributes need a constructor if the base class
>has one?

Yes.

>The following code compiles under gnu but MS Visual C++ (2.0) says:
>
>line 16: class 'B' can never be instantiated; user-defined constructor is required
>line 20: 'setValue' : cannot access protected member declared in class 'A'

When I tried it with g++ 2.6.3 I got the following:

g.c:16: warning: base `A' with only non-default constructor
g.c:16: warning: in class without a constructor
g.c: In function `static void B::doubleValue(class A *)':
g.c:6: method `void A::setValue(int)' is protected
g.c:20: within this context

Perhaps you were using an obsolete version of GNU C++?

--
Fergus Henderson                       | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au                        | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh            |             - Linus Torvalds