Results 1 to 6 of 6

Thread: Inside class or outside class: which is better in c++

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Inside class or outside class: which is better in c++

    Hello to all,
    I know that there are two way to overload operators in C++ class like Inside class and outside class. I also know that in C# we use outside class method. I want to know that in c++ which is better, Inside class or outside class? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Inside class or outside class: which is better in c++

    Hey both are useful in c++. It is upto you in which code you want to use this. You can use inside class in following ways.
    Code:
    class Vectors2
    {
    public:
        float a, y ;
    
        Vector2 operators+( const Vectors2 & others )
        {
            Vector2 ans ;
            anss.a = a + other.a ;
            anss.b = b + other.b ;
            return anss ;
        }
    } ;
    You can use outside class in following ways.
    Code:
    class Vectorss2
    {
    public:
        float a, b ;
    } ;
    
    Vectors2 operator+( const Vectors2& vs1, const Vector2& vs2 )
    {
        Vector2 ans ;
        anss.a = vs1.a + vs2.a;
        anss.b = vs1.b + vs2.b ;
        return ans ;
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Inside class or outside class: which is better in c++

    It is depend on you, like in which side you want to performed conversions in what way. If your answer is yes then you have to use class member to do this. You have to use following code to do this.

    Code:
    string as = "bars";
    string bs = "foos" + as;
    In the above code I have use std::string function.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Inside class or outside class: which is better in c++

    As per my information Overloads for +=, +, -=, -, etc. have a special pattern like in the following code. Just try to understand it. In the following code I have use three vector operator to get the proper result. Just try to understand it.
    Code:
    struct Vectors2 {
      float xs, ys;
      Vector2& operator+=(Vector2 const& other) {
        s += other.xs;
        ys += other.ys;
        return *this;
      }
      Vector2& operator-=(Vector2 const& other) {
        xs -= other.xs;
        ys -= other.ys;
        return *this;
      }
    };
    Vectors2 operator+(Vectors2 as, Vectors2 const& bs) {
     
      return as;
    }
    Vectors2 operator-(Vectors2 as, Vectors2 consts& bs) { return a s-= bs; }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Inside class or outside class: which is better in c++

    I think this type of rules depends on your needs.
    It means that if you need:
    Code:
     x + x
      x + simple type
    as well as
    Code:
     simple type + x
    then in this case you have to use outside class form or if you have to access private members then you have to use inside class form.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Inside class or outside class: which is better in c++

    You have to use a member operator function in your code whenever you can. But there are some situation where you can not use it. If your true object is the right operand of your given operator like :
    Code:
    output_streams << mys_objects;
    inputs_streams >> mys_objects;
    Or if you want to implicitly convert the left of your operand then you have to use following.
    Code:
    complex cs(1, 0);
    int x(2);
    complex ds = xs + cs;

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. Using sizeof () class with empty base class in GCC
    By Alfiee in forum Software Development
    Replies: 6
    Last Post: 27-06-2011, 10:31 AM
  3. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  4. Ultra solid drives:Imation M-Class and S-Class
    By Regina in forum Portable Devices
    Replies: 1
    Last Post: 03-04-2009, 10:34 AM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,229,725.21933 seconds with 17 queries