Reputation: 73
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
Upvotes: 0
Views: 924
Reputation: 141
Well, how class B can invoke a function by itself? You have just two places for copy constructor calling: main() and foo(). And I can't see any calls from foo, so the answer is obvious. And don't forget about return type of foo().
Upvotes: 0
Reputation: 1825
Now I'll answer as general as I am able.
The short answer is that it is your compiler's job to call the constructor. It will as far as I know also be constructed by the caller. Exactly how this is done, however, may depend on the calling convention you are using.
There exists a number of calling conventions, one compiler may support more than one, see for instance this page at MSDN. And different architectures may demand a specific convention.
For x86, one calling convention would be like YePhIck answered, whilst on for instance on an ARM platform, the address would be placed directly in a register before the branch to the function. And not pushed to the stack (unless you have more than 4 parameters).
Can it ever be the callee's responsibility to copy the object? I'm not aware of such a calling convention, but it might exist.
Upvotes: 0
Reputation: 5856
obj.foo(passed);
will result in the following sequence of steps, generated by a compiler:
obj
's pointer onto stack frame (the this
parameter)temp
variable (this is where the copy constructor is being called)temp
onto stack framefoo()
Upvotes: 2