Topic: protected access and constructor


Author: Edward Diener <eddielee@abraxis.com>
Date: 1997/07/11
Raw View
Srinivas Vobilisetti wrote:
 >
 > In the following peice of code, it allows to create base class object
 > even if the constructor is protected, from a derived class method
 > (Derived::getValue()). I am little confused about this. Isn't it
 > contradicting the definition of protected access?
 >
 > Srinivas
 >
 > #include <iostream.h>
 >
 > class Base {
 > protected:
 >    int i;
 >    Base(int j) : i(j) { }
 > };
 >
 > class Derived : public Base {
 > public:
 >    Derived(int j) : Base(j) { }
 >    int getValue();
 > };
 >
 > int Derived::getValue()
 > {
 >    Base b(i);  // Allows to access protected base class constructor
for
 >                // a base class object. This does not make sense.
 >    return b.i; // But does not allow access to protected data member
of
 >                // a base class object. This makes sense.
 > }
 >
 > int main()
 > {
 >    Derived d(2);
 >    cout << d.getValue() << '\n';
 > }

Both situations are errors. Borland C++ 5.02 gives both of them as
errors and I think that is right. Your compiler has a bug in it to allow
the Base b(i); line to get by without an error message.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with
    ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu
    ]
[ FAQ:
http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu
    ]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com>
Date: 1997/07/07
Raw View
In the following peice of code, it allows to create base class object
even if the constructor is protected, from a derived class method
(Derived::getValue()). I am little confused about this. Isn't it
contradicting the definition of protected access?

Srinivas

#include <iostream.h>

class Base {
protected:
   int i;
   Base(int j) : i(j) { }
};

class Derived : public Base {
public:
   Derived(int j) : Base(j) { }
   int getValue();
};

int Derived::getValue()
{
   Base b(i);  // Allows to access protected base class constructor for
               // a base class object. This does not make sense.
   return b.i; // But does not allow access to protected data member of
               // a base class object. This makes sense.
}

int main()
{
   Derived d(2);
   cout << d.getValue() << '\n';
}
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com>
Date: 1997/07/08
Raw View
M Chandrashekhar wrote:
 >
 > > In the following peice of code, it allows to create base class object
 > > even if the constructor is protected, from a derived class method
 > > (Derived::getValue()). I am little confused about this. Isn't it
 > > contradicting the definition of protected access?
 > >
 > > Srinivas
 > >
 > > #include <iostream.h>
 > >
 > > class Base {
 > > protected:
 > >    int i;
 > >    Base(int j) : i(j) { }
 > > };
 > >
 > > class Derived : public Base {
 > > public:
 > >    Derived(int j) : Base(j) { }
 > >    int getValue();
 >
 > > };
 > >
 > > int Derived::getValue()
 > > {
 > >    Base b(i);  // Allows to access protected base class constructor for
 > >                // a base class object. This does not make sense.
 > >    return b.i; // But does not allow access to protected data member of
 > >                // a base class object. This makes sense.
 > > }
 >
 > First things first - My compiler doesn't allow either statement.
 > I get compiler errors for both.
 > I experimented a little bit and I'm a bit surprised too.
 > Apparently, the access rights are on a object level basis i.e, I can
 > modify "base member variables" of the SAME object in the derived class
 > functions.
 > But if I have a base class object, say passed to a function of a derived
 > class object, I can't access the protected member variables/funcs of
 > that object!!
 >
 > I'll have to check the draft to clear this.
 >
 > Chandru

The meaning of protected is little different from the rest. You can
access base class members of another object only if the other object
(static) type is same as your object's type or derived from the type of
your object.

Srinivas
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]