CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2003
    Posts
    22

    virtual functions

    gurus,
    i am trying to understand virtual functions.
    Why does the line "A s = B()" in the code below not
    compile (error C2259: 'A' : can not instiante abstract class)
    but the line A* t = new B() works perfectly ok.
    I would prefer not to use pointers as memory leaks
    tend to occur.
    I am using visual studio 2005.

    class A
    {
    public:
    virtual void func() = 0;
    };

    class B : public A
    {
    public:
    virtual void func();
    };

    void B::func()
    {

    }

    int main()
    {
    A* t = new B(); // compiles perfectly ok
    A s = B(); // error C2259
    return 1;
    }

    thank you

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: virtual functions

    Its because you are trying to instantiate an object of type A.
    A is an abstract class, so cannot be instantiated.
    your humble savant

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: virtual functions

    Quote Originally Posted by innovaltec View Post
    int main()
    {
    A* t = new B(); // compiles perfectly ok
    A s = B(); // error C2259
    return 1;
    }

    thank you
    Syntactically that line doesn't even makes sense. Aside from the fact that you can't instantiate an abstract class, what do you think that line should do?

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: virtual functions

    Quote Originally Posted by GCDEF View Post
    Syntactically that line doesn't even makes sense. Aside from the fact that you can't instantiate an abstract class, what do you think that line should do?
    Create a temporary B, construct an A from that B (if A has a copy constructor). Since B is a A, the temporary B is a valid argument to a const A&. Finally, once the A is built, the temporary B is destroyed.

    I'll grant you it's a stupid way of doing it, but it doesn't look like invalid C++ to me. Then again, you've proven me wrong more than once, so I'd be happy if you tought me something new.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: virtual functions

    Quote Originally Posted by monarch_dodra View Post
    Create a temporary B, construct an A from that B (if A has a copy constructor). Since B is a A, the temporary B is a valid argument to a const A&. Finally, once the A is built, the temporary B is destroyed.

    I'll grant you it's a stupid way of doing it, but it doesn't look like invalid C++ to me. Then again, you've proven me wrong more than once, so I'd be happy if you tought me something new.
    I was talking about this line.

    A s = B(); // error C2259

    It's invalid because A is an abstract class.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: virtual functions

    Quote Originally Posted by GCDEF View Post
    I was talking about this line.

    A s = B(); // error C2259

    It's invalid because A is an abstract class.
    I guess I read too much into your "Aside from"...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured