M
Michael P. O'Connor
it was my understanding that if you were to declare a function as virtual
you could not declare a body for it in the class it was declared virtual,
but playing around with it tonight, it let me do this. and with the
following code
class a
{
public: virtual void fun() {std::cout << "class a" << std::endl; };
};
class b : public class a
{
public: void fun() { std::cout << "class b" << std::endl; };
};
int main()
{
a aa;
b bb;
a *p;
p = &aa;
p->fun();
p = &bb;
p->fun();
return 0;
}
it printed out
class a
class b
my question is what is the benefits to this, what are the down falls to
this? I have a use for this ability but something tells me there is
something that I am overlooking and I might run into problems down the
road. So am I being paranoid?
you could not declare a body for it in the class it was declared virtual,
but playing around with it tonight, it let me do this. and with the
following code
class a
{
public: virtual void fun() {std::cout << "class a" << std::endl; };
};
class b : public class a
{
public: void fun() { std::cout << "class b" << std::endl; };
};
int main()
{
a aa;
b bb;
a *p;
p = &aa;
p->fun();
p = &bb;
p->fun();
return 0;
}
it printed out
class a
class b
my question is what is the benefits to this, what are the down falls to
this? I have a use for this ability but something tells me there is
something that I am overlooking and I might run into problems down the
road. So am I being paranoid?