Constructor call

C

cppquester

What does this code do?

#include <iostream>

class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};

int main( int argc, char* argv[])
{
A(); // constructor call?

return 0;
}

I.e.
a) What does this constructor call do?
b) could this be used for somehing which makes sense?
c) if not why is it allowed?

Thanks,
Marc
 
M

Markus Moll

Hi
What does this code do?

#include <iostream>

class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};

int main( int argc, char* argv[])
{
A(); // constructor call?

return 0;
}

I.e.
a) What does this constructor call do?

It's not really a constructor call, but the creation of an unnamed
temporary. An object of type A is created, not at all used, and then
destroyed.
b) could this be used for somehing which makes sense?

What do you mean? The above example? That doesn't make sense. Creating
unnamed temporaries like this in general is useful, consider:

void func(const A&);
func(A()); // Call func with a "default" A

Otherwise you would have to give that A a name:

void func(const A&);
A a;
func(a);
c) if not why is it allowed?

Why not? You could also write:

int main()
{
5+5;
}

But that doesn't make any sense either. You can always write completely
useless programs (at least in every sensible programming language).

Markus
 
A

Alf P. Steinbach

* Markus Moll:
Hi

What does this code do?

#include <iostream>

class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};

int main( int argc, char* argv[])
{
A(); // constructor call?

return 0;
}

I.e.
a) What does this constructor call do?

It's not really a constructor call,

Depends on whose terminology you adopt.

In the terminology used in the standard, and by the language's creator
Bjarne Stroustrup[1] and people like Andrew Koenig and Nicolai Josuttis,
it's an explicit constructor call.

It is however a popular sport among some contributors to clc++, which
group has even included some competent folks, to deny that that can make
sense. Happily the numbers have dwindled. I'm using the authority
argument above because that's the only one that's worked with them.

but the creation of an unnamed
temporary. An object of type A is created, not at all used, and then
destroyed.

Yes.

Cheers, & hth.,

- Alf


Notes:
[1] <url:
http://www.google.com/search?q=stroustrup+koenig+"explicit+constructor+call">
 
M

Markus Moll

Alf said:
* Markus Moll:
It's not really a constructor call,

Depends on whose terminology you adopt.

In the terminology used in the standard, and by the language's creator
Bjarne Stroustrup[1] and people like Andrew Koenig and Nicolai Josuttis,
it's an explicit constructor call.

Um... okay, I wasn't aware of that (although this means I must have read it
quite often). To me, "constructor call" did not make as much sense, because
it sounds like an invocation of the constructor is all that happens (which
is of course not true). But good to know, I will refrain from "correcting"
it in the future.

Markus
 
J

Jim Langston

What does this code do?

#include <iostream>

class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};

int main( int argc, char* argv[])
{
A(); // constructor call?

return 0;
}

I.e.
a) What does this constructor call do?

Creates a temporary A instance.
b) could this be used for somehing which makes sense?

It could, but probably isn't. I could think of a scenario where someone
might have a class constructor that does something useful but they wouldn't
need to keep an instance around. I'm not sure if that would be good code or
not though.
c) if not why is it allowed?

The same reason:

1;

is allowed.
 
C

Chris ( Val )

What does this code do?
#include <iostream>
class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};
int main( int argc, char* argv[])
{
A(); // constructor call?
return 0;
}
I.e.
a) What does this constructor call do?

Creates a temporary A instance.
b) could this be used for somehing which makes sense?

It could, but probably isn't. I could think of a scenario where someone
might have a class constructor that does something useful but they wouldn't
need to keep an instance around. I'm not sure if that would be good code or
not though.

There is nothing wrong with working with temporary objects. As I see
it, there can be many circumstances where it doesn't make much sense
to instantiate an explicit object.
 
P

Puppet_Sock

Creates a temporary A instance.


It could, but probably isn't. I could think of a scenario where someone
might have a class constructor that does something useful but they wouldn't
need to keep an instance around. I'm not sure if that would be good code or
not though.

Probably most of the "good" reasons for doing such
are outside the standard language. For example, if
you were controlling some hardware, you might want
that temporary to do some prep work for you. Like,
start up a motor or some such, and not return until
the system gives the "motor at operating speed"
message. That's a natural for a ctor in a temp.

Probably most cases in the standard language are
at least as easy and straightforward done in other
ways.
Socks
 
J

Joe Greer

Probably most of the "good" reasons for doing such
are outside the standard language. For example, if
you were controlling some hardware, you might want
that temporary to do some prep work for you. Like,
start up a motor or some such, and not return until
the system gives the "motor at operating speed"
message. That's a natural for a ctor in a temp.

Probably most cases in the standard language are
at least as easy and straightforward done in other
ways.
Socks

It's hard to imagine a scenario where those aren't just functions in a
namespace somewhere. Exactly why is it that you would want to create an
object for that?

joe
 
J

James Kanze

What does this code do?
#include <iostream>
class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};
int main( int argc, char* argv[])
{
A(); // constructor call?
return 0;
}
I.e.
a) What does this constructor call do?

It outputs "A::A()\n" to standard out.
b) could this be used for somehing which makes sense?

Hard to say. I've never used this exactly, but similar
constructs do sometimes make sense.
c) if not why is it allowed?

What would you propose banning?
 
J

James Kanze

* Markus Moll:
(e-mail address removed) wrote:
What does this code do?
#include <iostream>
class A
{
public:
A() { std::cout << "A::A()" << std::endl;}
};
int main( int argc, char* argv[])
{
A(); // constructor call?
return 0;
}
I.e.
a) What does this constructor call do?
It's not really a constructor call,
Depends on whose terminology you adopt.
In the terminology used in the standard, and by the language's
creator Bjarne Stroustrup[1] and people like Andrew Koenig and
Nicolai Josuttis, it's an explicit constructor call.

According to the standard, it's a "Explicit type conversion
(functional notation)". The semantics of this expression are
specified to be "creates an rvalue of the specified type, which
is value-initialized".

Of course, informally, I'll call it a constructor call too (and
formally, I find it awkward to speak of an explicit type
conversion when there's nothing being converted). But for
whatever reasons, that's not the language the standard uses in
its formal definition of the construction.
It is however a popular sport among some contributors to
clc++, which group has even included some competent folks, to
deny that that can make sense. Happily the numbers have
dwindled. I'm using the authority argument above because
that's the only one that's worked with them.

The authority argument is IMHO the only one which can be made to
work against what you are saying:). Calling it an explicit
type conversion doesn't make any sense. But that's what the
standard does.

The only explination I can think of as to why the standard uses
such strange terminology is that 1) it doesn't want to talk
about constructors for things like int (and of course, "int()"
is another example of this type of expression), or 2) it doesn't
want to treat the case with a single argument (i.e. "A(b)") as a
special case (and of course, that case may call a user defined
conversion operator on b, rather than the constructor of A).

Of course, it is more than *just* a constructor call---the
compiler also allocates memory for the object.
 

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
473,981
Messages
2,570,187
Members
46,729
Latest member
ScarlettJe

Latest Threads

Top