inheritance question

Discussion in 'C++' started by hack_tick, Jul 12, 2004.

  1. hack_tick

    hack_tick Guest

    hi there
    I m having a class A

    class A
    {
    private:
    virtual void foo();
    };

    and class B

    class B : public A
    {
    protected:
    void foo();
    }

    then i reimplement my version of foo
    the problem i m facing is, i also require functionality of foo() from the
    base class A, if the foo() would have been declared as protected then i
    would have been able to access it, but since its private i cannot access it
    after inheritance

    i would like to do something similar to this

    void B::foo()
    {
    /*my own functionality*/
    A::foo();
    }

    any suggestion ??
     
    hack_tick, Jul 12, 2004
    #1
    1. Advertising

  2. hack_tick

    Sharad Kala Guest

    "hack_tick" <> wrote in message
    news:...
    > hi there
    > I m having a class A
    >
    > class A
    > {
    > private:
    > virtual void foo();
    > };
    >
    > and class B
    >
    > class B : public A
    > {
    > protected:
    > void foo();
    > }
    >

    Template pattern.

    class A
    {
    public:
    void f() {A::foo(); foo();}
    private:
    virtual void foo(){cout << "Base foo";}
    };


    class B : public A
    {
    protected:
    void foo(){cout << "Derived foo";}
    };

    int main()
    {
    A* b = new B;
    b->f();
    delete b;
    }

    -Sharad
     
    Sharad Kala, Jul 12, 2004
    #2
    1. Advertising

  3. On Mon, 12 Jul 2004 12:05:19 +0530, hack_tick <> wrote:

    > hi there
    > I m having a class A
    >
    > class A
    > {
    > private:
    > virtual void foo();
    > };
    >
    > and class B
    >
    > class B : public A
    > {
    > protected:
    > void foo();
    > }
    >
    > then i reimplement my version of foo
    > the problem i m facing is, i also require functionality of foo() from the
    > base class A, if the foo() would have been declared as protected then i
    > would have been able to access it, but since its private i cannot access
    > it
    > after inheritance
    >
    > i would like to do something similar to this
    >
    > void B::foo()
    > {
    > /*my own functionality*/
    > A::foo();
    > }
    >
    > any suggestion ??
    >



    The only possibility I know of is the old hack

    #define private protected

    but that breaks every C++ rule in the book (well a few of them anyway) and
    therefore is not guaranteed to work.

    Presumably the author of class A had a good reason for declaring foo
    private. They had some reason why what you are trying to do is a bad idea.
    I suggest you talk to them.

    john
     
    John Harrison, Jul 12, 2004
    #3
  4. On Mon, 12 Jul 2004 12:10:36 +0530, Sharad Kala
    <> wrote:

    >
    > "hack_tick" <> wrote in message
    > news:...
    >> hi there
    >> I m having a class A
    >>
    >> class A
    >> {
    >> private:
    >> virtual void foo();
    >> };
    >>
    >> and class B
    >>
    >> class B : public A
    >> {
    >> protected:
    >> void foo();
    >> }
    >>

    > Template pattern.
    >
    > class A
    > {
    > public:
    > void f() {A::foo(); foo();}
    > private:
    > virtual void foo(){cout << "Base foo";}
    > };
    >
    >
    > class B : public A
    > {
    > protected:
    > void foo(){cout << "Derived foo";}
    > };
    >
    > int main()
    > {
    > A* b = new B;
    > b->f();
    > delete b;
    > }
    >
    > -Sharad
    >


    I assumed that the OP wasn't in a position to modify class A. Otherwise
    s/he would have just changed private to protected.

    john
     
    John Harrison, Jul 12, 2004
    #4
  5. hack_tick

    Sharad Kala Guest

    "John Harrison" <> wrote in message
    news:eek:psa0igf06212331@andronicus...
    > On Mon, 12 Jul 2004 12:10:36 +0530, Sharad Kala
    > <> wrote:
    >
    >
    > I assumed that the OP wasn't in a position to modify class A. Otherwise
    > s/he would have just changed private to protected.


    Yep, I realized that too after reading your reply. But frankly that's the
    last thing or may be just not the right thing (and I think you agree too)
    to solve the problem.

    -Sharad
     
    Sharad Kala, Jul 12, 2004
    #5
  6. hack_tick

    hack_tick Guest

    hi there
    "Sharad Kala" <> wrote in message
    news:...
    [...]
    > Template pattern.
    >
    > class A
    > {
    > public:
    > void f() {A::foo(); foo();}
    > private:
    > virtual void foo(){cout << "Base foo";}
    > };
    >
    >
    > class B : public A
    > {
    > protected:
    > void foo(){cout << "Derived foo";}
    > };
    >
    > int main()
    > {
    > A* b = new B;
    > b->f();
    > delete b;
    > }


    thank you for your reply, one thing i didnt mentioned in my previous post
    was class "A" is a SDK header file, so modifying it is not the solution :)

    -regards,
    and once again thx for such a quick response :)
     
    hack_tick, Jul 12, 2004
    #6
  7. hack_tick

    hack_tick Guest

    hi there,
    "John Harrison" <> wrote in message
    news:eek:psa0idpq7212331@andronicus...
    [...]
    > The only possibility I know of is the old hack
    >
    > #define private protected
    >
    > but that breaks every C++ rule in the book (well a few of them anyway) and
    > therefore is not guaranteed to work.
    >
    > Presumably the author of class A had a good reason for declaring foo
    > private. They had some reason why what you are trying to do is a bad idea.
    > I suggest you talk to them.


    thankx for the reply that looks like a viable hack for now, actually the
    function is declared "virtual" in the base class, i dont know why did the
    author kept it virtual if he dosnt want it to be overridden :-( is there any
    other use of "virtual" keyword for the "member functions" ???

    regards :)
     
    hack_tick, Jul 12, 2004
    #7
  8. On Mon, 12 Jul 2004 12:29:56 +0530, hack_tick <> wrote:

    > hi there,
    > "John Harrison" <> wrote in message
    > news:eek:psa0idpq7212331@andronicus...
    > [...]
    >> The only possibility I know of is the old hack
    >>
    >> #define private protected
    >>
    >> but that breaks every C++ rule in the book (well a few of them anyway)
    >> and
    >> therefore is not guaranteed to work.
    >>
    >> Presumably the author of class A had a good reason for declaring foo
    >> private. They had some reason why what you are trying to do is a bad
    >> idea.
    >> I suggest you talk to them.

    >
    > thankx for the reply that looks like a viable hack for now, actually the
    > function is declared "virtual" in the base class, i dont know why did the
    > author kept it virtual if he dosnt want it to be overridden :-( is there
    > any
    > other use of "virtual" keyword for the "member functions" ???


    He does want it to be overridden, and if you override it your overridden
    function will be called. But he only wants it to be called from within
    class A, not from within a derived class.

    Why that might be I couldn't say without knowing more about the classes.

    I might add, how do you know what A::foo does? If it's a private function
    why has it been documented (that is strange). Perhaps one reason it is
    private is that the author wants to reserve the right to change it in a
    non-backwards compatible way in the future. Are you sure you need to call
    A:foo?

    john
     
    John Harrison, Jul 12, 2004
    #8
  9. hack_tick

    Sharad Kala Guest

    "hack_tick" <> wrote in message
    news:...
    > hi there,
    > "John Harrison" <> wrote in message
    > thankx for the reply that looks like a viable hack for now, actually the
    > function is declared "virtual" in the base class, i dont know why did the
    > author kept it virtual if he dosnt want it to be overridden :-( is there

    any
    > other use of "virtual" keyword for the "member functions" ???


    Access privilege and virtualness are independent of each other. A private
    virtual function means that derived classes may or may not override it as
    they choose, however they can not invoke its implementation.
     
    Sharad Kala, Jul 12, 2004
    #9
  10. hack_tick

    hack_tick Guest

    hi there
    "John Harrison" <> wrote in message
    news:eek:psa0jiqsx212331@andronicus...
    [...]
    > He does want it to be overridden, and if you override it your overridden
    > function will be called. But he only wants it to be called from within
    > class A, not from within a derived class.
    >
    > Why that might be I couldn't say without knowing more about the classes.
    >
    > I might add, how do you know what A::foo does? If it's a private function
    > why has it been documented (that is strange). Perhaps one reason it is
    > private is that the author wants to reserve the right to change it in a
    > non-backwards compatible way in the future. Are you sure you need to call
    > A:foo?


    Actually i m overwriding default Eventhandler provided with my Platform SDK
    which is triggered by the framework, i dont know the exact way the funcion
    is implemented but i have the rough Idea what it does, the other alternative
    would be to duplicate the code what it does, but that would lead to another
    issues of testing the code, so i was looking for some way to maintian the
    basic functionality of what the basic code does,

    i m basically preparing log of all the events triggered(testing the OOP SDK)
    and also maintaining the dafault functionality of the SDK methods :)

    regards
     
    hack_tick, Jul 12, 2004
    #10
  11. hack_tick

    JKop Guest

    hack_tick posted:

    > hi there
    > "Sharad Kala" <> wrote in

    message
    > news:...
    > [...]
    >> Template pattern.
    >>
    >> class A
    >> {
    >> public:
    >> void f() {A::foo(); foo();} private:
    >> virtual void foo(){cout << "Base foo";} };
    >>
    >>
    >> class B : public A { protected:
    >> void foo(){cout << "Derived foo";} };
    >>
    >> int main()
    >> {
    >> A* b = new B;
    >> b->f();
    >> delete b;
    >> }

    >
    > thank you for your reply, one thing i didnt mentioned in

    my previous
    > post was class "A" is a SDK header file, so modifying it

    is not the
    > solution :)
    >
    > -regards,
    > and once again thx for such a quick response :)



    copy blah.h my_blah.h

    edit my_blah.h

    #include <my_blah.h>


    -JKop
     
    JKop, Jul 12, 2004
    #11
  12. hack_tick

    hack_tick Guest

    "JKop" <> wrote in message
    news:ddvIc.4398$...
    [..]
    > copy blah.h my_blah.h
    >
    > edit my_blah.h
    >
    > #include <my_blah.h>


    ;-)
     
    hack_tick, Jul 12, 2004
    #12
  13. In message <ddvIc.4398$>, JKop <>
    writes
    >copy blah.h my_blah.h
    >
    >edit my_blah.h
    >
    >#include <my_blah.h>
    >

    And remember to do it every time blah.h changes...

    And don't forget to set some time aside to look for a new job.

    --
    Richard Herring
     
    Richard Herring, Jul 12, 2004
    #13
    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. maxw_cc
    Replies:
    1
    Views:
    3,180
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    842
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,061
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,943
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    788
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page