I’m very new to C++ and I’m currently learning it. I got a few questions..
-
What is the differences between void DoSomething(const Foo& foo) and void DoSomething(Foo foo)? If we don’t specify & then the instance of Foo will be passed by value ( not reference ). It will be the same as having const + & in argument except no checking at compile-time. So, Why does having const + & become the best practice over the argument without & and const?
In C#, passing the object is "by reference" but seems like it’s not in C++.
-
The book that I’m reading said that Member functions pass the implicit parameter by reference..
Could anyone give me the sample of implicit parameter and by reference? I know that if we want to pass the object by reference, we need to use & (e.g. Foo(Person& p) ) but how come C++ pass the object by reference for implicit parameter? I read that implicit parameter in C++ is like Contructor(string str) : strMemberVariable(str) {} …
-
Is the array the only that pass by reference in C++?
-
Why can’t I use Foo fInstance in Foo class?
Example:
class Foo {
public:
Foo() { }
Foo(const Foo& f) : fInstance(f) { }
Foo fInstance;
};
Thanks in advance.
There are several differences, in order of importance:
Foocannot be copied, you need to pass it by referenceFoois a base class, you should get it by reference so that users can call your functions with derived classesconstreference to itBy implicit parameter you should understand
this, that is the object itself. It is effectively passed by reference since you can modify its state in the member function.Following
Konrad‘s remark: note thatthisitself is not passed by reference,thisis a reference (pointer) to the object, but is passed by value. You can’t change the memory address of your object as you wish 😉They aren’t. You will see changes to the elements of the array, but the array (structure) will not change.
Following
FredOverflow‘s remark, an illustration:We don’t know what
fundoes, it will probably change some elements ofarray, but whatever its action,arraywill remain an Array of 15 integers: the content changes, the structure does not.As a result, to change
arraywe need another declaration:This way we can change both the content and the structure (and pass back the new size too). And of course we can only call this function with an array that was dynamically allocated.
Because that’s infinite recursion. Think about it from a compiler point of view, and try to guess the size of
Foo. The size ofFoois the sum of the sizes of its attributes, plus possibly some padding and type information. Also, an object size is at least1so that it can be addressed. So, ifFoohas aFoo, what’s its size 🙂 ?The usual solution is to use a smart pointer:
Because the size of a pointer does not depend on the size of the object pointed to, so there is not recursion going on here 🙂