Re: pure virtual functions as non-const in derived classes

Discussion in 'C++' started by Gianni Mariani, Aug 27, 2003.

  1. Victor Hannak wrote:
    > All the documentation I have looked through (the FAQ, textbooks) all show
    > pure virtual functions declared as const.


    No problem whatsoever.

    Why not give it a go before posting ?

    >
    > Is there anything wrong with instantiating a non-const pure virtual
    > function? What if I need the polymorphed version of the function to be able
    > to modify private data members of the derived class? I wrote some code with
    > the pure virtual function not defined as const, and it seems to work fine,
    > but is this bad coding style? Why doesn't any of the documentation have
    > examples like this?


    Bad docs ?

    >
    > I understand why the definition of the pure virtual function in the ABC is
    > const. After all, it can't/shouldn't do anything in the ABC. But if I make
    > it const, then I also have to make the polymorphed versions of it const as
    > well in order for the declaration to match, right? (Otherwise, it seems
    > that the compiler does not see the relationship between the abstract and
    > derived version of the function, and I get an error about my derived class
    > also being an ABC)
    >


    class ABC
    {
    public:
    virtual void F();
    };

    class DC : public ABC
    {
    public:
    int x;
    virtual void F()
    {
    x=1;
    }
    };

    .... try that !
     
    Gianni Mariani, Aug 27, 2003
    #1
    1. Advertising

  2. >
    > class ABC
    > {
    > public:
    > virtual void F();
    > };
    >
    > class DC : public ABC
    > {
    > public:
    > int x;
    > virtual void F()
    > {
    > x=1;
    > }
    > };
    >
    > ... try that !
    >


    I am not trying to nitpick, but I want to understand this...the virtual
    declaration for F() in class ABC above is missing the " = 0 " (see FAQ
    22.4), and therefore is _not_ a pure virtual function, right? Thus, you
    would have to define the functionality of F() in class ABC and class ABC
    would no longer be abstract. Is this right?

    Thanks, Vic
     
    Victor Hannak, Aug 27, 2003
    #2
    1. Advertising

  3. Victor Hannak wrote:

    >
    > I am not trying to nitpick, but I want to understand this...the virtual
    > declaration for F() in class ABC above is missing the " = 0 " (see FAQ
    > 22.4), and therefore is _not_ a pure virtual function, right?


    YES.

    Thus, you
    > would have to define the functionality of F() in class ABC and class ABC
    > would no longer be abstract. Is this right?


    YES

    >
    > Thanks, Vic


    I'm glad you asked..

    All F() = 0 means is that the class is abstract (abstract meaning that
    it may not be instantiated without being derived). It does not mean you
    can't call it. See below.

    Also, a const method is distinct from a non-const method of the same name.


    #include <iostream>

    class ABC
    {
    public:
    virtual void F() = 0;
    virtual void F() const = 0;
    };

    class DC : public ABC
    {
    public:
    int x;
    virtual void F()
    {
    x=1;
    std::cout << "DC::F\n";
    }

    virtual void F() const
    {
    // can't do x=1;
    std::cout << "DC::F const\n";
    }
    };

    void ABC::F()
    {
    std::cout << "ABC::F\n";
    }

    void ABC::F() const
    {
    std::cout << "ABC::F const\n";
    }


    int main()
    {
    ABC * p = new DC;

    p->F();

    const ABC * pc = p;

    pc->F();

    pc->ABC::F();

    delete p; // VERY BAD - MUST MAKE ABC destructor virtual
    }

    code prints:

    DC::F
    DC::F const
    ABC::F const

    Any class with a virtual method should have a virtual destructor.
     
    Gianni Mariani, Aug 27, 2003
    #3
    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. jeffc
    Replies:
    0
    Views:
    359
    jeffc
    Aug 27, 2003
  2. Steven T. Hatton
    Replies:
    5
    Views:
    980
    Steven T. Hatton
    May 23, 2005
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. reppisch
    Replies:
    6
    Views:
    408
    Andrey Tarasevich
    May 6, 2008
Loading...

Share This Page