using constructors for primitive types?!

G

Gaijinco

I have used before:

class A
{

};

operator& operator<<(operator& out, A& a);

cout << A();

I used thinking this spent fewer memory than doing something like:

A a;
cout << a;

Now I tried this and it worked:

cout << int(0);

Is this compiler-dependent or is a language feature?
 
K

Kai-Uwe Bux

Gaijinco said:
I have used before:

class A
{

};

operator& operator<<(operator& out, A& a);

cout << A();

I used thinking this spent fewer memory than doing something like:

A a;
cout << a;

That may or may not use more memory. The compiler is free to optimize away
the variable unless it would change the observable behavior of the program.

Now I tried this and it worked:

cout << int(0);

Is this compiler-dependent or is a language feature?

It is a language feature. It is important, e.g., in templated code where it
allows one to treat built-in types and user defined classes uniformly.


Best

Kai-Uwe Bux
 
T

Thomas J. Gritzan

Gaijinco said:
I have used before:

class A
{

};

operator& operator<<(operator& out, A& a);

What is operator (the type) here? You can't have a type named so. Did you
mean std::eek:stream?
For outputting it would be wise to make the reference const:

std::eek:stream& operator<<(std::eek:stream& out, const A& a);
cout << A();

I used thinking this spent fewer memory than doing something like:

A a;
cout << a;

It would be the same, I guess. Why should it use more or less memory?
Now I tried this and it worked:

cout << int(0);

Is this compiler-dependent or is a language feature?

What is compiler-dependent?

cout can output an integer, of course. It's like:

cout << 42;
 
R

Ron Natalie

Gaijinco said:
Now I tried this and it worked:

cout << int(0);

Is this compiler-dependent or is a language feature?

That is not a call to the int constructor. Int doesn't
have a constructor. It is an explicit conversion to
int. It is (by definition in the language) exactly the
same as:
cout << (int) 0;
 
G

Gaijinco

Int doesn't
have a constructor. It is an explicit conversion to
int. It is (by definition in the language) exactly the
same as:
cout << (int) 0;

And what's the case when you use:

class A
{
int x;
A(int z): x(z){};
}

Thanks.
 
J

James Kanze

That is not a call to the int constructor. Int doesn't
have a constructor. It is an explicit conversion to
int. It is (by definition in the language) exactly the
same as:
cout << (int) 0;

That's true, but:
cout << A(0) ;
is also the same as:
cout << (A)0 ;
and
cout << static_cast< A >( 0 ) ;

They're all conversions (according to the standard). The only
real difference is that "type(arg_list)" allows any number of
args (including 0), where as the two other syntaxes only work
with exactly one argument, and that "type(arg_list)" requires
that the type be a single token or a qualified name, where as
the two other syntaxes accept more complicated type names (like
"unsigned long"). But the standard still qualifies all as "type
conversions" (and defines the semantics of A(arg) in terms of
static_cast when there is a single argument).
 
R

Ron Natalie

Gaijinco said:
And what's the case when you use:

class A
{
int x;
A(int z): x(z){};
}

Same thing. Except that classes have constructors
that are called by the implementation to initialize
them.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top