Solved

user defined assignment

Posted on 2002-06-12
3
229 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

11 Experts available now in Live!

Get 1:1 Help Now