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 

Compile-time detection of abstract base classes

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





PostPosted: Tue Jan 20, 2004 11:25 am    Post subject: Compile-time detection of abstract base classes Reply with 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?

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





PostPosted: Wed Jan 21, 2004 9:04 am    Post subject: Re: Compile-time detection of abstract base classes Reply with quote



[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





PostPosted: Wed Jan 21, 2004 9:14 am    Post subject: Re: Compile-time detection of abstract base classes Reply with quote



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





PostPosted: Wed Jan 21, 2004 9:17 am    Post subject: Re: Compile-time detection of abstract base classes Reply with quote

[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





PostPosted: Wed Jan 21, 2004 7:53 pm    Post subject: Re: Compile-time detection of abstract base classes Reply with quote

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





PostPosted: Fri Jan 23, 2004 10:00 am    Post subject: Re: Compile-time detection of abstract base classes Reply with quote

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





PostPosted: Sun Jan 25, 2004 1:32 am    Post subject: Re: Compile-time detection of abstract base classes Reply with quote

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