By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

using constructors for primitive types?!

P: n/a
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?

Jun 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Gaijinco wrote:
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
Jun 17 '07 #2

P: n/a
Gaijinco wrote:
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::ostream?
For outputting it would be wise to make the reference const:

std::ostream& operator<<(std::ostream& 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;

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 17 '07 #3

P: n/a
Gaijinco wrote:
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;
Jun 18 '07 #4

P: n/a
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.

Jun 18 '07 #5

P: n/a
On Jun 18, 8:20 am, Gaijinco <gaiji...@gmail.comwrote:
cout << A();
You should also take care the following while using paranthesis with
defualt contructor. Moreover it's a temporary object.

http://www.parashift.com/c++-faq-lit....html#faq-10.2

Jun 18 '07 #6

P: n/a
Gaijinco wrote:
>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){};
That's an initialiser, different beast.

--
Ian Collins.
Jun 18 '07 #7

P: n/a
On Jun 18, 2:10 am, Ron Natalie <r...@spamcop.netwrote:
Gaijinco wrote:
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;
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).

--
James Kanze (GABI Software, from CAI) 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

Jun 18 '07 #8

P: n/a
Gaijinco wrote:
>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){};
}
Same thing. Except that classes have constructors
that are called by the implementation to initialize
them.
Jun 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.