C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Syntax of pure virtual function definition

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sat
Guest





PostPosted: Fri Feb 10, 2006 3:06 pm    Post subject: Syntax of pure virtual function definition Reply with quote



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! ]
Back to top
Jeffrey Schwab
Guest





PostPosted: Sat Feb 11, 2006 12:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote



sat wrote:
Quote:
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! ]
Back to top
Dave Smith
Guest





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote



"Jeffrey Schwab" <jeff (AT) schwabcenter (DOT) com> wrote in message
news:L9aHf.11602$915.2203 (AT) southeast (DOT) rr.com...
Quote:
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! ]
Back to top
kwikius
Guest





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote

sat wrote:
Quote:
why is this allowed :
class Base {
public:
virtual void f() = 0;
};

Course you should be able to do:

class Base {
public:
virtual void f() = 1;
};

;-)

regards
Andy Little


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





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Hi

Jeffrey Schwab wrote:

Quote:
The =0 means "No body is provided here for this function."

A common misconception.

Quote:
You can't then provide a body for the function without the compiler
calling you a liar.

Yes you can.

Quote:
Is there some reason you think you want both =0 and an explicit
function body?

It's important with destructors (declared pure virtual), but it's useful
whenever you want to supply functionality and still force derived classes
to override the function.

Markus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jhair Tocancipa Triana
Guest





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote

sat writes:

Quote:
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! ]
Back to top
Alex Vinokur
Guest





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Jeffrey Schwab wrote:
Quote:
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
};


Quote:
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! ]
Back to top
sat
Guest





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Jeffrey Schwab wrote:

Quote:

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
"Does it ever make sense to make a function pure virtual, but still
provide a body?"
http://www.gotw.ca/gotw/031.htm


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

"Alex Vinokur" <alexvn (AT) users (DOT) sourceforge.net> writes:

| 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

My reading of the C++ grammar does not suggest that is a valid syntax.


--
Gabriel Dos Reis
gdr@integrable-solutions.net

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





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Hi

Alex Vinokur wrote:

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

And that is not allowed. You have to define the function separately.

However, I have no idea why.


Markus



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

"Dave Smith" <invalid (AT) null (DOT) com> writes:

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

Because the C++ standard says so :-)

--
Gabriel Dos Reis
gdr@integrable-solutions.net

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





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

On 11 Feb 2006 12:31:01 -0500, Dave Smith <invalid (AT) null (DOT) com> wrote:

Quote:
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.

Unless, of course, the derived class is also abstract.

A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Markus Moll <moll (AT) rbg (DOT) informatik.tu-darmstadt.de> writes:

| Hi
|
| Jeffrey Schwab wrote:
|
| > The =0 means "No body is provided here for this function."
|
| A common misconception.

Why?
My reading of the C++ stadnard suggests that if the pure-specified is
present, then the function body (if present) must be provided
somewhere else (i.e. out of the class.)

--
Gabriel Dos Reis
gdr@integrable-solutions.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jhair Tocancipa Triana
Guest





PostPosted: Sun Feb 12, 2006 11:06 am    Post subject: Re: Syntax of pure virtual function definition Reply with quote

Dave Smith writes:

Quote:
"Jeffrey Schwab" <jeff (AT) schwabcenter (DOT) com> wrote in message
news:L9aHf.11602$915.2203 (AT) southeast (DOT) 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.

No, the derived class could still define the given function as a pure
virtual function (without definition).

Quote:
However, you may provide a "default" implementation in the base
class (even with =0).

Not in a function declaration.

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

Because it is against the standard to provide the definition of a pure
virtual function in its declaration.

--
--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! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.