Solved

user defined assignment

Posted on 2002-06-12
3
239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now