Avatar ·

Why is it better to pass objects by pointer in C++ rather than by value?

Hi everyone! I just started learning C++ after Java, and I have some confusion with pointers.\n\nIn Java, when I write `MyClass obj = new MyClass()`, I always work with a reference to the object. But in C++, I can write both `MyClass obj;` and `MyClass* obj = new MyClass();`. And here I get lost.\n\nLet's say I have a function that takes an object and does something with it:\n\n```cpp\nvoid processData(MyClass data) {\n // modify data\n data.setValue(42);\n}\n\nint main() {\n MyClass myObj;\n myObj.setValue(10);\n processData(myObj);\n // myObj.getValue() is still 10, not 42! Why?\n return 0;\n}\n```\n\nI expected that after calling `processData` the value would change, but it didn't. I had to rewrite it with pointers:\n\n```cpp\nvoid processData(MyClass* data) {