|
-
November 3rd, 2010, 09:26 AM
#1
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
-
November 3rd, 2010, 09:34 AM
#2
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
-
November 3rd, 2010, 09:39 AM
#3
Re: virtual functions
 Originally Posted by innovaltec
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?
-
November 3rd, 2010, 10:18 AM
#4
Re: virtual functions
 Originally Posted by GCDEF
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.
-
November 3rd, 2010, 10:22 AM
#5
Re: virtual functions
 Originally Posted by monarch_dodra
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.
-
November 3rd, 2010, 10:45 AM
#6
Re: virtual functions
 Originally Posted by GCDEF
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|