Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using constructors for primitive types?!

Reply
Thread Tools

using constructors for primitive types?!

 
 
Gaijinco
Guest
Posts: n/a
 
      06-17-2007
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?

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-17-2007
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
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      06-17-2007
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:stream?
For outputting it would be wise to make the reference const:

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

--
Thomas
http://www.netmeister.org/news/learn2quote.html
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      06-18-2007
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;
 
Reply With Quote
 
Gaijinco
Guest
Posts: n/a
 
      06-18-2007
> 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.

 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      06-18-2007
On Jun 18, 8:20 am, Gaijinco <gaiji...@gmail.com> wrote:

> 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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-18-2007
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-18-2007
On Jun 18, 2:10 am, Ron Natalie <r...@spamcop.net> wrote:
> 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:
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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      06-18-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57