-2

I have a base class and a derived class. Inside the base class is a value, and a function virtual modify(int val) that subtracts that value from the member. Additionally, the derived class overrides modify(int val), which does some checking before calling the base class' method.

When this is called by using derivedObject.modify(someVal) in main(), everything works fine. However, when I call a function modifyAndPrintValue(Base obj), which calls this modify(), the call pattern correctly hits Derived::modify() => Base::modify(), but the next time I print, the value stored in the Base class is not modified (it is the original value).

class Base
{
   public:
            int value;
            void virtual modify(val) {value -= val;}
            int getValue() {return value;}
}
class Derived: public Base
{
    public:
            void modify(val) {Base::modify(val);}
}

void modifyAndPrint(Base obj)
{
     obj.modify(5);
     cout << obj.getValue(); // should print 9 -5 = 4
}

int main()
{
    Derived derivedObj(); //assume this initializes the object with a value, say 10

    derivedObj.modify(1);
    cout << derivedObj.getValue(); // returns correct value, 9 (10 - 1)

    modifyAndPrint(derivedObj); // does not print correct value, still prints 9
}

What am I doing wrong?

user2009750
  • 3,169
  • 5
  • 35
  • 58
  • 3
    -1 The code given is **not the real code**. It does not illustrate the problem. It can't be the real code because it has several syntax errors etc. – Cheers and hth. - Alf Dec 08 '13 at 20:55
  • Sorry, yes this was just a bit of pseudocode. The actual code is a little more complex, and part of a larger program, but I was attempting to capture the crux of the problem here. The answers below were correct, I didn't realize the object was being copied before I modified it. – user3080705 Dec 08 '13 at 21:33

2 Answers2

1

You're passing Base obj to modifyAndPrint, which means you're passing it by value - which means a copy of it gets made and given to the function. If you want the function to modify the original value, you have to pass by reference, ie. Base &obj. For further reading see Pass by Reference/Value in C++.

Community
  • 1
  • 1
  • First, if the syntax errors etc. are fixed, then this isn't the issue. But secondly, with the posted "like this" pseudo-code not being the real code, there's no telling what the issue is. It might even be what you say. – Cheers and hth. - Alf Dec 08 '13 at 20:56
1

C++ is value based: when you don't pass objects by pointer or reference, they are copied. The copy will have the static type declaredby tbe function, i.e., any derived part will by "sliced" off. You want to pass your object by reference:

modifyAndPrint(Base& obj) {
    ...
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • No, note that the problem is alleged to be that calling `modify` on the passed object does not modify that object locally. Nothing about modifying caller's object. But also note that the code given is not the real code. – Cheers and hth. - Alf Dec 08 '13 at 20:58
  • @Cheersandhth.-Alf: I can see what you are saying. When fixing the obvious errors in the posted code, it seems to behave as expected, actually... – Dietmar Kühl Dec 08 '13 at 21:12
  • This solved it, thanks very much! I was passing the object and it was making a copy, so I wasn't modifying the original object. – user3080705 Dec 08 '13 at 21:35