473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?

Jun 17 '07 #1
8 2390
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ken | last post by:
Hi. Does the Java spec mandate set sizes for primitive types (e.g., int, float, char, etc.)? I know the ANSI C++ standard does not do this, which makes these sizes compiler dependent. So I guess...
26
by: Kip | last post by:
Greetings everyone, Is there anyone here who could point me to a page or pdf that has a list of the sizes of all of the C primitive data types on various implementations such as SPARC, x86,...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
1
by: Neil Zanella | last post by:
Hello all, In C and C++ a primitive data type is represented by a minimum number of bits as defined by the corresponding standard. For instance an int is assumed to be at least 16 bits on all...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
5
by: Abhilash.k.m | last post by:
This is regarding the session management using Out of proc session management(SQL SERVER). Among the samples below which one is better to set the session? 1. There are 20 session...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.