DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Operator overloading

  1. #1
    Adam Guest

    Operator overloading


    I have a struct declared in a file. I want to overload the '+' and '<' operators
    to use with this struct.So will it work if I have the overload function in
    my main file???

    Adam

  2. #2
    ch0rlt0n Guest

    Re: Operator overloading


    "Adam" <adamApp@netscape.com> wrote:
    >
    >I have a struct declared in a file. I want to overload the '+' and '<' operators
    >to use with this struct.So will it work if I have the overload function

    in
    >my main file???
    >
    >Adam


    Should do.

    #include "yourclass.h"

    bool operator<(const yourclass& lhs, const yourclass& rhs)
    {
    // Your code (can't access private members though)
    }


  3. #3
    Danny Kalev Guest

    Re: Operator overloading



    ch0rlt0n wrote:
    >
    > "Adam" <adamApp@netscape.com> wrote:
    > >
    > >I have a struct declared in a file. I want to overload the '+' and '<' operators
    > >to use with this struct.So will it work if I have the overload function

    > in
    > >my main file???
    > >
    > >Adam

    >
    > Should do.
    >
    > #include "yourclass.h"
    >
    > bool operator<(const yourclass& lhs, const yourclass& rhs)
    > {
    > // Your code (can't access private members though)
    > }


    Adding to ch0rlt0n's explanation: if the overloaded operator needs to
    access non public date members, the struct should declare the overloaded
    operator as its friend.

    Danny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center