Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help with objects

Hi all,
I was wondering, is there a way to assign one object another objects' abilities? I have tried 'object1 = object2;' but it doesn't seem to work.
Does anybody know how to do this?
[214 byte] By [Lord Squallop] at [2007-11-11 10:04:29]
# 1 Re: Help with objects
object1=object2; should normally work, unless the class of these objects explicitly prohibits assignment. You need to shwo us more of your code, particularly the class declaration, to tell why this code doesn't work.
Danny at 2007-11-11 20:59:33 >
# 2 Re: Help with objects
If they r from the same class it must work by puting each member value to it's corresponing ... else [if they r not from same class] u must specify the way to overload this equality .
Amahdy at 2007-11-11 21:00:27 >
# 3 Re: Help with objects
Sorry for the long wait. Even if I found out what was wrong, I couldn't compile my code to see if it worked. VC++ Express is acting all wierd and won't compile anything. It might just be my computer, though. Any ideas how I could fix this? I think I should before I change the program.
Thanks for the help.
Lord Squallop at 2007-11-11 21:01:37 >
# 4 Re: Help with objects
disable precompiled headers. If thats not it, give more info or reinstall. Also you might try g++ or the free borland one just to have a compiler while you figure out what is wrong with VC -- its always good to have 2+ compilers around anyway.
jonnin at 2007-11-11 21:02:38 >
# 5 Re: Help with objects
On your orig question:
if the two objects are inherited one from another (either direction) you can just use assignment operator. If there is complexity, you might write your own assignment operator to handle whatever you want to happen. If they are the same type of object you can use assignmet as well of course.
jonnin at 2007-11-11 21:03:37 >
# 6 Re: Help with objects
i think the problem you are facing is operator overloading. When creating objects, you need to define what each operator does to these objects and their variables. For examlpe, the assignment operator for a class Cat could mean to assign the first object's age to the others. Heres the code for this example:

#include <iostream>

using namespace std;

class Cat
{
public:
Cat();
~Cat(){}
void SetAge(int age) { itsAge = age; }
int GetAge() const { return itsAge; }
void display() const { cout << "age: " << itsAge << endl; }
Cat & operator=(const Cat &);
private:
int itsAge;
};

Cat::Cat():
itsAge(0)
{}

Cat & Cat::operator =(const Cat & rhs)
{
itsAge = rhs.GetAge();
return *this;
}

int main()
{
Cat one;
one.SetAge(8);
one.display();

Cat two;
two.display();

two = one;
two.display();
return 0;
}
may4life at 2007-11-11 21:04:37 >
# 7 Re: Help with objects
in the operator no need for the GetAge() function just the private member could be acceed : "rhs.itsAge;"
Amahdy at 2007-11-11 21:05:41 >
# 8 Re: Help with objects
Yep, thats true! you can use

itsAge = rhs.itsAge;

instead of

itsAge = rhs.GetAge();

due to the fact that the function is a member function, meaning all variables can be directly accessed. Good man for pointing it out ;)
may4life at 2007-11-11 21:06:38 >
# 9 Re: Help with objects
Thanks, but I don't really understand the Cat & operator= (const Cat &) function. Just a quick explanaition would help a lot.
Lord Squallop at 2007-11-11 21:07:35 >
# 10 Re: Help with objects
First remember that if the target is to put the data of an object in another object of the same class , no need to overload operators ..
-The operator "=" from the class Cat once found it's by default take a "Cat" type from the left hand side , I mean when the compiler find a Cat object followed by "=" , it will go throw the function "Cat & Cat:: operator=(const Cat & rhs)" which take parameter "(const Cat & rhs)" found at the right hand side of the operator .
if there is no returns the left hand side will not change , but the return type "Cat" founded at the begin of the function satisfy that it must be changed to what the function return . hope this helps .
Amahdy at 2007-11-11 21:08:43 >
# 11 Re: Help with objects
Cat & operator= (const Cat &) is a member function. It allows any Cat object to use the = operator to assign another Cat object to itself.
The return value is a reference to the modified object, after the assignment, so in sessence, the function returns a reference to its *this.
The parameter const Cat & is a reference to a const Cat object. This means that the function doesn't modify its parameter, it only reads data from it.
How is the assignment operator used? Fro example:
Cat c1, c2;
c2=c1; //calls assignment op
Danny at 2007-11-11 21:09:41 >
# 12 Re: Help with objects
Thanks! That helps a lot. I got a second compiler, so I should be able to continue writing the program. :D
Lord Squallop at 2007-11-11 21:10:44 >