user defined assignment

Posted on 2002-06-12
Medium Priority
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.

Question by:GGRUNDY

Accepted Solution

jasonclarke earned 200 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:


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

LVL 22

Expert Comment

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;

Author Comment

ID: 7075343
Thanks guys.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

624 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