Archived

This topic is now archived and is closed to further replies.

Neo Genesis10

Overloading the '=' operator

Recommended Posts

Neo Genesis10    140
Hi. I want to overload the '=' operator so that I can assign a structure to be equivalent to another structure. Eg. structA = structB. All of the examples I have seen show the operator as part of a class, for use only when the operation involves that class (such as adding vectors together). What is the syntax for a standalone overload? I also want to overload the '!' operator to handle invalid structures. Any assistance appreciated. Worship the holy trinity that is Blitz, Babes and Beers! [edited by - Neo Genesis10 on March 30, 2004 8:16:33 AM]

Share this post


Link to post
Share on other sites
Neo Genesis10    140
Okay... the site wasnt that great a help, but I thought more about the error message I recieved I got it working finally. Now assignment works perfectly. It apparently needs to be a member of the structure.

Now the ! operator is another story. The real problem here is that if the operator is being called within the struct, then its rather pointles. The idea of using it is to figure out whether or not my struct actually exists in memory. If it doesnt, then attempting to use the overloaded operator portion will (and does) screw up. But it also screws up without it, so im a bit stuck with this one.
#include <string.h>
#include <iostream.h>

struct MyStruct
{
int valueA;
int valueB;
char mystring[20];

void operator=(MyStruct B)
{
this->valueA = B.valueA;
this->valueB = B.valueB;
strcpy(this->mystring, B.mystring);
};

/* bool operator!()
{
if (((this->valueA ==0) && (this->valueB==0)) && strcmp(this->mystring, ""))
return true;
return false;
}; */

};



main()
{
MyStruct * Test[3];

Test[0]->valueA = 50;
Test[0]->valueB = 25;
strcpy(Test[0]->mystring, "TEST A");

Test[1]->valueA = 100;
Test[1]->valueB = 75;
strcpy(Test[1]->mystring, "
TEST B");

Test[0] = Test[1];

cout << "
Test A value A: " << Test[0]->valueA << endl;
cout << "
Test A value B: " << Test[0]->valueB << endl;
cout << Test[0]->mystring << endl;

if (!Test[2]) cout << "
Test C does not exist!" << endl;

return (0);
};
The real problem I have is since the memory space is not initialised, the variables in the struct wont be anything like zero. How can I work this so it reports true if the struct is non-existant?

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Agony    3452
Checking to see whether a pointer is valid or not (which is basically what it sounds like you want your ! operator to do), is platform dependent, and I bet that many platforms don''t have anything available at all o check them, and others might have inadequate methods. I haven''t used them, but I just noticed the other day that Win32 has functions such as IsBadReadPtr() and IsBadWritePtr(). They might do what you want.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites
Neo Genesis10    140
Yeah, its exactly what I want. But I always thought this kinda stuff was a built in ability. It seems perfectly fine to use with classes. I have used !class many times without overloading the operator and it works without fail.

Structs seem to have a very different approach.

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Agony    3452
The only time I''ve seen ! used with pointers is when the pointer has been explicitly set to a valid object or to NULL (or where the pointer is a global static or something, and thus automatically initialized to NULL). Structs and Classes should operate in the exact same way in this situation. A !ptr statement basically says "return true (non-zero) if ptr equals NULL (zero), else return false (zero) if ptr doesn''t equal NULL (zero)". It''s simply converting the pointer to a bool value, and then doing a logical not on it. But it still requires the pointer to either be NULL, or point to a valid object. If it isn''t NULL, but doesn''t point to a valid object, then you''re usually in a pickle. The only option at that point is to use those platform-specific pointer tests.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites
lemurion    336
There''s only one problem with your code, you can''t do:

MyStruct a;
MyStruct b;
MyStruct c;

//set some members of a


c = b = a; // <-- this wont work because your = operator returns void


// your = operator declaration should be (if i recall correctly)

// i''m not sure about the ''const'' though

MyStruct &operator =(const MyStruct &B)
{
}


hope that helps !
Matt

Share this post


Link to post
Share on other sites
Neo Genesis10    140
Thanks for the heads up. I dont particularly see a benefit to the syntax ''a = b = c'' and hence have never used it, but it certainly seems good practice to implement.

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Lord Bart    226
Hello Neo,

define a global = op.

note: I was wrong here need to be class members.

should be fix now for correct way, were you need op = in each class that is passed in the other.


// A = B;

structA structA::operator=(const structB & B)
{
// put code here to asign structA data members form structB

return *this;
}
// B = A;

structB structB::operator=(const structA & A)
{
// put code here to asign structB data members form structA

return *this;
}


Now since your using structs all data member are public,
if you were using a class declear these the other class as friends in each class.
ie. in structA have a
friend class structB;
You should return a copy of the struct since this is not a class memebr op =.

for your ! of invaid structure try this.
make a member operator! method.
Since you can only do a ! on a object


// if(!A)

bool structA::operator! ()
{
bool ret(false);
// put code in here which will test to see if it is vaild

// set ret it vaild

return ret;
}


See if this helps you.

Lord Bart

[edited by - lord bart on March 30, 2004 12:24:31 PM]

[edited by - lord bart on March 30, 2004 2:00:13 PM]

Share this post


Link to post
Share on other sites
Fruny    1658
Neo Genesis10 - Why are you using <string.h> and <iostream.h>? If you implement operator=, you will also need a copy constructor.
Lord Bart - operator= is a binary operator.
Lemurion - You got the const right. operator= returns *this - which is non-const, otherwise you wouldn't be able to do the assignment.



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan


[edited by - Fruny on March 30, 2004 1:13:59 PM]

Share this post


Link to post
Share on other sites