473,804 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1645
Hi

cp********@goog lemail.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, "constructo r 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********@goo glemail.comwrot e in message
news:11******** **************@ 57g2000hsv.goog legroups.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...@rock etmail.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*********@ho tmail.comwrote in
news:11******** *************@5 7g2000hsv.googl egroups.com:
On Oct 2, 11:10 am, "Jim Langston" <tazmas...@rock etmail.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...@goog lemail.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 objektorientier ter Datenverarbeitu ng
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.no wrote:
* Markus Moll:
cppques...@goog lemail.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 objektorientier ter Datenverarbeitu ng
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
3116
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 this in any way used to create singletons. Can someone say how? Cheers, Andy
23
5186
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
2985
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
24
3787
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
6370
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
13
9732
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
74
16040
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
12
7220
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? class Trial { public: Trial() {
9
23777
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 of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
3
2448
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9134
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.