-
April 27th, 2009, 10:00 AM
#1
class constructor question
I got this code:
Code:
#include <iostream>
using namespace std;
class test
{
private:
int y;
public:
int z;
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();
}
I got dozens of errors like "You should use const test &". I think everything is correct with the code. why i got those errors?
Thanks in advance.
Regards.
-
April 27th, 2009, 10:14 AM
#2
Re: class constructor question
Originally Posted by StGuru
I think everything is correct with the code.
Obviously it's not.
First, you can't pass test by-value in your copy constructor. Think about it, the reason should be quite obvious (HINT: what happens when you pass something by-value?).
Second, you'll need to explicitly define a default constructor in addition to your (broken) copy constructor.
Third, this rubbish interface can be easily replaced by providing an appropriate operator>> overload.
-
April 27th, 2009, 10:29 AM
#3
Re: class constructor question
If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?
-
April 27th, 2009, 10:32 AM
#4
Re: class constructor question
Originally Posted by StGuru
If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?
Sigh... Let's try this again.
What gets invoked when you pass by-value?
-
April 27th, 2009, 10:35 AM
#5
Re: class constructor question
Originally Posted by StGuru
If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?
and where do you magically get your copy from?
-
April 27th, 2009, 10:42 AM
#6
Re: class constructor question
There is copy created of the ob2. I should use pass by reference if I alter the ob1 variables but I don't alter them.
For example if I need to change something in the object
Function1(test &ob1)
{
ob1.z=5;
}
or something like that.
-
April 27th, 2009, 10:47 AM
#7
Re: class constructor question
Originally Posted by StGuru
There is copy created of the ob2. I should use pass by reference if I alter the ob1 variables but I don't alter them.
For example if I need to change something in the object
Function1(test &ob1)
{
ob1.z=5;
}
or something like that.
You're missing the ****ing point.
When you pass T by-value, T's copy constructor gets invoked. In your case, over and over and over. In short, impossible. Passing T by (const) reference alleviates that problem.
If you don't understand this simple concept, perhaps you should stick with shallow copying for now. Besides, that snippet in your OP doesn't require an explicit copy constructor in the first place.
-
April 27th, 2009, 10:48 AM
#8
Re: class constructor question
Originally Posted by StGuru
I should use pass by reference if I alter the ob1 variables but I don't alter them.
There is another reason to use pass by reference: to avoid copying the object. If you do not want to modify the object through the reference then pass by const reference.
Originally Posted by StGuru
There is copy created of the ob2.
Indeed. And the act of copying invokes the copy constructor, which happens to be precisely what you are trying to implement. So what you are saying is this: I want to copy the object in order to implement copying of the object. That does not make sense.
-
April 27th, 2009, 11:00 AM
#9
Re: class constructor question
@Plasmator, I understand what are u saying to me, but I got copy of ob2, which doesn't got constructor.
Even if I put
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();
}
the copy would invoke parametrized constructor and initialize y=1. I don't see a problem.
-
April 27th, 2009, 11:10 AM
#10
Re: class constructor question
You're passing ob2 to the copy constructor by-value.
To pass by value the compiler uses the copy constructor, which copies its parameter by value...
So the compiler uses the copy constructor, which copies its parameter by value...
So the compiler uses the copy constructor, which copies its parameter by value...
etc. etc. until something breaks.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 27th, 2009, 11:13 AM
#11
Re: class constructor question
Originally Posted by StGuru
I got copy of ob2, which doesn't got constructor
obj2 is default constructed.
Originally Posted by StGuru
the copy would invoke parametrized constructor and initialize y=1. I don't see a problem.
How did you manage to come to that conclusion?
Look, I think you are sorely in need of an example:
Code:
#include <iostream>
class X
{
public:
X()
{
std::cout << "default constructor invoked" << std::endl;
}
X(const X&)
{
std::cout << "copy constructor invoked" << std::endl;
}
X& operator=(const X&)
{
std::cout << "copy assignment operator invoked" << std::endl;
return *this;
}
~X()
{
std::cout << "destructor invoked" << std::endl;
}
};
int main()
{
X a;
X b(a);
X c;
c = a;
}
-
April 27th, 2009, 11:15 AM
#12
Re: class constructor question
Originally Posted by StGuru
@Plasmator, I understand what are u saying to me, but I got copy of ob2, which doesn't got constructor.I don't see a problem.
Originally Posted by Plasmator
When you pass T by-value, T's copy constructor gets invoked. In your case, over and over and over. In short, impossible. Passing T by (const) reference alleviates that problem.
JohnW@Wessex's getting at the same thing.
-
April 27th, 2009, 12:10 PM
#13
Re: class constructor question
ok, now I am totaly confused. When the object is declared then the constructor is invoked.
Now when ob1(ob2) the constructor:
test(test ob2)
{
y=object.z;
}
the constructor will create copy of ob2, which will invoke another unparametrized constructor in this case my constructor is:
test()
{
y=1;
}
It is like I create two separate objects. I don't know why u say that the process is infinite.
-
April 27th, 2009, 12:11 PM
#14
Re: class constructor question
Originally Posted by StGuru
the constructor will create copy of ob2, which will invoke another unparametrized constructor in this case my constructor is:
It won't. Logically, the act of copying invokes this constructor:
Code:
test(test ob2)
{
y=object.z;
}
-
April 27th, 2009, 12:36 PM
#15
Re: class constructor question
Why that constructor, when I got other unparamterized?
Why in this code when I pass by value, there isnt invoking of the constructor of class B?
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");
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|