What does the standard say about this

Discussion in 'C++' started by Xenos, Jul 10, 2004.

  1. Xenos

    Xenos Guest

    I just spend about an hour tracking down a link error with GCC. I had
    defined a virtual member function, but never referenced it. With this
    declaration, the linker would complain that the virtual table for the class
    was missing. I know that it is acceptable by the standard to declare a
    non-virtual member but not define it, if it is never accessed. Is the same
    true for virtual members?

    Thanks,

    DrX
     
    Xenos, Jul 10, 2004
    #1
    1. Advertising

  2. Xenos wrote in news:9DTHc.28395$ in
    comp.lang.c++:

    > I just spend about an hour tracking down a link error with GCC. I had
    > defined a virtual member function, but never referenced it. With this
    > declaration, the linker would complain that the virtual table for the
    > class was missing. I know that it is acceptable by the standard to
    > declare a non-virtual member but not define it, if it is never
    > accessed. Is the same true for virtual members?
    >


    No, but you can declare the member pure-virtual, in which case
    (as long as it isn't the destructor) you don't need a defenition.

    However it makes the class *abstract*, i.e. you may not create
    instances of it (even if you do provide a defenition).

    struct X
    {
    virtual int f() = 0; /* = 0 means: pure */
    };

    struct Y : X
    {
    virtual int f();
    };

    int Y::f()
    {
    return 0;
    }

    int main()
    {
    Y y;
    return y.f();
    }

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jul 10, 2004
    #2
    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. Dave Rahardja
    Replies:
    5
    Views:
    1,142
    John Harrison
    Jul 18, 2003
  2. Tobias Oed
    Replies:
    11
    Views:
    1,158
    Dan Pop
    Jun 30, 2003
  3. Sten Westerback

    Re: What does the standard say?

    Sten Westerback, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    424
    Rob Thorpe
    Apr 6, 2004
  4. r martin

    Re: What does the standard say?

    r martin, Apr 3, 2004, in forum: C Programming
    Replies:
    6
    Views:
    357
    Raymond Martin
    Apr 5, 2004
  5. David Mathog
    Replies:
    24
    Views:
    867
    Eric Sosman
    Jun 3, 2004
Loading...

Share This Page