473,511 Members | 12,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment Overloading

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

Oct 3 '05 #1
1 1798
Jon Slaughter wrote:
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];
}
Oct 3 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4816
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
2566
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
1595
by: jim.brown | last post by:
The attached code implements a test class to show an error in overloading operator=. This code works on Windows with Visual Studio and simpler cases work with gcc 3.3.2 on Solaris 9. On Windows,...
2
1697
by: Funky | last post by:
In the code snippet below, I have a base class (originally written in C++) that byte swaps data when assigned or read from the class (thus, always storing the data as big-endian). As for those...
12
8041
by: Achim Domma | last post by:
Hi, I want to use Python to script some formulas in my application. The user should be able to write something like A = B * C where A,B,C are instances of some wrapper classes. Overloading...
9
3477
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
12
438
by: subramanian100in | last post by:
For a class Test, we write the assignment operator as Test & Test::operator=(const Test & rhs); instead of const Test & Test::operator=(const Test & rhs); that is, the return type of...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.