Is this valid C++ (protected member of base accessed in derived class function)

Discussion in 'C++' started by Shelly Adhikari, Sep 10, 2003.

  1. class B {
    public:
    B() { }
    ~B() { }
    protected:
    int abc;
    void g() {}
    };

    class C : public B {
    public:
    void f(B* p) {
    p->g(); // Should this be an error?
    int m = p->abc; // Should this be an error?
    int n = abc;
    g();
    }
    };

    int main() {
    B* y = new B();
    C x;
    x.f(y);
    }

    If you could also point me to the ISO C++ standard section and page
    number, it would be great.
    Shelly Adhikari, Sep 10, 2003
    #1
    1. Advertising

  2. Re: Is this valid C++ (protected member of base accessed in derivedclass function)

    Shelly Adhikari escribió:

    > class B {
    > public:
    > B() { }
    > ~B() { }
    > protected:
    > int abc;
    > void g() {}
    > };
    >
    > class C : public B {
    > public:
    > void f(B* p) {
    > p->g(); // Should this be an error?
    > int m = p->abc; // Should this be an error?


    Yes, they are errors. You have access only to a the B that is part of a
    C, not to a unrelated B.

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Sep 10, 2003
    #2
    1. Advertising

  3. Shelly Adhikari

    Ron Natalie Guest

    "Shelly Adhikari" <> wrote in message
    > p->g(); // Should this be an error?
    > int m = p->abc; // Should this be an error?

    Yes, these are both errors.
    > int n = abc;
    > g();

    These are OK.

    Protected only lets you get at your own instances (this's) subobjects.
    Ron Natalie, Sep 10, 2003
    #3
  4. Shelly Adhikari

    red floyd Guest

    Re: Is this valid C++ (protected member of base accessed in derivedclass function)

    Shelly Adhikari wrote:
    > [protected member access question redacted]


    To whoever maintains the FAQs:

    Is this one in the FAQs? It's been asked at least five times in the past week. If it isn't,
    it should be in there.
    red floyd, Sep 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran
    Replies:
    4
    Views:
    803
    Micah Cowan
    Jan 12, 2005
  2. Andy Lomax
    Replies:
    5
    Views:
    465
    John Carson
    Jun 30, 2005
  3. Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    386
    Victor Bazarov
    May 23, 2007
  5. blangela
    Replies:
    8
    Views:
    663
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page