Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

user defined assignment

Posted on 2002-06-12
3
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
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:

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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++.

722 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