Re: class constructor question
EDIT:
Quote:
Originally Posted by StGuru
Why that constructor, when I got other unparamterized?
Why not that constructor, when that constructor's signature is a match?
Quote:
Originally Posted by StGuru
Why in this code when I pass by value, there isnt invoking of the constructor of class B?
The copy constructor of B is invoked. Since no copy constructor is declared, the compiler defines it for you.
Re: class constructor question
Why it must be parametrized so that the copy of the object would invoke the constructor?
Is that by default?
Re: class constructor question
Quote:
Originally Posted by StGuru
Why it must be parametrized so that the copy of the object would invoke the constructor?
Back to the basics:
Code:
#include <iostream>
void foo()
{
std::cout << "hello" << std::endl;
}
void foo(int n)
{
std::cout << n << std::endl;
}
int main()
{
foo(123);
}
I think that the output should involve "hello" because the function foo is called. Do you agree?
Re: class constructor question
I totally agree. It is same with the objects. The constructor that is called depends from the arguments that it has.
Here:
Code:
#include <iostream>
using namespace std;
class B
{
public:
B()
{
cout<<"test";
}
int n;
};
class test
{
private:
int y;
public:
int z;
test()
{
y=1;
}
test(B object)
{
y=object.n;
}
void print_y();
};
void test::print_y()
{
cout<<y<<endl;
}
int main()
{
B ob2;
B ob3;
cout<<"Put whatever you want";
cin>>ob2.n;
test ob1(ob2);
ob1.print_y();
system("PAUSE");
}
test ob1(ob2);
The constructor
test(B object)
{
y=object.n;
}
is invoked because of the argument ob2.
Now there is copy of ob2, which is object from the class B. I asked when this object is created, which constructor is invoked, the parametrized or parametrized and why (since I never stated each of them to be invoked with the creation of the copy) ?
Thanks in advance.
Re: class constructor question
Quote:
Originally Posted by StGuru
I asked when this object is created, which constructor is invoked, the parametrized or parametrized and why (since I never stated each of them to be invoked with the creation of the copy) ?
It has to be parameterised, since you are invoking the copy constructor to make a copy of what you passed as an argument.
Re: class constructor question
Quote:
Originally Posted by
StGuru
Now there is copy of ob2, which is object from the class B. I asked when this object is created,
Which object? The B object?
Let's take this one step at a time:
1) You created a test object with a single parameter, which is a B object. So the constructor for test that takes a B object is invoked.
2) Since you passed by-value a B object to the test constructor, the compiler automatically generates a copy of the B object. For the compiler to generate a copy of the B object, the B object's copy constructor is called. Since you didn't provide a user-defined copy constructor for B, the compiler generated version of the copy constructor for B is invoked instead.
So you have two objects being created with that single line of code:
You have one "test" object, and a temporary "B" object being created.
Also note that copy construction can be removed by the compiler if it is detected that the copy is superfluous -- another wrinkle in this whole scenario. But in general, steps 1) and 2) is what is being done.
Regards,
Paul McKenzie
Re: class constructor question
Thanks for the replies. I understand what are u saying to me. But I don't understand why it has to be parametrized. I got another nonparametric in the object B. Why it is not called, when there is not another constructor?
Re: class constructor question
Quote:
Originally Posted by StGuru
I got another nonparametric in the object B. Why it is not called, when there is not another constructor?
1. The default constructor is not called because its signature does not match in any way.
2. The compiler generates a copy constructor for you if necessary.
Re: class constructor question
And what does that constructor do (which is created by compiler)?
What if I write:
B(int y)
{
cout<<"test";
}
would the copy of object invoke this constructor( I tried and it didn't, but why)?
test(B object)
{
y=object.n;
}
Re: class constructor question
Quote:
Originally Posted by StGuru
And what does that constructor do (which is created by compiler)?
It performs "shallow" copying of the object's member variables (and base class subobjects).
Quote:
Originally Posted by StGuru
would the copy of object invoke this constructor( I tried and it didn't, but why)?
Why would it? A constructor that takes an int argument is not a copy constructor.
Re: class constructor question
Oh, I see now what are you saying to me. I thought that with the creation of new object there will be invoked constructor which is inside the class (the same like declaring object and invoking constructor). I didn't know that there is copy constructor which creates new object. And what if I write my own copy-constructor like this:
B(const B &object)
{
cout<<"test";
}
Would then will be created copies of the object's member variables?
test(B object)
{
y=object.n;
}
Re: class constructor question
Quote:
Originally Posted by
StGuru
Code:
#include <iostream>
using namespace std;
class test
{
private:
int y;
public:
int z;
test()
{
y=1;
}
test(test object)
{
y=object.z;
}
void print_y();
};
void test::print_y()
{
cout<<y<<endl;
}
int main()
{
test ob2;
cout<<"Put whatever you want";
cin>>ob2.z;
test ob1(ob2);
ob1.print_y();
}
Hehe, we're having a match of guru's national sport here. All trying to make u get it. Okay lemme join in :D
1st: StGuru, try to accept the idea that your C++ compiler is not bugged, but just rightfully complaining about the code input to it.
Here is my approach :)
say you are the programmer, and I am the compiler. Now let's inspect your code from the main() function line by line.
you say: test ob2;
I say: very good, I shall instantiate an object of type 'test' on main() local stack. It looks like the programmer has not specified any constructor for his object named 'ob2', so I shall just invoke the default (e.g. without arguments) constructor. I don't care if my programmer has specifically defined one or not, because if he didn't, I shall define it by default. However, in this scenario there is one such constructor defined explicitly by programmer.
you say: cout<<"Put whatever you want"; cin>>ob2.z;
I say: .. not really important what I say, I just do what you wanted to do here
you say: test ob1(ob2);
I say: (inside main() - excellent, another object of type 'test'. And my programmer would like me to use another constructor for this one, and I dont care if it is defined or not, because if it isn't I shall define a default copy constructor (i.e. with one 'test' argument). However in this scenario, I do have a copy constructor defined by programmer
I say: (in test(test object)) - Ok, so I shall just proceed along and carry on the instantiation of 'ob1'. Let me see now ... the arguments passed is of ... type .. 'test'. And it is a very vivid (valid and instantiated) argument this one u have passed - 'ob2'. Only one tiny problem, 'ob2' is valid and instantiated in the context of function main (i.e. on main()'s stack. I do not have any idea of what this parameter might be on the current (i.e. on test(test object)'s stack. But I can fix it, I shall just copy the value of 'ob2' from inside main() function into another variable, this time local to me, and carry on. I other words, the 'ob2' that is supposed to get inside of test(object test) is not the same thing with 'ob2' from main()'s tack. So I have to make a copy of this ob2 as I have said, and how to do that other than ... well ... invoking once again the copy constructor, so:
I say: (in test(test object) {2} ) - Ok, so I shall just proceed along and carry on the instantiation of 'ob1'. Let me see now ... the arguments passed is of ... type .. 'test'. And it is a very vivid (valid and instantiated) argument this..... ET CETERA
Now, what if the copy constructor was test(test* object) or test(test& object) - the compiler would have checked to see if it has access to the 'ob2' defined inide main() only this time it would conclude that yes, it does, so no copy constructor is needed. It would just use the same 'ob2' as the one in main. Full stop. End of story.
Do I get a beer ?
B)
Re: class constructor question
**** it! It took me 32 minutes+ to rite the previous post, and U have figured it out ....
Re: class constructor question
kellogs it helped me. Thank you very much for the help. I own u beer! :-). What I can't understand now is, will the same do
B(const B &object)
{
cout<<"test";
}
or it will not create object and will print test?
Re: class constructor question
Quote:
Originally Posted by StGuru
What I can't understand now is, will the same do
B(const B &object)
{
cout<<"test";
}
or it will not create object and will print test?
No copying is involved since the parameter is a const reference.