Afficher un message
Vieux 18/01/2008, 19h34   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function pointer to constructor

On Jan 18, 3:11 pm, jkherci...@gmx.net wrote:
> James Kanze wrote:
> > On Jan 18, 1:44 pm, jkherci...@gmx.net wrote:
> >> Morten Lemvigh wrote:
> >> > Is it possible to pass a pointer to a constructor or a class definition
> >> > as argument to a function? Maybe in a way similar to passing function
> >> > pointers...?


> >> Not directly. The standard somewhat ominously says that
> >> constructors have no names.


> > More to the point, there is no way in the standard of "calling a
> > constructor", period.


> There is also no way of "calling a function" either. All that
> there is, according to the standard, is that a function call
> expression is evaluated and in the course of the execution of
> that evaluation, a function will be called (but sometimes more
> happens: in the case of virtual functions, there might even be
> some mechanism involved behind the scenes to decide which
> one).


And your point is? The standard speaks of a function call
operator, and a function call expression. The expression
evaluates the arguments and calls the function. (Which function
is called, of course, depends on the expression, and a number of
other things.)

The standard doesn't have a constructor call expression. It
speaks of calling the constructor in the context of other
operations, such as type conversions, declarations or a new
expression. And all of these other operations have additional
semantics associated with them, systematically.

> Similarly, there are expressions whose evaluation involves the
> call of constructors (and usually much more). In fact, there
> are several.


> Despite this, I take the linguistic license to say that I call
> a function. Also, I don't see a reason not to take the same
> linguistic license with constructor calls.


Why not? The standard doesn't. I don't either, because I
think precision of expression is important when talking about
something as precise as a computer language.

Of course, if you don't care about being understood, you can use
any terminology you want.

> But, actually, this terminological issue is not "more to the
> point" of the OP.


> > All of the contexts where a constructor is called also
> > involve allocating memory for the object. If you try to
> > call the constructor through a pointer to a function, how
> > would the compiler know where (stack?, dynamically?) or how
> > much memory to allocate?


> This, on the other hand, is. However:


> > (I suppose one could claim that placement new just calls a
> > constructor. But from the language point of view, it's a new
> > expression, which calls an operator new() function to get the
> > necessary memory.)


> Well, C++ just provides a very very roundabout way to call a
> constructor on a given region in memory (object): you have to
> go through a new-expression with the of a library
> provided new() function that just returns its argument and
> does no allocation of its own. Given all those provisions in
> place, evaluation of a placement new expression boils down to
> nothing more than calling a constructor. (Very much like some
> function call expressions boil down to nothing more than
> calling a function:-)


Except that C++ also makes a distinction between library and
language---as far as the language is concerned, it is just
another case of an new expression.

> In any case, we need to know more from the OP. The creator
> objects I suggestest, will not use placement new but allocate
> memory and return pointers to the constructed objects. If the
> OP has different requirements, he would need to tell us.


He asked why you can't take a pointer to a constructor, like you
could a pointer to any other function. One of the reasons, of
course (not the only one), is that it makes no sense. The only
thing you can do with a pointer to a function is call the
function. Since a constructor can't be called without some
additional context, there's no practical way to implement
through a pointer to a function.

One could imagine a "pointer to a constructor", which, when
called, would also allocate memory in some way. C++ doesn't
have such, however, and if it did, it wouldn't be compatible
with a pointer to a function.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
 
Page generated in 0,20287 seconds with 9 queries