Connecting Tech Pros Worldwide Help | Site Map

using constructors for primitive types?!

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 17th, 2007, 11:25 PM
Gaijinco
Guest
 
Posts: n/a
Default using constructors for primitive types?!

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?


  #2  
Old June 17th, 2007, 11:55 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Gaijinco wrote:
Quote:
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.

Quote:
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
  #3  
Old June 17th, 2007, 11:55 PM
Thomas J. Gritzan
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Gaijinco wrote:
Quote:
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);
Quote:
>
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?
Quote:
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
  #4  
Old June 18th, 2007, 12:15 AM
Ron Natalie
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Gaijinco wrote:
Quote:
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;
  #5  
Old June 18th, 2007, 01:25 AM
Gaijinco
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Int doesn't
Quote:
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.

  #6  
Old June 18th, 2007, 01:35 AM
Sarath
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

On Jun 18, 8:20 am, Gaijinco <gaiji...@gmail.comwrote:
Quote:
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

  #7  
Old June 18th, 2007, 02:05 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Gaijinco wrote:
Quote:
Quote:
>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.
  #8  
Old June 18th, 2007, 08:05 AM
James Kanze
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

On Jun 18, 2:10 am, Ron Natalie <r...@spamcop.netwrote:
Quote:
Gaijinco wrote:
Quote:
Now I tried this and it worked:
Quote:
Quote:
cout << int(0);
Quote:
Quote:
Is this compiler-dependent or is a language feature?
Quote:
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:james.kanze@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

  #9  
Old June 18th, 2007, 11:25 AM
Ron Natalie
Guest
 
Posts: n/a
Default Re: using constructors for primitive types?!

Gaijinco wrote:
Quote:
Quote:
>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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.