Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Operator overloading


  • Please log in to reply
9 replies to this topic

#1 adam

adam

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 February 2010 - 03:55 AM

Hello! Help me please solve the problem.

Promlem:
Write a class A, so that this piece of code compiled and worked.

A a1;
const A a2(a1);
a1[1]=a2[2];

# include <iostream>

using namespace std;

class A{
      private:
      int x;
      public:
             A():x(0){};//Constructor
             A(int y):x(y){};
                      
             A operator=(A obj);
      
      };
  
      A A::operator=(A obj)
      {
           x=obj.x;
           return *this;
      }
  
  int main()
  
  {
      A a1;
      const A a2(a1);
      a1[1]=a2[2];
      return 0;
  }                

  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 12 February 2010 - 05:22 AM

Wow... uhh wow, okay. First off you don't need to overload operator=, you need to overload operator[]. Second, you need to close the class when you're done. Third, layout man!
#include <iostream>

using namespace std;

class A
{
public: // Public should always go first.
    A() // You don't need a comment saying this is a constructor.
    {
        x = new int[5];
        for (int i = 0; i < 5; ++i)
        {
            x[i] = i + 1;
        }
    }

    ~A()
    {
        delete[] x;
    }

    // All you've got to do is implement the operator overload.

private:
    int *x;
}; // You need to end a class this way.

int main()
{
    A a1;
    const A a2(a1);
    a1[1]=a2[2];
    // You don't need "return 0;" in C++ code.
}
Read here for more on Operator Overloading, hopefully that'll clear things up.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 hakancemyilmaz

hakancemyilmaz

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 12 February 2010 - 06:08 AM

shall we write a copy constructor because we have pointer in the private data , so copying an instance of A object will mean more than copying the data also we should create a new pointer ?
  • 0

#4 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 12 February 2010 - 06:14 AM

You'd probably want to, but the point wasn't to make a copy constructor or to even make the class versatile, the point was to make the code work, right? All you have to do is get the code to function, and to do that all you've got to do is overload the [] operator. If you do that properly you should also be able to get cout to work too. I directed you to Learn C++, which has a specific section on overloading the subscript operator that should shed some light on it.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 hakancemyilmaz

hakancemyilmaz

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 12 February 2010 - 06:35 AM

im ok with [] thing but a1(a2) just a little bit confused me . so writing a copy cons. will make the class more versatile but the code still works without it as i understood . thx anyway for making it clear zeke
  • 0

#6 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 12 February 2010 - 06:57 AM

Well like I said, you should make the copy constructor perform a deep copy because of that, but it's not required. Remember that you don't produce two copies of the array if you don't, you only have one copy of the array, and actually yes you'd need to perform some extra work on the destructor to make sure that it when you delete the array that you NULL it out too or else the second delete[] command will cause a segmentation fault.

I'd go with the copy constructor route, like this:
A(A& other)
    {
        x = new int[5];
        for (int i = 0; i < 5; ++i) x[i] = other[i];
    }
That'll take care of any segfaulting and will perform a deep copy which is what would be user expected.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 adam

adam

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 February 2010 - 11:47 AM

ZekeDragon thank you for the wonderful advice and for the link. I have a another problem

Write a class A, so that this piece of code compiled and worked.

A a1;
A & f(){
return a1;
}
A a2(f());
A & a3(f());

At this time i have no any idea
  • 0

#8 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 12 February 2010 - 12:14 PM

Do you understand what's going on in that code segment?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 adam

adam

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 February 2010 - 12:18 PM

No, I didn't understand especially
A & f(){
return a1;
}
  • 0

#10 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 13 February 2010 - 12:45 PM

f() is a function returning a reference to an A object, in this case a1.
  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download