From: sat on
why is this allowed :
class Base {
public:
virtual void f() = 0;
};
void Base::f() {
cout << "bla";
}

When this is not:

class Base {
public:
virtual void f() = 0 { cout << "bla"; }
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jeffrey Schwab on
sat wrote:
> why is this allowed :
> class Base {
> public:
> virtual void f() = 0;
> };
> void Base::f() {
> cout << "bla";
> }
>
> When this is not:
>
> class Base {
> public:
> virtual void f() = 0 { cout << "bla"; }
> }

The =0 means "No body is provided here for this function." You can't
then provide a body for the function without the compiler calling you a
liar. Is there some reason you think you want both =0 and an explicit
function body?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alex Vinokur on

Jeffrey Schwab wrote:
> sat wrote:
> > why is this allowed :
> > class Base {
> > public:
> > virtual void f() = 0;
> > };
> > void Base::f() {
> > cout << "bla";
> > }
> >
> > When this is not:
> >
> > class Base {
> > public:
> > virtual void f() = 0 { cout << "bla"; }
> > }
>
> The =0 means "No body is provided here for this function."

Not always.
For instance,

class Base
{
public:
virtual ~Base() = 0 {} // Virtual destructor must have a body
};


> You can't
> then provide a body for the function without the compiler calling you a
> liar. Is there some reason you think you want both =0 and an explicit
> function body?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jhair Tocancipa Triana on
sat writes:

> why is this allowed :
> class Base {
> public:
> virtual void f() = 0;
> };
> void Base::f() {
> cout << "bla";
> }

> When this is not:

> class Base {
> public:
> virtual void f() = 0 { cout << "bla"; }
> }

Because the ISO/IEC 14882:1998 C++ standard says:

,----[ ?10.4.2 - Abstract classes ]
| Note: a function declaration cannot provide both a pure specifier and
| a definition.
`----

--
--Jhair

PGP key available from public servers - ID: 0xBAA600D0

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Dave Smith on

"Jeffrey Schwab" <jeff(a)schwabcenter.com> wrote in message
news:L9aHf.11602$915.2203(a)southeast.rr.com...
> sat wrote:
> > why is this allowed :
> > class Base {
> > public:
> > virtual void f() = 0;
> > };
> > void Base::f() {
> > cout << "bla";
> > }
> >
> > When this is not:
> >
> > class Base {
> > public:
> > virtual void f() = 0 { cout << "bla"; }
> > }
>
> The =0 means "No body is provided here for this function." You can't
> then provide a body for the function without the compiler calling you a
> liar. Is there some reason you think you want both =0 and an explicit
> function body?

The =0 means "The derived function *must* provide a body for this
function". The compiler will catch it if you don't. However, you may
provide a "default" implementation in the base class (even with =0).
See Scott Meyers, "Effective C++" 3rd edition.

I don't know why your compiler accepts one but not the other.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]