0

I have a class named MyInteger and this class has one data member of type int - how could a overload the operator "=" to return this integer? I am not allowed to use an accessor-method to return the integer.

data member

 private:
    int number;

function

 int MyInteger::operator=(MyInteger myInteger) {

    myInteger = this->number;

    return myInteger;
}

I know this is wrong and I have tried to make a typecast but that is also wrong.

In another class I am using this integer just to print

 cout << number << endl;

How do I solve this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2991252
  • 760
  • 2
  • 11
  • 28
  • How are you wanting to use this operator? – Joseph Mansfield Dec 21 '13 at 12:49
  • 2
    `operator=` is supposed to return `*this;` by convention. Not doing that instantly adds confusion. It looks like you're after more of a conversion operator. – chris Dec 21 '13 at 12:50
  • 3
    You should be overloading `operator<<` for your class instead of making `operator=` return something other than `MyInteger&`. – Simple Dec 21 '13 at 12:52

2 Answers2

5

Your example of how you want to use this does not use the = operator at all. How would overloading operator= help you here?

If you want to be able to insert your MyInteger objects to output streams, you need to overload operator<< where the left operand is an output stream and the right operand is your MyInteger object.

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
-6

I think you do not mean the assignment operator, but are speaking about a conversion operator.

For example:

#include <iostream>
#include <algorithm>
#include <iterator>

class MyInteger
{
public:
    MyInteger() : number( 0 ) {}
    void operator ()( int x ) { if ( x < 0 ) ++number; }
    operator int() const { return number; }

private:
    int number;
};

int main()
{
   int a[] = { 1, 2, -3, 4, -5, 6, -7 };
   int count = std::for_each( std::begin( a ), std::end( a ), MyInteger() );
   std::cout << "There are " << count << " negative values" << std::endl;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 5
    But note that this is almost certainly a bad idea. You're going to totally mess up various overloaded function calls. Prefer to define `operator<<` for `std::ostream&` and `MyInteger`. – Lightness Races in Orbit Dec 21 '13 at 13:06
  • @ Lightness Races in Orbit I do not think so because as I have understood this class emulates integral numbers. So it also can be used in arithmetic operations. Moreover I gave direct answer to the question. There is no question what is better. – Vlad from Moscow Dec 21 '13 at 13:10
  • 1
    It may emulate integral numbers, but it is not one. It is a class type. Implicit conversions — particularly to built-in types — are _evil_! Sometimes a direct answer is harmful. Of course in this case it's hard to tell what the real use case is since this particularly example is so contrived and does nothing but wrap `int`. – Lightness Races in Orbit Dec 21 '13 at 13:23
  • @Lightness Races in Orbit You are trying to discuss totally another question. – Vlad from Moscow Dec 21 '13 at 13:26
  • 1
    You can talk! The question asks for an `operator=` and you give him a conversion operator. We're both looking for his _actual_ problem and trying to give an _actual_ solution. – Lightness Races in Orbit Dec 21 '13 at 13:37
  • @Lightness Races in Orbit I gave an actual solution. I did not answer the question what approach is better because I even do not know how the class is defined. It is not my task to design the class. Your reasoning is simply stupid because for example its class can be a functor used for example in std::for_each algorithm and the best and simple way to get value is to use the conversion operator. Please stop trolling. – Vlad from Moscow Dec 21 '13 at 13:54