Explicit call to constrcutors

P

Prasoon

Can constructors be explicitly called in a program with using
placement new???

As far as I know....considering the code snippet

#include<iostream>

class demo
{
public:

demo(){
std::cout<<"Constructor invoked";
}

~demo(){
std::cout<<"Destructor invoked";
}
};

int main()
{
demo d;//Constructor invoked
d.demo();//Compilation error

demo();//Creation of nameless object
d.~demo();//Allowed but may lead to undefined behaviour as the
local object
//d can be destructed twice
}

Is the creation of nameless object in the above code an explicit call
to constructor or not???

According to me,its not. To call a constructor explicitly we need to
use Placement new but again it is not recommended to use "Placement
New"

Is there any other way of calling constructors explicitly????

P.S: Not to confuse with the explicit keyword

Prasoon
 
A

Alf P. Steinbach

* Prasoon:
Can constructors be explicitly called in a program with using
placement new???

The question implies some confusion about what "explicit" means.

Look up the words "explicit" and "implicit" in your nearest dictionary.

As far as I know....considering the code snippet

#include<iostream>

class demo
{
public:

demo(){
std::cout<<"Constructor invoked";
}

~demo(){
std::cout<<"Destructor invoked";
}
};

int main()
{
demo d;//Constructor invoked
d.demo();//Compilation error

demo();//Creation of nameless object
d.~demo();//Allowed but may lead to undefined behaviour as the
local object
//d can be destructed twice
}

Is the creation of nameless object in the above code an explicit call
to constructor or not???

Is it explicit? Yes. Is it a constructor call? Yes.

According to me,its not.

You can define your own terminology, of course, e.g. that when you refer to a
"car" you mean a little a flying insect.

And in some cases it's a good idea to not succumb to standardeese, e.g. in
common speak we just say "promotion" when in standardeese we'd have to say
"usual arithmetic conversion", and to top it off "promotion" means something
different, more limited and specialized, in standardeese.

But for most terms the standard's usage (and in this case also the usage of the
language's creator Bjarne Stroustrup) is all fine, it's just the usual normal
meaning!, so it's very counter-productive to adopt any other meaning.

To call a constructor explicitly we need to
use Placement new but again it is not recommended to use "Placement
New"

Placement new has nothing to do with the explicitness of your code.

You are confused about the word "explicit".

Look up the words "explicit" and "implicit" in your nearest dictionary.

Is there any other way of calling constructors explicitly????

You have an explicit call when you do it explicitly, i.e. when you supply an
argument list (which may be empty).


Cheers & hth.,

- Alf
 
K

Kai-Uwe Bux

Prasoon said:
Can constructors be explicitly called in a program with using
placement new???

The word "explicit" is not very helpful in this context.

What you can do is write expressions into your program code the evaluation
of which will cause a constructor call (BTW, this is parallel to calling a
function). Nonetheless there are differences depending on the expression
you write. E.g., with a placement new expression, calling the constructor
is the only effect of evaluation (OK: the arguments, if any, are also being
evaluated). With other expressions, you usually get more. Whether a
conforming program can tell that there is more, is a slightly different
matter, though.

As far as I know....considering the code snippet

#include<iostream>

class demo
{
public:

demo(){
std::cout<<"Constructor invoked";
}

~demo(){
std::cout<<"Destructor invoked";
}
};

int main()
{
demo d;//Constructor invoked
d.demo();//Compilation error

demo();//Creation of nameless object

The evaluation of

demo();

entails more than calling the constructor: the destructor is also called.

Nonetheless, evaluation of the expression demo() does entail invoking the
constructor (and it doesn't get more explicit than this expression, does
it?).

d.~demo();//Allowed but may lead to undefined behaviour as the
local object
//d can be destructed twice
}

Is the creation of nameless object in the above code an explicit call
to constructor or not???

The standard calls it "explicit type conversion" in [5.2.3]. This is a
little unfortunate in cases like the above where these is actually no
argument (so that it is questionable what is being converted).

According to me,its not. To call a constructor explicitly we need to
use Placement new but again it is not recommended to use "Placement
New"

Is there any other way of calling constructors explicitly????

For calling the copy constructor, there is the construct() member function
from allocator<T>. I think that C++0X extends this option to other
constructors. But the difference is purely formal: you just have another
expression whose evaluation results in a constructor call (and nothing else
a conforming program could detect). It is really not a matter of
being "explicit". What matters to writing programs is the effects that
evaluation of an expression entails.


Maybe, however, your question has some more context to it that would
illustrate what you mean by "explicit".


Best

Kai-Uwe Bux
 
J

James Kanze

The word "explicit" is not very helpful in this context.
What you can do is write expressions into your program code
the evaluation of which will cause a constructor call (BTW,
this is parallel to calling a function). Nonetheless there are
differences depending on the expression you write. E.g., with
a placement new expression, calling the constructor is the
only effect of evaluation (OK: the arguments, if any, are also
being evaluated).

Just a nit, but the placement operator new function is not
inline, and formally, at least, gets called in a placement new
expression. (And of course, "placement new" has two different
meanings, and in its broader meaning, a lot more can happen than
just calling the constructor. But I presume that the narrower
meaning is what is meant here.)
With other expressions, you usually get more. Whether a
conforming program can tell that there is more, is a slightly
different matter, though.

Any object which is created has an address. For certain types,
at least, a conformant program can probably tell that the
address doesn't refer to any other object, which means that some
sort of allocation has occured.
The evaluation of

entails more than calling the constructor: the destructor is
also called.

At the end of the full expression. Memory is also obtained
(allocated) for the object before the constructor is called, and
is freed after the destructor has been called.
Nonetheless, evaluation of the expression demo() does entail
invoking the constructor (and it doesn't get more explicit
than this expression, does it?).
The standard calls it "explicit type conversion" in [5.2.3].
This is a little unfortunate in cases like the above where
these is actually no argument (so that it is questionable what
is being converted).

Yes. I prefer the expression "explicit creation of a
temporary", but it's a bit heavy.

[...]
Maybe, however, your question has some more context to it that
would illustrate what you mean by "explicit".

That's the key. From the point of view of syntax, "demo()"
certainly looks like an explicit constructor call, even if it
does more. From the point of view of intent, I use placement
new when I explicitly want to call the constructor, and do
nothing else. But there's nothing "explicit" about the syntax.

In the end, I find it best to avoid the expression "explicit
constructor call" entirely, since it can mean different things
to different people. In some ways, it would be nice to use it
for placement new (in the narrow sense), because placement new
is also ambiguous. But this is not what most people immediately
understand by it, so I don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top