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

Instansiating an object

Hello!

Assume I have a class named Student and I instansiate an object called kasia
in this way.
Student kasia(100);

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.

//Tony
Jul 23 '05 #1
15 1523
in Student kasia(100); a constructor with one argument (may be int)
will be called.
in Student kasia = Student(100); the copy constructor is invoked. If
you have not defined your own, then compiler generates it by default
which will make a copy of values of all data members in the target.
One should normally prefer the former way.

-- Regards
Shivank

Jul 23 '05 #2

Tony Johansson wrote:
Hello!

Assume I have a class named Student and I instansiate an object called kasia in this way.
Student kasia(100);
This will work only if you have a parameterised constructor which
accepts an integer as a parameter. Soo, in this case the class decl.
should have :

Student(int);

as one of the constructors, considering you do not have any default
argument constructor.

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.
No these two are not same. Actually this will not call the copy
constructor because you need to pass a copy of an existing object. If
it were, say:
Student kasia(100); // calls 1-argument constructor
Student Tony(kaisa); // calls the copy constructor

then instantiating Tony would have called a copy constructor.

However, In you case if the call has to go through it would require
something like:
Student(Student );

in your class decl. But if you do that, the compiler (gcc 3.2.2)would
complain saying that you probably wanted a copy constructor, so you
should have had :

Student(const Student &)

as the decl..

So, bottom line is your first call is fine and it makes a call to a
1-argument constructor to instantiate your Student object. Your second
call is not correct and it would NOT call a copy constructor.

Hope that makes some sense..
//Tony


Jul 23 '05 #3
Tony Johansson wrote:
Hello!

Assume I have a class named Student and I instansiate an object called
kasia in this way.
You mean you instantiate the class, not the object. Creating an instance of
the class (i.e. an object) means to instantiate that class.
Student kasia(100);

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);


The first one will create kasia as an instance of Student and initialize it
with 100.
The second one first creates a nameless temporary Student instance and
initializes it with 100. Then, kasia is initialized from that temporary by
the copy constructor. Then the temporary is destroyed. Many modern
compilers will optimize the temporary away, and the C++ standard explicitly
allows that, but even then, a copy constructor must be accessible.

Jul 23 '05 #4
Tony Johansson wrote:
Assume I have a class named Student and I instansiate an object called kasia in this way.
Student kasia(100);
This is C++.
What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);
This is Java (almost, you forgot the 'new').
I assume these two are equivalent. But I always use former way.


I wouldn't program Java in C++. ;-)

R.C.

Jul 23 '05 #5
Rolf Magnus wrote:
The second one first creates a nameless temporary Student instance and
initializes it with 100. Then, kasia is initialized from that temporary by
the copy constructor. Then the temporary is destroyed. Many modern
compilers will optimize the temporary away, and the C++ standard explicitly ^^
I could not find out the relavent C&V from standard. :(
Anyone kind enough?

Thanks
Krishanu
allows that, but even then, a copy constructor must be accessible.

Jul 23 '05 #6
Rapscallion wrote:

Tony Johansson wrote:
Assume I have a class named Student and I instansiate an object

called kasia
in this way.
Student kasia(100);


This is C++.
What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);


This is Java (almost, you forgot the 'new').


This is C++ too.
Please don't confuse people.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7
Jaspreet wrote:
What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.
No these two are not same. Actually this will not call the copy
constructor because you need to pass a copy of an existing object.


What do you mean by "a copy of an existing object"? You need to pass an
existing object, and Student(100) creates such an object.
However, In you case if the call has to go through it would require
something like:
Student(Student );
No, it wouldn't. Such a constructor would be nonsense anyway, since it would
create an endless loop.
in your class decl. But if you do that, the compiler (gcc 3.2.2)would
complain saying that you probably wanted a copy constructor, so you
should have had :

Student(const Student &)

as the decl..
Yes, or just let the compiler generate one.
So, bottom line is your first call is fine and it makes a call to a
1-argument constructor to instantiate your Student object. Your second
call is not correct and it would NOT call a copy constructor.
Where did you get that from?
Hope that makes some sense..


It doesn't.

Jul 23 '05 #8
Krishanu Debnath wrote:
Rolf Magnus wrote:
The second one first creates a nameless temporary Student instance and
initializes it with 100. Then, kasia is initialized from that temporary
by the copy constructor. Then the temporary is destroyed. Many modern
compilers will optimize the temporary away, and the C++ standard
explicitly

^^
I could not find out the relavent C&V from standard. :(
Anyone kind enough?


8.5 Initializers

14
[...] The function selected is called with the initializer expression as its
argument; if the function is a constructor, the call initializes a temporary
of the destination type. The result of the call (which is the temporary for
the constructor case) is then used to direct-initialize, according to the
rules above, the object that is the destination of the copy-initialization.
In certain cases, an implementation is permitted to eliminate the copying
inherent in this direct-initialization by constructing the intermediate
result directly into the object being initialized; see 12.2, 12.8.

12.8 Copying class objects

15
Whenever a temporary class object is copied using a copy constructor, and
this object and the copy have the same cv-unqualified type, an
implementation is permitted to treat the original and the copy as two
different ways of referring to the same object and not perform a copy at
all, even if the class copy constructor or destructor have side effects.
[...]

Jul 23 '05 #9

Rolf Magnus wrote:
Jaspreet wrote:
What the difference if I instead instansiate this kasia in this way. Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.
No these two are not same. Actually this will not call the copy
constructor because you need to pass a copy of an existing object.


What do you mean by "a copy of an existing object"? You need to pass

an existing object, and Student(100) creates such an object.
Oops slip of the keyboard.
However, In you case if the call has to go through it would require
something like:
Student(Student );
No, it wouldn't. Such a constructor would be nonsense anyway, since

it would create an endless loop. Thats why I said the compiler will complain.
in your class decl. But if you do that, the compiler (gcc 3.2.2)would complain saying that you probably wanted a copy constructor, so you
should have had :

Student(const Student &)

as the decl..
Yes, or just let the compiler generate one.

Yes thats true..
So, bottom line is your first call is fine and it makes a call to a
1-argument constructor to instantiate your Student object. Your second call is not correct and it would NOT call a copy constructor.
Where did you get that from?

Try having a copy constructor in your program and make the second call.
It will not call the copy constructor. Try compiling the following
program.

#include <iostream>

using namespace std;

class stud
{
public:
stud (stud &s)
{
cout<<endl<<"copy";
}
stud(int a)
{
cout<<endl<<"1-arg constructore";
}
};
int main()
{
stud s=stud (5);
return (EXIT_SUCCESS);
}
Hope that makes some sense..


It doesn't.

Try compiling the above program or even try:

#include <iostream>

using namepsace std;

class stud
{
};

int main()
{
stud s=stud(5);
return (EXIT_SUCCESS);
}

I got the following errors: (gcc 3.2.2)
obj1.cc: In function `int main()':
obj1.cc:11: no matching function for call to `stud::stud(int)'
obj1.cc:6: candidates are: stud::stud()
obj1.cc:6: stud::stud(const stud&)

Not sure how, the call "stud s=stud(5);" would call a copy constructor.
It should not. Let me know if I am wrong..

It might make some sense!!!

Jul 23 '05 #10
Jaspreet wrote:
So, bottom line is your first call is fine and it makes a call to a
1-argument constructor to instantiate your Student object. Your
second call is not correct and it would NOT call a copy constructor.
Where did you get that from?

Try having a copy constructor in your program and make the second call.
It will not call the copy constructor.


Right. It will try to, but it can't because your copy constructor takes a
non-const reference, and C++ forbids binding a temporary to a non-const
reference.
Try compiling the following program.

#include <iostream>

using namespace std;

class stud
{
public:
stud (stud &s)
{
cout<<endl<<"copy";
}
stud(int a)
{
cout<<endl<<"1-arg constructore";
}
};
int main()
{
stud s=stud (5);
return (EXIT_SUCCESS);
}
> Hope that makes some sense..
It doesn't.

Try compiling the above program or even try:

#include <iostream>

using namepsace std;

class stud
{
};

int main()
{
stud s=stud(5);
return (EXIT_SUCCESS);
}

I got the following errors: (gcc 3.2.2)
obj1.cc: In function `int main()':
obj1.cc:11: no matching function for call to `stud::stud(int)'
obj1.cc:6: candidates are: stud::stud()
obj1.cc:6: stud::stud(const stud&)


What does that have to do with the copy constructor or with the
initialization using =? You're trying to initialize a stud with 5. But
there is no constructor that takes an int or something that int can be
converted to.

stud s(5);

would fail with the same message. Try the following code:

class stud
{
// deliberately make the copy constructor private, so it's inaccessible
stud(const stud&) {}
public:
stud(int) {}
};

int main()
{
stud s = stud(5);
}
Not sure how, the call "stud s=stud(5);" would call a copy constructor.
As I wrote, stud(5) creates a temporary stud object and initializes with
with 5. It needs something like a stud(int) constructor to do this. Then, s
is created from that temporary, and for that, the copy constructor is used.
Then, the temporary is destroyed.
It should not. Let me know if I am wrong..


You are.

Jul 23 '05 #11
Jaspreet wrote:


#include <iostream>

using namespace std;

class stud
{
public:
stud (stud &s)
{
cout<<endl<<"copy";
}
stud(int a)
{
cout<<endl<<"1-arg constructore";
}
};
int main()
{
stud s=stud (5);
return (EXIT_SUCCESS);
}

[snip]
Not sure how, the call "stud s=stud(5);" would call a copy constructor.
It should not. Let me know if I am wrong..
You are wrong

The sequence of events in

stud s = stud(5);

is:

create a temporary stud object and initialize it with 5 using
the stud::stud(5) constructor.

create the object s and initialize it with the previously generated
temporary object. For this the copy constructor is used (since this
is object creation. An assignment is never used for object initialization,
even if you write '=')

Your program failes because a temporary cannot be bound to a reference which
is not const. Now look at your 'copy constructor':
stud (stud &s)
{
cout<<endl<<"copy";
}


It takes the argument by reference. But this reference is not const. Thus the
temporary cannot be bound to it and hence the compilation fails.
stud s = stud(5);

is equivalent to

stud s ( stud(5) );

Don't confuse yourself. Even if you write '=', this is *not* an assignment.
This is the construction of a new object and for that *always* a constructor
is used. (Assignment would require that the object existed previously, but this
is an object under construction, thus assigment cannot be used)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #12

Karl Heinz Buchegger wrote:
Jaspreet wrote:


#include <iostream>

using namespace std;

class stud
{
public:
stud (stud &s)
{
cout<<endl<<"copy";
}
stud(int a)
{
cout<<endl<<"1-arg constructore";
}
};
int main()
{
stud s=stud (5);
return (EXIT_SUCCESS);
}

[snip]
Not sure how, the call "stud s=stud(5);" would call a copy constructor. It should not. Let me know if I am wrong..


You are wrong

The sequence of events in

stud s = stud(5);

is:

create a temporary stud object and initialize it with 5 using
the stud::stud(5) constructor.

create the object s and initialize it with the previously generated
temporary object. For this the copy constructor is used (since this
is object creation. An assignment is never used for object

initialization, even if you write '=')

Your program failes because a temporary cannot be bound to a reference which is not const. Now look at your 'copy constructor':
stud (stud &s)
{
cout<<endl<<"copy";
}
It takes the argument by reference. But this reference is not const.

Thus the temporary cannot be bound to it and hence the compilation fails.
stud s = stud(5);

is equivalent to

stud s ( stud(5) );

Don't confuse yourself. Even if you write '=', this is *not* an assignment. This is the construction of a new object and for that *always* a constructor is used. (Assignment would require that the object existed previously, but this is an object under construction, thus assigment cannot be used)


Hi

Yes I know that is not assignment. I apologise for dragging it more,
but consider the following code:

#include <iostream>
using namespace std;

class stud
{
};
int main()
{
stud s=stud (5); //this should fail
return (EXIT_SUCCESS);
}

According to me, "stud s=stud (5);" should fail and should never try to
call a copy constructor. While compiling the code, compiler (gcc 3.2.2)
complains:
obj.cc: In function `int main()':
obj.cc:10: no matching function for call to `stud::stud(int)'
obj.cc:6: candidates are: stud::stud()
obj.cc:6: stud::stud(const stud&)

If that call had been successful then it should have called the copy
constructor which is created by c++ for us, but it gives the above
error messages. Let me know what I am missing.

Thanks!! and have a nice day!!

Jul 23 '05 #13
Jaspreet wrote:

[snip]

Hi

Yes I know that is not assignment. I apologise for dragging it more,
but consider the following code:

#include <iostream>
using namespace std;

class stud
{
};
int main()
{
stud s=stud (5); //this should fail
return (EXIT_SUCCESS);
}

According to me, "stud s=stud (5);" should fail and should never try to
call a copy constructor. While compiling the code, compiler (gcc 3.2.2)
It must have been tried to call the copy constructor(unless compiler
optimize it away as mentioned by Rolf Magnus), if you had supplied a
constructor like .. stud::stud(int).
complains:
obj.cc: In function `int main()':
obj.cc:10: no matching function for call to `stud::stud(int)'
obj.cc:6: candidates are: stud::stud()
obj.cc:6: stud::stud(const stud&)

If that call had been successful then it should have called the copy
constructor which is created by c++ for us, but it gives the above
error messages. Let me know what I am missing.

Thanks!! and have a nice day!!


Krishanu

Jul 23 '05 #14
Jaspreet wrote:

Hi

Yes I know that is not assignment. I apologise for dragging it more,
but consider the following code:

#include <iostream>
using namespace std;

class stud
{
};
int main()
{
stud s=stud (5); //this should fail
return (EXIT_SUCCESS);
}

According to me, "stud s=stud (5);" should fail and should never try to
call a copy constructor. While compiling the code, compiler (gcc 3.2.2)
complains:
obj.cc: In function `int main()':
obj.cc:10: no matching function for call to `stud::stud(int)'
obj.cc:6: candidates are: stud::stud()
obj.cc:6: stud::stud(const stud&)

If that call had been successful then it should have called the copy
constructor which is created by c++ for us, but it gives the above
error messages. Let me know what I am missing.
What's unclear about that?

What is the compiler telling you?
It tells you:
obj.cc: In function `int main()':
obj.cc:10: no matching function for call to `stud::stud(int)'
"Hey buddy, I was looking for a constructor that takes an int as an argument.
But I could not find one, because ....
obj.cc:6: candidates are: stud::stud()
obj.cc:6: stud::stud(const stud&)


... I found only those 2: The default constructor and the copy constructor
But no constructor taking an int."

The compiler is not complaining about the copy constructor. It is complaining
about the missing constructor which takes an int.

The copy constructor has nothing to do with the error message (other then
that g++ lists it as one constructor which it tried, but could not use for
creating the temporary object).

Just in case you missed it, again: In

stud s=stud (5);

there are *2* constructors called!
One for creating the temporary object. The second for initializing
the object s from that temporary.

This time your example fails, because the first part (creating the temporary)
cannot be done.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #15

Karl Heinz Buchegger wrote:
[snip]
Just in case you missed it, again: In

stud s=stud (5);

there are *2* constructors called!
One for creating the temporary object. The second for initializing
the object s from that temporary.

This time your example fails, because the first part (creating the temporary) cannot be done.

--
Karl Heinz Buchegger
kb******@gascad.at


Thanks Karl. I should have got this before. Apologies for being a drag
on this.

Thanks!! and have a nice day!!
Jaspreet

Jul 23 '05 #16

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
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);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.