Well I have a small question to solve a matter of mine (and maybe some others ^^). So let's assume the following in C++ :
class A {
public:
A() { }
A(const A& src) { }
};
class B {
public:
B() { }
void foo(A valuePassing) { }
};
int main(int argc, char* argv[])
{
A passed;
B obj;
obj.foo(passed);
return EXIT_SUCCESS;
}
In this code, who is responsible of calling A
's copy constructor
? The question could be generalized : when passing by value, who is responsible of the copy of the variable, the caller or the callee? In that specific case, will B
call the copy constructor when receiving the var or is it main()
which will call it and send the copy over to B
's method?
My first thought is that, to maintain the protection induced by C++ accesibilities, it should be the caller doing the copy job. But I need to be sure of it. And is it common to every compiler (don't see why not but differences between compilers are sometimes so weird)? Namely VC++ and gcc.
Thanks in advance for your answers ;)
Edit:
If anyone wonders, this is about implementing the Pass Key pattern created by Georg Fritzsche. Indeed, as some stated, making it non-copyable is a good idea to prevent not too smart developers from making a function that gives the key to anyone from the class that is granted the key. But making it non-copyable also prevents standard value argument passing (at least it should, I have found out that Visual C++ compiler does not follow the standard and is actually ok with that...)
My idea to solve the problem is to create a base PassKey
class that has protected copy constructor and assignment operator. Then every PassKey
classes will inherit from this one. Then the function that require the key to be used ask as first parameter the key by value. This way even if a smartass developer do the previously mentioned function and gives a reference to the key, the external class that will try to use it won't be able to because it needs access to the key's copy constructor
which is accessible only to the protected and granted classes.
Was I clear enough or do that need some clarification? :S