2

I have a simple c++ class, intHolder, that only holds an int. It can also add to that int, which works, or add itself to another int contained in a different intHolder, which does not work. This is nothing like what I've encountered in Java. What's going on?

class intHolder{
private:
    int i;
public:
    intHolder(int myInt){i = myInt;}
    void addToInt(int inc){ i = i + inc;}
    void printInt(){cout << i << endl;}
    void addToOtherInt(intHolder other){other.addToInt(i);}
};

Main Method

int main () {
    intHolder ih1(1);
    ih1.printInt();//Should be 1, is 1
    ih1.addToInt(3);
    ih1.printInt();//Should be 4, is 4
    intHolder ih2(2);
    ih2.printInt();//Should be 2, is 2
    ih1.addToOtherInt(ih2);
    ih1.printInt();//Should be 4, is 4
    ih2.printInt();//Should be 6, is 2
};
Enrico
  • 10,377
  • 8
  • 44
  • 55
Elliot JJ
  • 543
  • 6
  • 19

1 Answers1

12

You are passing the intHolder by value. This means the function acts on a local copy, so there is no effect on the caller side. You need to pass a reference:

void addToOtherInt(intHolder& other) { other.addToInt(i); }
                            ^

Note that usually when you have types that hold other types that support arithmetic operations, you provide overloaded operators so you can do things like

intHolder a = 5;
intHolder b = 10;
intHolder c = a + b;
c += 42;
a = 42 - b;

and so on. See this extensive discussion on operator overloading for more information.

Also, for "printing", it is customary to overload ostream& operator<<, which then allows you to stream to all kinds of streams, including but not limited to std::cout. For example:

struct Foo
{
  int i;
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
  return o << f.i;
}

allows you to say

Foo f;
std::cout << f;
std::cerr << f;
std::ofstream tmp("foo.txt");
tmp << f;
Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I think that if you add to the answer a little explanation about operator overloading, it would be useful and nice :) – PaperBirdMaster Feb 06 '14 at 08:33
  • This answers my question; I successfully complete my test. Pointers are a bigger issue for me than operators. Can you point to some source oriented around "Pointer pitfalls for the Java programmer transitioning to C/C++"? – Elliot JJ Feb 06 '14 at 08:40
  • 1
    It should also be noted that the usual convention is that method modifies the invocant and not the arguments, so the method should be written the other way around. And in general, functions modifying their arguments are not very readable. – Jan Hudec Feb 06 '14 at 08:41
  • 1
    @ElliotJans I would take a look at some of the material in the [C++ book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). – The Forest And The Trees Feb 06 '14 at 08:42
  • @ElliotJans re. pointers, I like the general advice given [here](http://klmr.me/slides/modern-cpp/#1). – juanchopanza Feb 06 '14 at 08:43
  • @ElliotJans also pay special attention to Jan Hudec's comment. – juanchopanza Feb 06 '14 at 08:45