473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Constructor call

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

Oct 2 '07 #1
7 1624
Hi

cp********@googlemail.com 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, 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

Oct 2 '07 #2
Alf P. Steinbach wrote:
* 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

Oct 2 '07 #3
<cp********@googlemail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
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.
Oct 2 '07 #4
On Oct 2, 11:10 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
[snips]
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.
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

Oct 2 '07 #5
Puppet_Sock <pu*********@hotmail.comwrote in
news:11*********************@57g2000hsv.googlegrou ps.com:
On Oct 2, 11:10 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
[snips]
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.

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
Oct 2 '07 #6
On Oct 2, 11:01 am, cppques...@googlemail.com 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 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?

--
James Kanze (GABI Software) email:ja*********@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

Oct 3 '07 #7
On Oct 2, 11:58 am, "Alf P. Steinbach" <al...@start.nowrote:
* Markus Moll:
cppques...@googlemail.com 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.

--
James Kanze (GABI Software) email:ja*********@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

Oct 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
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...? The function should construct a number...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.