Is this right? Can't call protected member of base class from derivedclass method, for another obje

Discussion in 'C++' started by Asfand Yar Qazi, Sep 9, 2003.

  1. Basically this:

    //file 'B.hh'
    class B
    {
    protected:
    void f() {}
    };

    //file 'C.hh'
    #include "B.hh"
    class C
    {
    public:
    void doit(B& arg)
    {
    // do some stuff
    arg.f();
    }
    };

    Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    achieve this, considering I do not want to do something like the following?

    class C;

    class B
    {
    friend class C;
    ....


    Thanks,
    Asfand Yar


    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #1
    1. Advertising

  2. Re: Is this right? Can't call protected member of base class fromderivedclass method, for another object

    >
    > Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    > achieve this, considering I do not want to do something like the following?


    So, why is f protected? By making it protected, that one should not do
    what you are trying to do. Makes no sense.

    Anyway:
    class B
    {
    protected:
    void f() {}
    public:
    void g() {f();}
    }

    call g in C

    --
    Gabriel
     
    Gabriel Schreiber, Sep 9, 2003
    #2
    1. Advertising

  3. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bjk06p$26d$>...
    > Basically this:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}
    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C
    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();
    > }
    > };
    >
    > Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    > achieve this, considering I do not want to do something like the following?


    There is no way of doing it without the friend qualifier if you keep
    f() protected. Protected members can be accessed only in methods of
    its class or its class' descendants.

    Make f() a public member and doit(B& arg) will be able to access it.

    >
    > class C;
    >
    > class B
    > {
    > friend class C;
    > ...
    >
    >
    > Thanks,
    > Asfand Yar


    Marcelo Pinto
     
    Marcelo Pinto, Sep 9, 2003
    #3
  4. Asfand Yar Qazi

    jeffc Guest

    Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:bjk06p$26d$...
    > Basically this:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}
    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C
    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();
    > }
    > };
    >
    > Apparently I can't do this (according to GCC 3.3.1).


    No, obviously not. That's the whole poinf of making something protected.
    Who designed B? If it was you, then there's something not quite right with
    your design.

    > Any tips on how to
    > achieve this, considering I do not want to do something like the

    following?
    >
    > class C;
    >
    > class B
    > {
    > friend class C;


    You can make only the function a friend. But again, why do you want to
    break the design?
     
    jeffc, Sep 9, 2003
    #4
  5. Re: Is this right? Can't call protected member of base class fromderived class method, for another object

    jeffc wrote:
    > "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    > news:bjk06p$26d$...
    >
    >>Basically this:
    >>
    >>//file 'B.hh'
    >>class B
    >>{
    >>protected:
    >>void f() {}
    >>};
    >>
    >>//file 'C.hh'
    >>#include "B.hh"
    >>class C
    >>{
    >>public:
    >>void doit(B& arg)
    >>{
    >>// do some stuff
    >>arg.f();
    >>}
    >>};
    >>
    >>Apparently I can't do this (according to GCC 3.3.1).

    >
    >
    > No, obviously not. That's the whole poinf of making something protected.
    > Who designed B? If it was you, then there's something not quite right with
    > your design.
    >
    >
    >>Any tips on how to
    >>achieve this, considering I do not want to do something like the

    >
    > following?
    >
    >>class C;
    >>
    >>class B
    >>{
    >>friend class C;

    >
    >
    > You can make only the function a friend. But again, why do you want to
    > break the design?
    >
    >


    I forgot one VERY important detail. Typo. Sorry.

    Here are the classes again:

    //file 'B.hh'
    class B
    {
    protected:
    void f() {}
    };

    //file 'C.hh'
    #include "B.hh"
    class C : public B
    {
    public:
    void doit(B& arg)
    {
    // do some stuff
    arg.f();
    }
    };


    I hope you see the typo corrected...........





    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #5
  6. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bjk06p$26d$>...
    > Basically this:
    > ....
    > Any tips on how to
    > achieve this, considering I do not want to do something like the following?
    >
    > class C;
    >
    > class B
    > {
    > friend class C;
    > ...
    >


    Yeah. Forget all about private/public/protected/friend/using,
    make everything public, do your work.

    Otherwise, you'll have make the classes friends.

    stelios
     
    stelios xanthakis, Sep 9, 2003
    #6
  7. Re: Is this right? Can't call protected member of base class fromderived class method, for another object

    Marcelo Pinto wrote:
    > There is no way of doing it without the friend qualifier if you keep
    > f() protected. Protected members can be accessed only in methods of
    > its class or its class' descendants.


    Hi, thanks for the reply. Could you please refer to my reply jeffc
    below? I made a typo in my original post, unfortunately - silly old
    brain of mine! :)




    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #7
  8. Re: Is this right? Can't call protected member of base class fromderivedclass method, for another object

    Gabriel Schreiber wrote:
    >>Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    >>achieve this, considering I do not want to do something like the following?

    >
    >
    > So, why is f protected? By making it protected, that one should not do
    > what you are trying to do. Makes no sense.
    >
    > Anyway:
    > class B
    > {
    > protected:
    > void f() {}
    > public:
    > void g() {f();}
    > }
    >
    > call g in C
    >


    Hi, thanks for the reply. Could you please refer to my reply jeffc
    below? I made a typo in my original post, unfortunately - silly old
    brain of mine! :)




    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #8
  9. Re: Is this right? Can't call protected member of base class fromderived class method, for another object

    Asfand Yar Qazi wrote:
    > Basically this:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}
    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C


    ^^class C : public B ^^^^^^^^^^^^^^^

    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();
    > }
    > };
    >
    > Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    > achieve this, considering I do not want to do something like the following?
    >
    > class C;
    >
    > class B
    > {
    > friend class C;
    > ....
    >
    >
    > Thanks,
    > Asfand Yar
    >
    >



    Typo - whoops.



    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #9
  10. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    Asfand Yar Qazi wrote in news:bjkqnj$96q$:

    >
    > I forgot one VERY important detail. Typo. Sorry.
    >
    > Here are the classes again:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}


    static call_t( B & arg ) { arg.f(); }

    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C : public B
    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();


    call_f( arg );

    > }
    > };
    >
    >


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 9, 2003
    #10
  11. Asfand Yar Qazi

    jeffc Guest

    Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    news:bjkqnj$96q$...
    > >
    > > No, obviously not. That's the whole poinf of making something

    protected.
    > > Who designed B? If it was you, then there's something not quite right

    with
    > > your design.
    > >
    > >
    > >>Any tips on how to
    > >>achieve this, considering I do not want to do something like the

    > >
    > > following?
    > >
    > >>class C;
    > >>
    > >>class B
    > >>{
    > >>friend class C;

    > >
    > >
    > > You can make only the function a friend. But again, why do you want to
    > > break the design?
    > >
    > >

    >
    > I forgot one VERY important detail. Typo. Sorry.
    >
    > Here are the classes again:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}
    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C : public B
    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();
    > }
    > };


    Ah, well that is a more interesting question. However, the answer remains
    the same. The reason is that the argument is not the same thing as the
    object itself. For your code to run, 2 objects must exist. The first
    object is the one that has "doit()" invoked on it. The second object is a
    separate B object that is passed to doit() as a parameter. Inside doit, you
    could defnitely do this:
    void doit(B& arg)
    {
    f();
    }

    Unfortunately, that's not what you're doing. I know it seems strange, but
    when you're not "inside" a B object, then any "outside" B object has to be
    treated through its public interface only. In doit(), you are "inside" a B
    object, but you're trying to access a different instance of an "outside" B
    object.
     
    jeffc, Sep 9, 2003
    #11
  12. Asfand Yar Qazi

    jeffc Guest

    Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "Rob Williscroft" <> wrote in message
    news:Xns93F1A8ABFFF9ukcoREMOVEfreenetrtw@195.129.110.130...
    > Asfand Yar Qazi wrote in news:bjkqnj$96q$:
    >
    > >
    > > I forgot one VERY important detail. Typo. Sorry.
    > >
    > > Here are the classes again:
    > >
    > > //file 'B.hh'
    > > class B
    > > {
    > > protected:
    > > void f() {}

    >
    > static call_t( B & arg ) { arg.f(); }


    Typo? Besides, that looks a bit dodgy.
     
    jeffc, Sep 9, 2003
    #12
  13. Re: Is this right? Can't call protected member of base class fromderived class method, for another object

    >
    > Ah, well that is a more interesting question. However, the answer remains
    > the same. The reason is that the argument is not the same thing as the
    > object itself. For your code to run, 2 objects must exist. The first
    > object is the one that has "doit()" invoked on it. The second object is a
    > separate B object that is passed to doit() as a parameter. Inside doit, you
    > could defnitely do this:
    > void doit(B& arg)
    > {
    > f();
    > }
    >
    > Unfortunately, that's not what you're doing. I know it seems strange, but
    > when you're not "inside" a B object, then any "outside" B object has to be
    > treated through its public interface only. In doit(), you are "inside" a B
    > object, but you're trying to access a different instance of an "outside" B
    > object.
    >
    >


    Hmm... I didn't know that. I do now.

    Many thanks to all who replied.

    --


    http://www.it-is-truth.org/
     
    Asfand Yar Qazi, Sep 9, 2003
    #13
  14. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    jeffc wrote in news::

    >
    > "Rob Williscroft" <> wrote in message

    [snip]
    >> > protected:
    >> > void f() {}

    >>
    >> static call_t( B & arg ) { arg.f(); }

    >
    > Typo? Besides, that looks a bit dodgy.
    >
    >


    Yep, it should have been ... call_f( ....

    But what do you find "dodgy" about it ?

    Would it perhapse be less dodgy if call_f() did what f() does
    all by itself, and was maybe called f( B & arg ) ?

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 9, 2003
    #14
  15. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bjk06p$26d$>...
    > Basically this:
    >
    > //file 'B.hh'
    > class B
    > {
    > protected:
    > void f() {}
    > };
    >
    > //file 'C.hh'
    > #include "B.hh"
    > class C
    > {
    > public:
    > void doit(B& arg)
    > {
    > // do some stuff
    > arg.f();
    > }
    > };
    >
    > Apparently I can't do this (according to GCC 3.3.1). Any tips on how to
    > achieve this, considering I do not want to do something like the following?
    >
    > class C;
    >
    > class B
    > {
    > friend class C;
    > ...
    >
    >
    > Thanks,
    > Asfand Yar



    In your subject you mentioned derived classes, but in the code you
    gave C is not derived from B. That is why you can't access protected
    members of B from C. If you declare C like this it should work:

    #include "B.hh"
    class C : public B // C is derived from B
    {
    // class C details
    };
     
    Michael Klatt, Sep 9, 2003
    #15
  16. Asfand Yar Qazi

    jeffc Guest

    Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "Rob Williscroft" <> wrote in message
    news:Xns93F1B65866ED0ukcoREMOVEfreenetrtw@195.129.110.130...
    >
    > But what do you find "dodgy" about it ?
    >
    > Would it perhapse be less dodgy if call_f() did what f() does
    > all by itself, and was maybe called f( B & arg ) ?


    Well, I guess it depends on how you view the original problem. I suppose if
    you don't like the way the language was designed in this respect, then this
    workaround can be accepted as a way to make it work differently Otherwise,
    the original posted code really shouldn't work and the interface maybe
    should be changed in a more standard way to accomplish what you want to
    accomplish.
     
    jeffc, Sep 9, 2003
    #16
  17. Asfand Yar Qazi

    jeffc Guest

    Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "Michael Klatt" <> wrote in message
    news:...
    >
    > In your subject you mentioned derived classes, but in the code you
    > gave C is not derived from B. That is why you can't access protected
    > members of B from C. If you declare C like this it should work:
    >
    > #include "B.hh"
    > class C : public B // C is derived from B
    > {
    > // class C details
    > };


    Nope, not even then.
     
    jeffc, Sep 9, 2003
    #17
  18. Re: Is this right? Can't call protected member of base class from derived class method, for another object

    "jeffc" <> wrote in message
    news:...
    >
    > "Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
    > news:bjkqnj$96q$...
    > > >


    lines snipped

    > > Here are the classes again:
    > >
    > > //file 'B.hh'
    > > class B
    > > {
    > > protected:
    > > void f() {}
    > > };
    > >
    > > //file 'C.hh'
    > > #include "B.hh"
    > > class C : public B
    > > {
    > > public:
    > > void doit(B& arg)
    > > {
    > > // do some stuff
    > > arg.f();
    > > }
    > > };

    >
    > Ah, well that is a more interesting question. However, the answer remains
    > the same. The reason is that the argument is not the same thing as the
    > object itself. For your code to run, 2 objects must exist. The first
    > object is the one that has "doit()" invoked on it. The second object is a
    > separate B object that is passed to doit() as a parameter. Inside doit,

    you
    > could defnitely do this:
    > void doit(B& arg)
    > {
    > f();
    > }
    >
    > Unfortunately, that's not what you're doing. I know it seems strange, but
    > when you're not "inside" a B object, then any "outside" B object has to be
    > treated through its public interface only. In doit(), you are "inside" a

    B
    > object, but you're trying to access a different instance of an "outside" B
    > object.
    >
    >


    I'm also trying to understand this. The above seems to imply it is based on
    whether the invoking object
    is the same as the object owning the method. At least according to
    VisualC++ 7,


    class B
    {
    protected:
    void f() {}
    virtual ~B(){}
    };

    class C : public B
    {
    public:
    void doit(B& arg1, C &arg2)
    {
    // do some stuff
    arg1.f(); //error
    arg2.f(); //ok
    (dynamic_cast<C*>(&arg1))->f();//ok
    doit2(*this);
    this->f();//ok
    (dynamic_cast<C*>((dynamic_cast<B*>(this))))->f();//ok
    (dynamic_cast<B*>(this))->f(); //error
    }

    private:
    void doit2(C& arg) { };
    };

    int main()
    {}

    The instance wouldn't seem to matter from this. It also seems odd that a C
    object can call
    the protected B method of a different C object, but not of a different B
    object.

    What are the rules?
     
    Douglas Stuart, Sep 12, 2003
    #18
    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.

Share This Page