Assignment Overloading

J

Jon Slaughter

I have a chain of classes(i.e., a series of classes each containing an array
of the next class). Each class has array like access.

struct Myclass1
{
vector(Myclass2) _Myclass2;

Myclass2& operator[](int i);
}


Anyways, I want to be able to do something like

Myclass1 A;
Myclass2 B;
A[3] = B;

that is "caught" by Myclass1. i.e., instead of treating A[3] as a Myclass2
class it would treat it as a Myclass1 class.


Is this possible to do? I know I could write a function in Myclass1 that
would be something like

void assignment(Myclass2& B, int i);

that would do the exact same thing but it would be nice if it were possible
to do the above since it is much easier... in Myclass2 there will be no
assignment operator from Myclass2 to Myclass2 so I don't see why there would
be any confusion... actually, the assignment operator in Myclass2 will do
the exact same but on Myclass3(an so on until the last class, which I don't
know what it will do).

Anyways, using the resolution operator isn't really worth it since it does
exactly the same as the assignment function(though I guess I'd have to
modify it a little to be the exact same).

Jon
 
G

Gianni Mariani

Jon said:
I have a chain of classes(i.e., a series of classes each containing an array
of the next class). Each class has array like access.

struct Myclass1
{
vector(Myclass2) _Myclass2;

(note that _M is a reserved prefix, your app should not use it)
(Any identifier starting with _[A-Z_] is reserved.)
Myclass2& operator[](int i);
}


Anyways, I want to be able to do something like

Myclass1 A;
Myclass2 B;
A[3] = B;

that is "caught" by Myclass1. i.e., instead of treating A[3] as a Myclass2
class it would treat it as a Myclass1 class.


Is this possible to do? I know I could write a function in Myclass1 that
would be something like

void assignment(Myclass2& B, int i);

that would do the exact same thing but it would be nice if it were possible
to do the above since it is much easier... in Myclass2 there will be no
assignment operator from Myclass2 to Myclass2 so I don't see why there would
be any confusion... actually, the assignment operator in Myclass2 will do
the exact same but on Myclass3(an so on until the last class, which I don't
know what it will do).

Anyways, using the resolution operator isn't really worth it since it does
exactly the same as the assignment function(though I guess I'd have to
modify it a little to be the exact same).

I'm not sure I get what you're trying to do but it sounds like you can
use the "proxy" class. The proxy class is usually associated with
temporary objects only.

class X;

struct XArrayAssignProxy
{
unsigned m_i;
X & m_x;
Proxy( unsigned i, X & x )
: m_i(i),
m_x(x)
{}

Myclass1 & assignment( const Myclass1 & A );
Myclass2 & assignment( const Myclass2 & B );

Myclass1 & reference1();
Myclass2 & reference2();

Myclass1 & operator( const Myclass1 & A )
{
return assignment( A );
}

Myclass2 & operator( const Myclass2 & B )
{
return assignment( B );
}

operator Myclass1 & ()
{
return reference1();
}

operator Myclass2 & ()
{
return reference2();
}
};


class X
{
.... stuff
};


Myclass1 & XArrayAssignProxy::assignment( const Myclass1 & A )
{
return m_x.assignment( A, m_i );
}


Myclass2 & XArrayAssignProxy::assignment( const Myclass2 & B )
{
return m_x.assignment( B, m_i );
}

Myclass1 & XArrayAssignProxy::reference1()
{
return m_x.array1[m_i];
}

Myclass1 & XArrayAssignProxy::reference2()
{
return m_x.array2[m_i];
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top