Topic: Address of constructor ?
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/03/01 Raw View
Mike Davies <mike_davies@noco.demon.co.uk> writes:
> Storing things is not useless if you can use them later.
^^^
Right !
> For Example : If I have a bunch of different messages coming in over a
> serial port with each message identified by a unique header byte then I
> can construct the corresponding message_objects by using the identifier
> to index into an array of constructors for the message_objects.
> At the moment I have to wrap each constructor in another function if I
> want to store it. thus :
>
> base_message make_message_1(char* data_tail)
> {
> return new(data_tail) message_1();
> }
>
> What I am trying to do is to convert the data_tail into a
> message_object.
What you want is an idiom, like the virtual ctor idiom, not
a language feature. virtual ctor as a language feature makes
*no sens*. Same for ctor pointer.
> C++ doesn't have a good way of allowing me to do this.
Before telling that C++ doesn't have a good way to do
something, unless you know everythign in C++, or have
a proof of that, add an 'IMO'.
You want to map from identifiers to types. Well, not to
types as typeinfo, but to a factory. Here is a simple
example using a function pointer:
[untested code, from the top of my head, just to show
the concept, don't use directly, but adapt to your
particular problem]
typedef BaseType* (*creator_t)(void* here, const void* from);
BaseType* create_foo (void* here, const void* from)
{
return new (here) Foo (from);
}
BaseType* create_bar (void* here, const void* from)
{
return new (here) Bar (from);
}
// I know I should have used templates and vritual
// functions, but I just want to show the basic
// concept
map<int, creator_t> types;
types[foo_id] = create_foo;
types[bar_id] = create_bar;
// read object_id and data
BaseType* object = types[object_id] (data);
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mike Davies <mike_davies@noco.demon.co.uk>
Date: 1998/02/25 Raw View
In message <34F2E920.41C67EA6@pratique.fr>, Valentin Bonnard
<bonnardv@pratique.fr> writes
>Mike Davies wrote:
>>
>> >If the original poster is still around, I'd like to ask him:
>> >
>> >If you could take the address of the constructor, what would you do with
>> >it?
>> >
>> >Mike
>> >
>>
>> I'm not the original poster, but I'd store them in arrays.
>
>Storing things is useless. What do you really want to do
>with the value ? You can't do anything with. Anything.
>It doesn't carry any information.
>
>The original poster wanted to do very low level manipulations
>(as far as I recall) by reading code which are outside the scope
>of the standard.
>
Storing things is not useless if you can use them later.
For Example : If I have a bunch of different messages coming in over a
serial port with each message identified by a unique header byte then I
can construct the corresponding message_objects by using the identifier
to index into an array of constructors for the message_objects.
At the moment I have to wrap each constructor in another function if I
want to store it. thus :
base_message make_message_1(char* data_tail)
{
return new(data_tail) message_1();
}
What I am trying to do is to convert the data_tail into a
message_object. C++ doesn't have a good way of allowing me to do this.
(Before you say it, I *know* there are problems with the vtable etc in
this approach, but I still need the functionality.)
I am currently casting the data into a struct, is there no better object
oriented method available in C++ ?
the messages are a bit stream, I know that it is dangerous to coerce
this into an object, but I cannot use rtti and streamable classes
because the message protocol has already been defined (and I've no
control over the compiler used at the sending end).
What we need IMO is placement constructors as well as placement
allocators.
--
Mike Davies
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/02/26 Raw View
Mike Davies wrote:
>
> >If the original poster is still around, I'd like to ask him:
> >
> >If you could take the address of the constructor, what would you do with
> >it?
> >
> >Mike
> >
>
> I'm not the original poster, but I'd store them in arrays.
That's not using the address, that is storing the address. What would
you use them for when you pulled them out of the array.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/02/19 Raw View
Martijn Lievaart wrote:
>
> pdemarco@ppg.com wrote in message <34eb44c5.28923970@internet.ppg.com>...
> >You don't have an instance of A yet.
>
> Why would you need an instance to take the adress of a ctor? It's like
> taking the adress of a member function (ctor is special member function).
No it isn't. The address of a member function in general carry
some bits of information. The address of the ctor carry zero
bits of information so the questions doesn't make sens. It isn't
possible to do such a thing. A ctor doesn't have a name.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/20 Raw View
Valentin Bonnard wrote in message <34EC6B09.167EB0E7@pratique.fr>...
>Martijn Lievaart wrote:
>>
>> Why would you need an instance to take the adress of a ctor? It's like
>> taking the adress of a member function (ctor is special member function).
>
>No it isn't. The address of a member function in general carry
>some bits of information. The address of the ctor carry zero
>bits of information so the questions doesn't make sens. It isn't
>possible to do such a thing. A ctor doesn't have a name.
>
I'm not sure what you mean by "carries bits of information". Could you
clarify this?
Thx in advance,
Martijn (mlievaar(at)orion(dot)nl)
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mike Schilling <mikes@forte.com>
Date: 1998/02/20 Raw View
If the original poster is still around, I'd like to ask him:
If you could take the address of the constructor, what would you do with
it?
Mike
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "C. Grant" <camg100@hermes.cam.ac.uk>
Date: 1998/02/21 Raw View
Buck wrote:
>
> Is there a way to get address of ctor ?
>
> Like ...
>
> class A
> {
> A(int i);
> // ...
> };
>
> A::A(int i)
> {
> // ...
> }
>
> int main(int argc, char **argv)
> {
> void *p =A::A; // Compiler Error !!!
> }
> ---
>
N.B. you also can't do
void (A::*p)()=&A::A;
Unfortunately A::A is not an accessible name. I think the spec is quite
right here, for what would you do if you had the pointer to a
constructor, call it?? That would be Very Bad.
-- Calum
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/02/21 Raw View
In article 167EB0E7@pratique.fr, Valentin Bonnard <bonnardv@pratique.fr> writes:
>Martijn Lievaart wrote:
>>
>> pdemarco@ppg.com wrote in message <34eb44c5.28923970@internet.ppg.com>...
>> >You don't have an instance of A yet.
>>
>> Why would you need an instance to take the adress of a ctor? It's like
>> taking the adress of a member function (ctor is special member function).
>
>No it isn't. The address of a member function in general carry
>some bits of information. The address of the ctor carry zero
>bits of information so the questions doesn't make sens. It isn't
>possible to do such a thing. A ctor doesn't have a name.
I'd like to add a little background to Valentin's explanation.
It has always been illegal in C++ to take the address of a
constructor. The ARM had an explicit rule against it (12.1
"Constructors", page 265). The reason for the rule was that
constructors are special in many ways, and typically have
hidden parameters to pass in context information. The details
are unspecified and vary among implementations. There is
no way for a programmer to provide the needed data, and so
trying to call a constructor via a pointer is unlikely to
work. Since you can't do anything useful with the address,
taking the address was made invalid.
The draft standard doesn't need the explicit rule because it has
a more elegant solution: Constructors don't have names; they
are invoked implicitly, not by direct calls. Looking up the
name T in class T will not find any constructor. Attempting
to take the address must fail because you can't refer to the
constructor directly.
---
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/02/22 Raw View
>Valentin Bonnard wrote in message <34EC6B09.167EB0E7@pratique.fr>...
>>Martijn Lievaart wrote:
>>>
>>> Why would you need an instance to take the adress of a ctor? It's like
>>> taking the adress of a member function (ctor is special member function).
>>
>>No it isn't. The address of a member function in general carry
>>some bits of information. The address of the ctor carry zero
>>bits of information so the questions doesn't make sens. It isn't
>>possible to do such a thing. A ctor doesn't have a name.
>>
>
>I'm not sure what you mean by "carries bits of information". Could you
>clarify this?
A variable is usefull only if it carries an information: a bool
hold exactly one bit, a short at least 16 bits... (well, at less
than 15 bits in theory, since -2**15-1 isn't garantied to be
representable).
A variable with only one state isn't very usefull (except for
templates, overloading, metaprogramming, ect...); for example:
enum Foo { FooUniqueValue };
Let do back to member function pointers:
struct T
{
int foo (int);
int bar (int);
int foo ();
};
int (T::*p) (int);
Here p carries exactly one bit of information (even if it
takes 64 bits in memory).
Have you heard the story of this scientist who communicated
a whole page of the bible by phone from Europe to the US
every day, and a chapter each sunday ? He didn't spent a lot
of money by doing this, because each day he just had to tell
the page number (they both had the same edition), and each
sunday he didn't even phoned: it was the same chapter each
time !
An information can be express by several ways; some takes
more space than others. The information in a function pointer
is represented in a way which always takes 64 on the compiler
<your favorite compiler>, even more on others.
But the informations this pointers carries is much smaller
than that: often a few bits, a zero for a ctor or dtor
pointer.
So a ctor or dtor pointer is useless. So we don't have them
in C++.
Is this explannation clearer ?
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mike Davies <mike_davies@noco.demon.co.uk>
Date: 1998/02/22 Raw View
>If the original poster is still around, I'd like to ask him:
>
>If you could take the address of the constructor, what would you do with
>it?
>
>Mike
>
I'm not the original poster, but I'd store them in arrays.
--
Mike Davies
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: peihao@ms3.hinet.net (Buck)
Date: 1998/02/18 Raw View
Is there a way to get address of ctor ?
Like ...
class A
{
A(int i);
// ...
};
A::A(int i)
{
// ...
}
int main(int argc, char **argv)
{
void *p =A::A; // Compiler Error !!!
}
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/18 Raw View
Buck wrote in message <34eb47e9.7932105@netnews.hinet.net>...
>Is there a way to get address of ctor ?
No. It is not allowed. But you can call a ctor by calling new with a
placement. Don't do this if you're not sure (read the ARM).
Are you really sure you need to do this?
Martijn (mlievaar(at)orion(dot)nl)
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/02/18 Raw View
Buck writes:
> Is there a way to get address of ctor ?
No, constructors don't have even names, i.e., they are not found with
name lookup. AFAIK, A::A is a class name.
BTW, there used to be a rule in CD2 and some of its successors saying
that a class name was introduced in the class itself, which implied
that A::A::A::A::A:::A was an alias to A. I can't find it any more,
was this changed? ^^^ double dots :-)
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/02/18 Raw View
Buck wrote:
>
> Is there a way to get address of ctor ?
Yes: do nothing.
The address of a ctor carry 0 bits of information, so to
take the address of a ctor is isomorphic to doing nothing.
> Like ...
What are you trying to do ?
If you just ask a language feature, and this feature doesn't
exists or is equivalent to a nop, then we can't help you. If
you ask how to do a higher level thing in comp.lang.c++.moderated,
then you'll get more helpfull answers.
Also note that you shouldn't use void* in C++ (it's bad style).
(Well, of course that's false: you can sometimes use void*,
but it means that you know what you are doing and you don't
ask questions in the newsgroups ;-)
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1998/02/18 Raw View
peihao@ms3.hinet.net (Buck) writes:
> Is there a way to get address of ctor ?
No. Why?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: pdemarco@ppg.com
Date: 1998/02/18 Raw View
You don't have an instance of A yet.
On 18 Feb 98 13:05:59 GMT, peihao@ms3.hinet.net (Buck) wrote:
>Is there a way to get address of ctor ?
>
>Like ...
>
>class A
>{
> A(int i);
> // ...
>};
>
>A::A(int i)
>{
>// ...
>}
>
>int main(int argc, char **argv)
>{
A anything;
void *p = &anything; // should be fine
// void *p = &anything; // Compiler Error !!!
>}
-- Paul
These views & opinions are my own, and I am not responsible if you use them.
Spam this! postmaster@198.105.232.67
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/19 Raw View
pdemarco@ppg.com wrote in message <34eb44c5.28923970@internet.ppg.com>...
>You don't have an instance of A yet.
>
Why would you need an instance to take the adress of a ctor? It's like
taking the adress of a member function (ctor is special member function).
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]