unexpected 'pure virtual function' error

Discussion in 'C++' started by Corno, Aug 5, 2006.

  1. Corno

    Corno Guest

    Hi all,

    I thought that the 2 following functions would have the same effect;

    void first()
    {
    ClassA a("bla");
    anotherFunction(a);
    }

    void second()
    {
    anotherFunction(ClassA("bla"));
    }

    but when using the second alternative I get a 'calling pure virtual
    function' error.
    Can anybody explain what the difference is between the 2 that would explain
    the error?

    TIA,

    Corno
     
    Corno, Aug 5, 2006
    #1
    1. Advertising

  2. * Corno:
    >
    > I thought that the 2 following functions would have the same effect;
    >
    > void first()
    > {
    > ClassA a("bla");
    > anotherFunction(a);
    > }
    >
    > void second()
    > {
    > anotherFunction(ClassA("bla"));
    > }
    >
    > but when using the second alternative I get a 'calling pure virtual
    > function' error.
    > Can anybody explain what the difference is between the 2


    In the second case you're using a temporary.


    > that would explain the error?


    Nope, for that you'll have to post a complete small program that
    demonstrates the problem (one possibility, though, is that ClassA's copy
    constructor is invoked and calls a pure virtual function).

    See the FAQ item "How do I post a question about code that doesn't work
    correctly?" currently at <url:
    http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 5, 2006
    #2
    1. Advertising

  3. Corno

    Corno Guest

    >> Can anybody explain what the difference is between the 2
    >
    > In the second case you're using a temporary.
    >
    >> that would explain the error?

    >
    > Nope, for that you'll have to post a complete small program that
    > demonstrates the problem (one possibility, though, is that ClassA's copy
    > constructor is invoked and calls a pure virtual function).
    >
    > See the FAQ item "How do I post a question about code that doesn't work
    > correctly?"


    I see your point about posting the code. However I don't think code is
    necessary in this case. The fact that the error I got was specifically a
    'pure virtual function call' is not that relevant to me. I trying to
    understand how the C++ standard handles temporary variables differently from
    named variables. You talk about the copy constructor, are temporary
    variables always copied before the statement is executed or is there
    anything else the standard has to say about this?

    TIA,

    Corno
     
    Corno, Aug 6, 2006
    #3
  4. * Corno:
    >>> Can anybody explain what the difference is between the 2

    >> In the second case you're using a temporary.
    >>
    >>> that would explain the error?

    >> Nope, for that you'll have to post a complete small program that
    >> demonstrates the problem (one possibility, though, is that ClassA's copy
    >> constructor is invoked and calls a pure virtual function).
    >>
    >> See the FAQ item "How do I post a question about code that doesn't work
    >> correctly?"

    >
    > I see your point about posting the code. However I don't think code is
    > necessary in this case. The fact that the error I got was specifically a
    > 'pure virtual function call' is not that relevant to me. I trying to
    > understand how the C++ standard handles temporary variables differently from
    > named variables. You talk about the copy constructor, are temporary
    > variables always copied before the statement is executed or is there
    > anything else the standard has to say about this?


    (Please do include relevant context).

    You're talking about the call

    anotherFunction(ClassA("bla"));

    No, temporaries are not always copied, and AFAIK in C++0x the temporary
    will be guaranteed to /not/ be copied. However, the current standard
    allows any number of copies to be made, and requires an accessible copy
    constructor of the form T(T const&). Whether a copy is actually made
    depends on the compiler, the code, and perhaps the phase of the moon.

    Another manifestation of this -- but one that can be regarded as a
    Very Nice Feature that as far as I understand it will disappear in C++0x
    -- is that you cannot pass a temporary std::auto_ptr to

    void foo( std::auto_ptr<Q> const& );

    because std::auto_ptr doesn't have a T(T const&) copy constructor, as
    required by the current standard.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 6, 2006
    #4
  5. Corno

    Ian Collins Guest

    Corno wrote:
    >>>Can anybody explain what the difference is between the 2

    >>
    >>In the second case you're using a temporary.
    >>
    >>
    >>>that would explain the error?

    >>
    >>Nope, for that you'll have to post a complete small program that
    >>demonstrates the problem (one possibility, though, is that ClassA's copy
    >>constructor is invoked and calls a pure virtual function).
    >>
    >>See the FAQ item "How do I post a question about code that doesn't work
    >>correctly?"

    >
    >
    > I see your point about posting the code. However I don't think code is
    > necessary in this case. The fact that the error I got was specifically a
    > 'pure virtual function call' is not that relevant to me. I trying to
    > understand how the C++ standard handles temporary variables differently from
    > named variables. You talk about the copy constructor, are temporary
    > variables always copied before the statement is executed or is there
    > anything else the standard has to say about this?
    >

    If I'm not mistaken, from your original (lost) posting, you are passing
    by value, so the first instance your object will be copied, in the
    second, it might not.

    With this type of behaviour, you realy should post the smallest snippet
    that compiles and exhibits your problem.

    --
    Ian Collins.
     
    Ian Collins, Aug 6, 2006
    #5
    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. Dario
    Replies:
    9
    Views:
    7,427
    Craig Powers
    Sep 20, 2003
  2. IK
    Replies:
    2
    Views:
    639
    hemraj
    Jul 23, 2004
  3. John Goche
    Replies:
    10
    Views:
    797
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    614
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    385
    dasjotre
    Jun 28, 2007
Loading...

Share This Page