473,386 Members | 1,810 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,386 software developers and data experts.

object of type int

Im looking for some technical clarification regarding exactly what the
differences, if any, are between the two pieces of code are, aside
from the obvious syntactical differences.

int x = 5;

int x(5);

First off, are they expected to be compiled into identical or
different machine code?

The later seems to take the form "object x of type int is being
constructed with a parameter of 5"

The first snippet can also considered a form of default construction
with regards to instantiating objects of class types for
single-parameter constructors.

However since the first snippet is also valid C style syntax and the
second snippetis not, it seems less clear in the first snippet whether
or not an object is actually being constructed.

Refering to 3.9.1 section 3 of the standard, the phrase "pointer to an
object of type int" seems to validate the notion of and int being an
object.

So, to what end exactly are native types such as int and double
objects?

What differences, for example, are there between C and C++ with
regards to "int" and its seemingly apparent construction in C++?

Similarly, consider the following:

int x = int( y * z );

Perhaps asking this question is premature without knowing the answers
to what I have already asked, but aside from a possible conversion
depending on what types y and z are, is there not also construction of
an object of type int taking place on the RHS?

Regards,
Charles
Jun 2 '06 #1
1 1856
ChasW posted:

int x = 5;

int x(5);

When working with an intrinsic type such as "int", the two forms are
identical.

When dealing with a fancy user-defined class type, the situation is
different (but I'll get to that further down).

So, to what end exactly are native types such as int and double
objects?

You're mixing up terminology.
"int" is a type.
"double" is a type.
"an object" is simply another term for "a variable". However, we only
tend to use "a variable" when we're dealing with something simple like an
int or a double. "object" is used to describe anything, be it an int, a
double, an std::string, or an std::vector<int>.
Here's how you define an object in C++:
TypeName object_name;
Here's some examples:

int i;

char k;

short b;

double j;

std::string str;

std::vector<int> vec;

bool n;
As I said, the two forms of initialisation are identical if you're
dealing with an intrinsic type:

int i = 5; int i(5);

double k(42.234); double k = 42.234;
If you're dealing with a class which has a constructor however, e.g.:

class SomeClass {
public:
SomeClass(int k) {}

};
Then:

SomeClass object = 5;
is interpreted as if it were written as:

SomeClass object = SomeClass(5);
As you can see, a nameless temporary is created on the right hand side,
and then "object" is copy-constructed from this nameless temporary.
However, a compiler won't bother making a temporary, it will just pass 5
to object's constructor. The caveat though is that even if no temporary
is created, the class must still have a public copy-constructor. Consider
the following:

class SomeClass {
private:

SomeClass( const SomeClass &original );
/* Can't copy-construct */

public:

SomeClass(int) {}

};
int main()
{
SomeClass obj1(5); /* No problem */

SomeClass obj2 = 5; /* Won't compile */
}
I myself advocate the following:

Use the "equals" form when dealing with an intrinsic type.
Use the "brackets" form when dealing with a fancy class.
-Tomás
Jun 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Avi Kak | last post by:
Hello: Please forgive me if my question is too silly or just not well-formed. Wesley Chun in his book (Core Python Programming) says that **everything** in Python is an object. So I became...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.