开发者

Compare/Copy COORD struct; overloading

开发者 https://www.devze.com 2023-02-14 10:41 出处:网络
This seems trivial yet endless searching doesn\'t yield an answer. I need to compare and assign. How can I overload the COORD if I can\'t add a member function or friend a function?

This seems trivial yet endless searching doesn't yield an answer.

I need to compare and assign.

How can I overload the COORD if I can't add a member function or friend a function?

is it just开发者_开发知识库 bad style to use this windows based structure?

Also, I know I can write my own class(or just do the operation once for each member), but this problem just really has me wondering.


COORD only has public members, so there's no need for friend functions -- free operators should suffice:

bool operator <(COORD const& lhs, COORD const& rhs)
{
    return lhs.Y < rhs.Y || lhs.Y == rhs.Y && lhs.X < rhs.X;
}

bool operator ==(COORD const& lhs, COORD const& rhs)
{
    return lhs.X == rhs.X && lhs.Y == rhs.Y;
}

COORD has an implicit copy c'tor and operator= already, no need to define those.


Why not derive your on class from public COORD and add the required statements? struct in C++ is just the same as class, except that by default all members are public.

struct MyCoord : public COORD
{
  // I like to have a typedef at the beginning like this
  typedef COORD Inherited;
  // add ctor, if you like ...
  MyCoord(SHORT x, SHORT y)
    : Inherited::X(x)
    , Inherited::Y(y)
  { }
  // no need for copy ctor because it's actually POD (plain old data)

  // Compatibility ... ;)
  operator COORD&()
  {
      return *this; // may need cast
  }

  COORD* operator&()
  {
     return this; // may need cast
  }
  // declare friends ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消