Implementing virtual methods declared public as private

Discussion in 'C++' started by earthwormgaz, Mar 10, 2010.

  1. earthwormgaz

    earthwormgaz Guest

    Ay up,

    Can I do this? If not, why not :)

    class CBase
    {
    public:
    virtual void method() = 0;
    };

    class CDerived : public CBase
    {
    private:
    virtual void method() { }
    };

    I might want to, because say, I don't want client code calling
    method()?

    Many thanks,

    Gaz
     
    earthwormgaz, Mar 10, 2010
    #1
    1. Advertising

  2. earthwormgaz

    Kai-Uwe Bux Guest

    earthwormgaz wrote:

    > Ay up,
    >
    > Can I do this? If not, why not :)
    >
    > class CBase
    > {
    > public:
    > virtual void method() = 0;
    > };
    >
    > class CDerived : public CBase
    > {
    > private:
    > virtual void method() { }
    > };


    You can do that.

    > I might want to, because say, I don't want client code calling
    > method()?


    That, however, will not exactly be the result:

    #include <iostream>
    #include <ostream>

    class CBase
    {
    public:
    virtual void method() = 0;
    };

    class CDerived : public CBase
    {
    private:
    virtual void method() {
    std::cout << "private implementation\n";
    }
    };


    int main ( void ) {
    CBase * ptr = new CDerived ();
    ptr->method();
    }


    Compiles and calls the private implementation of method().


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 10, 2010
    #2
    1. Advertising

  3. earthwormgaz <> writes:

    > Ay up,
    >
    > Can I do this? If not, why not :)
    >
    > class CBase
    > {
    > public:
    > virtual void method() = 0;
    > };
    >
    > class CDerived : public CBase
    > {
    > private:
    > virtual void method() { }
    > };
    >
    > I might want to, because say, I don't want client code calling
    > method()?


    I'm struggling a little to understand under what circumstances - given
    that you don't want client code calling method() - you would declare it
    public in your base interface in the first place. Can you provide a
    brief snippet by way of example of your thinking here?

    >
    > Many thanks,
    >
    > Gaz


    Regards

    Paul Bibbings
     
    Paul Bibbings, Mar 10, 2010
    #3
  4. On 10 mar, 21:53, Paul Bibbings <> wrote:
    > earthwormgaz <> writes:
    > > Ay up,

    >
    > > Can I do this? If not, why not  :)

    >
    > > class CBase
    > > {
    > >   public:
    > >     virtual void method() = 0;
    > > };

    >
    > > class CDerived : public CBase
    > > {
    > >   private:
    > >     virtual void method() { }
    > > };

    >
    > > I might want to, because say, I don't want client code calling
    > > method()?

    >
    > I'm struggling a little to understand under what circumstances - given
    > that you don't want client code calling method() - you would declare it
    > public in your base interface in the first place.  Can you provide a
    > brief snippet by way of example of your thinking here?


    It might be used when CBase is not directly accessible.

    Example:
    class CFoo {
    protected:
    struct CBase {
    virtual void method() = 0;
    };
    friend class CBar;
    };

    class CBar
    {
    public:
    class CDerived: CFoo::CBase {
    virtual void method() {
    std::cout << "private implementation\n";
    }
    };
    };

    int main ( void ) {
    CBar::CDerived derived;
    // Error: static_cast<CFoo::CBase*>(&derived)->method();
    }

    Typically, this could be the case in some CRTP design.

    --
    Michael
     
    Michael Doubez, Mar 11, 2010
    #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. Charles A. Lackman
    Replies:
    1
    Views:
    1,445
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    641
    SpamProof
    Oct 21, 2003
  3. qazmlp
    Replies:
    19
    Views:
    828
    Daniel T.
    Feb 4, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    449
    Howard
    May 16, 2005
  5. Robert
    Replies:
    16
    Views:
    206
Loading...

Share This Page