Solved

user defined assignment

Posted on 2002-06-12
3
247 Views
Last Modified: 2010-04-02
I would love to have a user defined copy function
like this one.

void operator=(RGBQUAD& q, PALETTEENTRY& pe)
{
  q.rgbRed = pe.peRed;
  q.rgbGreen = pe.peGreen;
  q.rgbBlue = pe.peBlue;
  q.rgbReserved = 0;
  }

but to compiler tells me to bugger off.
Is "void Copy(RGBQUAD& q, PALETTEENTRY& pe)"
my only option? Thanks.

0
Comment
Question by:GGRUNDY
3 Comments
 
LVL 9

Accepted Solution

by:
jasonclarke earned 50 total points
ID: 7075017
An assignment operator must be a member function of a class... it is an operator that works on an existing class instance.
 
Try adding it to your RGBQUAD class/struct, like this:

class RGBQUAD
{
//...

    RGBQUAD& operator=(const PALETTEENTRY& pe)
    {
       rgbRed = pe.peRed;
       rgbGreen = pe.peGreen;
       rgbBlue = pe.peBlue;
       rgbReserved = 0;
       return *this;
    }

};
0
 
LVL 22

Expert Comment

by:nietod
ID: 7075127
FYI the reason for this, is because you cannot write an assignment to one of the built-in types.  i.e. it is to prevent you from doing things like

int &operator = (int l, int r);
int operator = (int l, double r);

which have already been defined by the language.  


Note how Jason changed the return value of your function.     This is not required, but it is a customary practice.  by doing that you can "chain" assignments like

a = b = c;
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 7075343
Thanks guys.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question