 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sachin Guest
|
Posted: Tue Sep 14, 2004 11:57 am Post subject: Pure virtual function |
|
|
in following class declaration:
class CBase
{
public:
CBase();
~CBase();
virtual int testVirtual() = 0;
};
class CDerived : public CBase
{
public:
CDerived();
~CDerived();
int testVirtual() { cout << "tested virtual" << endl; return 0; }
};
Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
Thanks,
-Sachin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Sep 14, 2004 10:51 pm Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
| Quote: | in following class declaration:
class CBase
{
public:
CBase();
~CBase();
virtual int testVirtual() = 0;
};
class CDerived : public CBase
{
public:
CDerived();
~CDerived();
int testVirtual() { cout << "tested virtual" << endl; return 0; }
};
Why we need to assign "0" to declare a pure virtual function
|
Because that's the syntax.
| Quote: | and why
not any other number (+ve/-ve)?
|
Because the Standard says so.
| Quote: | If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
It means that "=0" is the syntax requirement for declaring a virtual
function pure.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Wed Sep 15, 2004 12:38 am Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
| Quote: | Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
|
That is *not* an assignment. Unfortunate as it may be, '= 0' it's just a
syntax to mean 'pure'. Literally. It's not '= x' where x can be 0 or
something else. I guess that syntax has been chosen simply to avoid
introducing a new keyword.
| Quote: | If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
It means what it says: it must '= 0'! After the '=', only the character
'0' (possibly separated by whitespaces) is allowed, when declaring a
virtual function.
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Sep 15, 2004 12:41 am Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
<snip>
| Quote: | Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
The syntax is perhaps meant to suggest that the implementation will
put a null pointer into the class's vtable rather than a pointer to an
implementation of the function. However, the existence and structure
of vtables is an implementation detail, and most implementations
actually put in the address of a special function that aborts the
program with an error message. You could probably find a full
explanation in The Design and Evolution of C++.
--
Ben Hutchings
If at first you don't succeed, you're doing about average.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicolas Pavlidis Guest
|
Posted: Wed Sep 15, 2004 12:46 am Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
| Quote: | in following class declaration:
[snipp] |
| Quote: |
Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
You can see this another way round:
Each function has in entryadress, each mehtod has this adress too. By
declaring an pure virtual (or _abstract_) method you only want to
deklare it and you don't want to impelemnt it. So you set it to 0, or
let's say NULL, you understand?
You are seting the entry adress to 0 and nothing else.
Every Compiler would complain about any other number, because the real
entry adress of an function/method is set by the compiler, and by noone
else .
Lind regards,
Nicolas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Wed Sep 15, 2004 9:59 am Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
| Quote: | in following class declaration:
class CBase
{
public:
CBase();
~CBase();
virtual int testVirtual() = 0;
};
class CDerived : public CBase
{
public:
CDerived();
~CDerived();
int testVirtual() { cout << "tested virtual" << endl; return 0; }
};
Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
|
We don't assign anything to anything here. '= 0' is just a special
syntax used to declare pure virtual functions. It has absolutely nothing
to do with assignment.
| Quote: | If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
It means exactly what it says. According to C++ grammar, when token '='
follows a member function declaration, it can be nothing other than a
part of '= 0' sequence (pure function declaration). If the compiler sees
anything other than '0' after '=', it is an error.
--
Best regards,
Andrey Tarasevich
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Wed Sep 15, 2004 9:59 am Post subject: Re: Pure virtual function |
|
|
Sachin wrote:
| Quote: | Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
|
It's not "assignment of 0" neither "initialization with 0" but
just the syntax chosen by the language creators to indicate that
the function is *pure* virtual. A keyword like 'pure', or 'abstract'
like in Java, or 'deferred' like in Eiffel, would perhaps have been
nicer / clearer, but people are often reticent to introduce new
keywords into the language as it breaks existing programs using the
word in question as an identifier.
BTW: What would you expect "virtual int testVirtual() = -42;" to do?
Falk Tannhäuser
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zian Smith Guest
|
Posted: Wed Sep 15, 2004 10:00 am Post subject: Re: Pure virtual function |
|
|
[email]skhairnar (AT) gmail (DOT) com[/email] (Sachin) wrote in message news:<a737c856.0409140130.1dddd8e1 (AT) posting (DOT) google.com>...
| Quote: | in following class declaration:
class CBase
{
public:
CBase();
~CBase();
virtual int testVirtual() = 0;
};
class CDerived : public CBase
{
public:
CDerived();
~CDerived();
int testVirtual() { cout << "tested virtual" << endl; return 0; }
};
Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
|
It means exactly what is says.. you're using incorrect syntax when you
use any number besides 0. The defined syntax for declaring a pure
virtual function is appending =0 at the end of the function, anything
else is incorrect syntax.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Hopkin Guest
|
Posted: Wed Sep 15, 2004 6:38 pm Post subject: Re: Pure virtual function |
|
|
Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote
| Quote: | Sachin wrote:
snip
Why we need to assign "0" to declare a pure virtual function and why
not any other number (+ve/-ve)?
If i assign a value other than 0, visual studio gives an error
"illegal pure syntax, must be '= 0'", what does this mean?
The syntax is perhaps meant to suggest that the implementation will
put a null pointer into the class's vtable rather than a pointer to an
implementation of the function.
|
I remember finding the syntax rather confusing, coming to C++ from C.
Two of the many new features to learn were default parameters and pure
virtuals. You can imagine seeing them together like this looked like
nonsense:
virtual void f(int = 0) = 0;
:-)
I've been looking in the standard to find out how the calling of pure
virtual functions is worded. I must be missing something.
10.3/12 says using :: suppresses the virtual call mechanism. [1]
10.4/6 says (only) that calling a pure virtual from a constructor or
destructor is undefined. [2]
This is the problem I have: what happens when you specify a pure
virtual function using : [1] seems to say it will be non-virtual,
but [2] doesn't cover it.
I'm under the impression it's still a virtual call, although I can't
recall ever doing it (deliberately, at least).
James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Thu Sep 16, 2004 11:22 am Post subject: Re: Pure virtual function |
|
|
James Hopkin wrote:
| Quote: |
I've been looking in the standard to find out how the calling of pure
virtual functions is worded. I must be missing something.
10.3/12 says using :: suppresses the virtual call mechanism. [1]
10.4/6 says (only) that calling a pure virtual from a constructor or
destructor is undefined. [2]
This is the problem I have: what happens when you specify a pure
virtual function using : [1] seems to say it will be non-virtual,
but [2] doesn't cover it.
I'm under the impression it's still a virtual call, although I can't
recall ever doing it (deliberately, at least).
|
The call would be static because of 10.3/12. The compiler should accept
the code as legal because the pure virtual function *might* be defined
somewhere. If it's not defined, the linker will then complain about the
unresolved external.
This is consistent with 10.4/2: "A pure virtual function need be defined
only if explicitly called with the qualified-id syntax (5.1)."
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|