Topic: Template with virtual functions ....
Author: Xing <xing@math.msu.edu>
Date: Fri, 13 Jul 2001 13:10:09 GMT Raw View
Hello,
Is the following program legal C++? Or just my compilers cannot do
the job?
Thanks!
Xing
class B {
public:
virtual void foo() const = 0;
};
template<class T>
class D: public T
{
public:
virtual void foo() { cout <<"I am a fool.\n"; }
};
int main ()
{
D<B> t;
t.foo();
return 0;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Daniel Frey <daniel.frey@aixigo.de>
Date: Fri, 13 Jul 2001 15:24:16 GMT Raw View
Xing wrote:
>=20
> class B {
> public:
> virtual void foo() const =3D 0;
> };
>=20
> template<class T>
> class D: public T
> {
> public:
> virtual void foo() { cout <<"I am a fool.\n"; }
virtual void foo() const { cout << "Now I'm enlightened.\n"; }
> };
>=20
> int main ()
> {
> D<B> t;
> t.foo();
>=20
> return 0;
> }
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Arne Adams" <Arne.Adams@bigfoot.com>
Date: Fri, 13 Jul 2001 15:24:21 GMT Raw View
"Xing" <xing@math.msu.edu> schrieb im Newsbeitrag
news:3B4E3995.7F6A6903@math.msu.edu...
> Hello,
>
> Is the following program legal C++? Or just my compilers cannot do
> the job?
>
> Thanks!
>
> Xing
>
>
> class B {
> public:
> virtual void foo() const = 0;
> };
>
> template<class T>
> class D: public T
> {
> public:
> virtual void foo() { cout <<"I am a fool.\n"; }
make it:
virtual void foo() const { cout <<"I am a fool.\n"; }
> };
>
>
> int main ()
> {
> D<B> t;
> t.foo();
>
> return 0;
> }
--
Arne
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: Fri, 13 Jul 2001 15:24:16 GMT Raw View
Hi,
"Xing" <xing@math.msu.edu> wrote in message
news:3B4E3995.7F6A6903@math.msu.edu...
> Is the following program legal C++? Or just my compilers cannot do
> the job?
>
> class B {
> public:
> virtual void foo() const = 0;
Note that the above method is declared *const*.
> };
>
> template<class T>
> class D: public T
> {
> public:
> virtual void foo() { cout <<"I am a fool.\n"; }
Note, that the above method is *not* declared as *const*.
This means that this method does not override (and implement) the virtual
method from the B class.
> };
>
> int main ()
> {
> D<B> t;
And here the compiler should choke because it cannot instantiate abstract
class.
He cannot do it, because class D has one method *without* const (defined)
and one *with* const, but unfortunately not defined.
> t.foo();
>
> return 0;
> }
You have to decide: if you want *const*, write it everywhere. If not, don't.
Interested in distributed, object-based programming?
Complete and consistent environment for Linux, Windows, ..., C, C++, ASP,
VB, ...
check: http://www.cern.ch/maciej/prog/yami
Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 13 Jul 2001 17:05:14 GMT Raw View
In article <3B4E3995.7F6A6903@math.msu.edu>, Xing <xing@math.msu.edu>
writes
>class B {
>public:
> virtual void foo() const = 0;
>};
>
>template<class T>
>class D: public T
>{
>public:
> virtual void foo() { cout <<"I am a fool.\n"; }
but that is not an overrider for the foo in B because one is a const
member function and the other is not.
>};
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]