Topic: Q: Creating constant pure virtual functions?
Author: bkline@cortex.nlm.nih.gov (Bob Kline Phoenix Contract)
Date: 1995/05/31 Raw View
Ralf B. Lukner (lukner@che.utexas.edu) wrote:
: Problem:
: --------------------------
: #include <iostream.h>
: class Foo1 {
: public:
: virtual int get()=0 const; // compile error
: };
[rest snipped because it works when problem above is fixed]
Do it this way:
virtual int get() const = 0; // no compile error
--
/*----------------------------------------------------------------------*/
/* Bob Kline Stream International */
/* bob_kline@stream.com formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/
Author: lukner@che.utexas.edu (Ralf B. Lukner)
Date: 1995/05/30 Raw View
Problem:
--------------------------
#include <iostream.h>
class Foo1 {
public:
virtual int get()=0 const; // compile error
};
class Foo2:public Foo1 {
public:
virtual int get() const {return i;};
private:
static int i;
};
int Foo2::i=0;
main() {
Foo2 f;
cout << f.get() << endl;
return 0;
}
$ g++ foo.C
foo.C:5: parse error before `const'
Attempt to fool compiler:
--------------------------
$ diff foo.C foo_.C
5c5
< virtual int get()=0 const;
---
> virtual int get()=0;
$ g++ foo_.C
foo_.C: In function `int main()':
foo_.C:19: cannot declare variable `f' to be of type `Foo2'
foo_.C:19: since the following virtual functions are abstract:
foo_.C:19: int Foo1::get()
Darn!
Solution:
---------------------------
{Place here}
Please respond by e-mail (at least).
Thanks,
--Ralf
Author: mikey@mcs.com
Date: 1995/05/30 Raw View
In article <lukner-3005951119540001@bingham.che.utexas.edu>,
lukner@che.utexas.edu says...
>
>Problem:
>--------------------------
>#include <iostream.h>
>
>class Foo1 {
>public:
> virtual int get()=0 const; // compile error
>};
--------------
Some very good books discuss syntax. Try:
virtual int get() const = 0;
Mike.
Author: lukner@che.utexas.edu (Ralf B. Lukner)
Date: 1995/05/30 Raw View
In article <lukner-3005951119540001@bingham.che.utexas.edu>,
lukner@che.utexas.edu (Ralf B. Lukner) wrote:
> Problem:
> virtual int get()=0 const; // compile error
> Darn!
>
> Solution:
> ---------------------------
virtual int get() const =0;
Thanks to everyone who helped....
--Ralf