| View previous topic :: View next topic |
| Author |
Message |
J Lindrud Guest
|
Posted: Tue Jan 20, 2004 11:25 am Post subject: Compile-time detection of abstract base classes |
|
|
Hi!
If I have a type T& and I want to dereference it and create an object
on the stack, ie "T t();", I run into problems when T happens to be an
abstract base class ("error: can't instantiate abstract base class").
So in order to avoid this error, I'm wondering if there is a way of
determining at compile time whether a class is an abstract base class
or not?
Jarl.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Wed Jan 21, 2004 9:04 am Post subject: Re: Compile-time detection of abstract base classes |
|
|
[email]jlindrud (AT) hotmail (DOT) com[/email] (J Lindrud) wrote in message
| Quote: |
If I have a type T& and I want to dereference it and create an object
on the stack, ie "T t();", I run into problems when T happens to be an
abstract base class ("error: can't instantiate abstract base class").
So in order to avoid this error, I'm wondering if there is a way of
determining at compile time whether a class is an abstract base class
or not?
Yes. |
http://groups.google.com/groups?group=comp.lang.c%2B%2B.moderated&selm=df893da6.0207110613.75b2fe90%40posting.google.com
Yoy will need GCC 3.4, VC 7.1, Intel C++ 7 or Comeau.
is_abstract<> type traits will be available in next Boost release too.
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Wed Jan 21, 2004 9:14 am Post subject: Re: Compile-time detection of abstract base classes |
|
|
Hi!
J Lindrud wrote:
| Quote: | ("error: can't instantiate abstract base class").
So in order to avoid this error, I'm wondering if there is a way of
determining at compile time whether a class is an abstract base class
or not?
|
This error already occurs at compile-time, doesn't it?
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Wed Jan 21, 2004 9:17 am Post subject: Re: Compile-time detection of abstract base classes |
|
|
[email]jlindrud (AT) hotmail (DOT) com[/email] (J Lindrud) writes:
| Quote: | Hi!
If I have a type T& and I want to dereference it and create an object
on the stack, ie "T t();", I run into problems when T happens to be an
abstract base class ("error: can't instantiate abstract base class").
So in order to avoid this error, I'm wondering if there is a way of
determining at compile time whether a class is an abstract base class
or not?
|
An is_abstract trait was recently submitted to Boost:
http://tinyurl.com/3xqp2
I believe it is in the CVS now.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Wed Jan 21, 2004 7:53 pm Post subject: Re: Compile-time detection of abstract base classes |
|
|
J Lindrud wrote:
| Quote: | create an object on the stack, ie "T t();"
|
T t();
declares a function named 't' with no parameters
returning a 'T'.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Guest
|
Posted: Fri Jan 23, 2004 10:00 am Post subject: Re: Compile-time detection of abstract base classes |
|
|
Given a object oriented style of programming T& probably will be a reference
to an abstract base.
Give each class in your hierarchy a member function "virtual T clone()" to
construct a copy of the class given only a reference or pointer to its
abstract base. This is the "virtual constructor" idiom. It described in
many C++ books. I know Scott Myers as a chapter on it. Everyone should
have all of his C++ books.
Joe
"J Lindrud" <jlindrud (AT) hotmail (DOT) com> wrote
| Quote: | Hi!
If I have a type T& and I want to dereference it and create an object
on the stack, ie "T t();", I run into problems when T happens to be an
abstract base class ("error: can't instantiate abstract base class").
So in order to avoid this error, I'm wondering if there is a way of
determining at compile time whether a class is an abstract base class
or not?
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
J Lindrud Guest
|
Posted: Sun Jan 25, 2004 1:32 am Post subject: Re: Compile-time detection of abstract base classes |
|
|
| Quote: |
Give each class in your hierarchy a member function "virtual T clone()" to
construct a copy of the class given only a reference or pointer to its
|
Only problem is that this is part of a serialization framework, that
needs to work as-is with user-supplied classes, and it wouldn't be
practical to require that the user always provide and properly
implement a clone method.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|