473,386 Members | 1,819 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 creation questiions

Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?: If not, what
is created first? the right operand or the left operand? In other
words; what does the code below do exactly?

MyClass x = MyClass();

// end

Jul 2 '06 #1
7 1645
<xl***************@gmail.comwrote:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
That constructs a local object of type MyClass and discards it.
Rather useless. Like the following program:

int main(void)
{
7; // valid statement
return 0;
}

A complete C++ program. It will compile, link, and run on nearly
any platform. But... it does... absolutely... nothing.

Same with this:

#include<iostream>
void Func(){std::cout << "Hello, world!" << std::endl;}
int main()
{
Func; // valid statement
return 0;
}

Does... absolutely... nothing. Can you see why?
Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?: If not, what
is created first? the right operand or the left operand? In other
words; what does the code below do exactly?

MyClass x = MyClass();
That's the canonical form for getting "default initialization" for
local objects. I'm not sure if a MyClass object would have all
of its members set to zero, or to values specified by the constructor,
if you just said:

MyClass x;

Perhaps others here could tell you more about this.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #2
xl***************@gmail.com wrote:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
Expression 'MyClass()' creates a value-initialized temporary object of class
type 'MyClass'. There's no way to say whether the default constructor of
'MyClass' is called or not in this case without knowing more details about the
class. The answer depends on whether the class has a _user-declared_ constructor.
Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object
The initializer expression returns a value-initialized temporary object, as
described above (and, once again, there's no way to say whether the constructor
is called or not).
and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?
No. Only the copy-constructor for x is called to copy the temporary 'MyClass()'
to 'x'. Default constructor for 'x' is not called. This sequence of steps can be
referred to as "long" initialization.

However, the compiler is allowed to optimize the code and eliminate the
temporary. I.e. the optimized version of this code would simply call default
constructor for 'x' and do nothing else. That would be the "short" initialization.

: If not, what
is created first? the right operand or the left operand?
If it decides to go the "long" way (with the temporary and copying), then the
temporary is created first, of course. The temporary is required to start
creating 'x' (as a parameter for 'x's copy constructor), so it has to be created
before.
In other
words; what does the code below do exactly?

MyClass x = MyClass();
See above. What it will do exactly depends on which optimizations the compiler
will decide to perform" go the "long" way or the "short" way.

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #3
Andrey Tarasevich wrote:
xl***************@gmail.com wrote:
>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.
No, it doesn't. This always calls the default constructor of MyClass. If
there is no user-defined default constructor, the compiler generates one
for you. If you only declare and not define the default constructor, you'll
get a compile error.
>Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object

The initializer expression returns a value-initialized temporary object,
as described above (and, once again, there's no way to say whether the
constructor is called or not).
Whenever an object of a class type is created, a constructor of that class
is called.

Jul 2 '06 #4
Rolf Magnus wrote:
Andrey Tarasevich wrote:
>xl***************@gmail.com wrote:
>>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.

No, it doesn't. This always calls the default constructor of MyClass. If
there is no user-defined default constructor, the compiler generates one
for you. If you only declare and not define the default constructor, you'll
get a compile error.
I'm sure you know this, but to be very clear, the compiler generates a
default constructor only if there is no user-defined constructor of any
type (default or not).

Mark
Jul 2 '06 #5
posted:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

class MyClass {
private:

int i;

public:

MyClass() : i(-1) {}

};

int main()
{
MyClass thing1;

/* Creates a local object by invoking
its default constructor. (NB: If this
were a POD, then it would contain
white noise. */
MyClass thing2();

/* Declares a function called "thing2"
which returns a MyClass by value,
and which doesn't take any parameters. */
MyClass thing3(void);

/* Identical to the previous declaration. */
MyClass();

/* Creates a non-const, R-value, name-less
object of type "MyClass" which will be
destroyed at the end of the statement. */
}
--

Frederick Gotham
Jul 2 '06 #6
Robbie Hatley wrote:
Rather useless. Like the following program:

int main(void)
{
7; // valid statement
return 0;
}

A complete C++ program. It will compile, link, and run on nearly
any platform. But... it does... absolutely... nothing.
It returns without error 100% of the time -- that ain't nothin'.

Luke

Jul 2 '06 #7
Rolf Magnus wrote:
Andrey Tarasevich wrote:
>xl***************@gmail.com wrote:
>>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.

No, it doesn't. This always calls the default constructor of MyClass.
No. That's incorrect. In post-TC1 language the '()' initializer will cause a
call to the constructor if and only if that constructor is user-declared (and
defined, of course). Otherwise, the value-initialization is performed _without_
the use of the top-level constructor.

For example, consider the following class

struct S { int x, y; };

This class has no user-declared constructor, which means that the compiler will
provide one implicitly. However, the compiler-provided constructor will
essentially do nothing (as specified in the standard). It will not initialize
S::x' and 'S::y' to any particular values.

Now consider the expression 'S()'. This expression is guaranteed to produce an
'S' object with zero-initialized 'S::x' and 'S::y'. What do you think does the
initialization? The default constructor of 'S'? No. The default constructor of
'S' does nothing of that nature. What happens in this case is an independent
initialization process that doesn't involve the constructor of 'S'.

That, BTW, how it was in pre-TC1 standard as well. Except that in pre-TC1 one
the line between "constructor called" and "_constructorless_ default
initialization performed" was drawn between non-POD and POD types. TC1 changed
that by introducing value-initialization and moving the line to the "has
user-declared constructor" and "has no user-declared constructor" position.

Read the description of '()' initialization in either version of the standard.
It is easy to see that the default constructor is not called for POD class
types, for example.

Moreover, for POD class types the existence of implicit default constructor is
postulated for consistency only. Even at the conceptual level, that constructor
is never called at all. Expression 'S()' does not call it. Declaration

S s;

still does not call the constructor (see 8.5/9), even though the result
(indeterminate values) is consistent with the constructor's behavior.
If
there is no user-defined default constructor, the compiler generates one
for you.
Yes. But that does not mean that it will be used in '()' initialization.

If you only declare and not define the default constructor, you'll
get a compile error.
That's understood, but that's not what I'm talking about.
...
Whenever an object of a class type is created, a constructor of that class
is called.
...
The above is true only for classes with user-declared constructors. But in
general case - no. Not in C++ language.

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #8

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
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:
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
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
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
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.