object of type int

C

ChasW

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
 
T

Tomás

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top