Topic: address of constructor (and destructor)
Author: eldiener@earthlink.net ("Edward Diener")
Date: Sun, 10 Aug 2003 00:00:25 +0000 (UTC) Raw View
root wrote:
> Why does c++ not allow taking an address of a constructor or
> destructor?
>
> I know people say it is useless but if the language wanted to support
> it, would it be technically possible? If so, how? If not why?
If there is no use for it in the language, why would you want it supported ?
You must prove a use, else adding features to the language with no good use
at all is just complicating the language unnecessarily.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: royalmacaronipenguin@yahoo.com (root)
Date: Sun, 10 Aug 2003 21:15:57 +0000 (UTC) Raw View
>
> If there is no use for it in the language, why would you want it supported ?
I do NOT want it to be supported. I just want to know why it is illegal
to do so. Of course you might say because it is useless. But I think
that's not the primary reason.
According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter 15 Class Hierarchies, Section 5 Free Store,
Subsection 2 "Virtual Constructors", page 424:
"Furthermore, a constructor is not quite an ordinary function.
In particular, it interacts with memory management routines
in ways that ordinary member functions don't.
Consequently, you cannot have a pointer to a constructor."
It doesn't sound like because it is useless that it is not allowed. BUt
can anybody elaborate what he says there?
thanks,
kn
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Tue, 12 Aug 2003 18:41:16 +0000 (UTC) Raw View
In article <bJOdna4zU53LOKuiU-KYuQ@comcast.com>, root
<royalmacaronipenguin@yahoo.com> writes
>It doesn't sound like because it is useless that it is not allowed. BUt
>can anybody elaborate what he says there?
You have it backwards. We did not sit down and write a special rule to
disallow taking the address of ctors and dtors, we just chose not to
spend time writing a rule that would allow their addresses to be taken.
The grammar of C++ requires that the declaration of a variable start
with a type name -- well just to complicate things we can start with a
type qualifier such as const but we inherited that from C (well actually
C++ invented const but chose to use the same rules as already aplied to
modifiers such as unsigned)
Supposing that we allowed pointers to members to hold the 'address' of a
ctor so we could write how would you declare a variable of that type?
Doing what is suggested in another post and writing
typedef void (apple::*PMF)(int);
PMF pmf1 = &apple::apple;
PMF pmf2 = &apple::f;
does not work because how does the compiler deal with:
void foo(PMF a_ptr_to_mbr){
now how do you use that parameter? if the argument passed is pmf1 the
call syntax and the semantics will be quite different from that for
pmf2.
So that does not work. Now why would we want to add syntax (I am not
going to kill brain cells trying to imagine what that might be) to
handle pointers to ctors when it simply does not buy us anything.
If you think differently produce a coding problem that it would solve.
--
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.jamesd.demon.co.uk/csc/faq.html ]