• Advertisement
Sign in to follow this  

Hi some quick advice needed on Copying Prevention!

This topic is 2638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI!

Sorry to bother you all, I've come across this latest thing and I've forgotten one or two things it appears! Check out this code:

class NoCopy
{
public:
NoCopy(int);

private:
NoCopy(const NoCopy&);
NoCopy& operator = (const NoCopy&);
};

void aFunc(NoCopy){};
void anotherFunc(const NoCopy&){};

int main()
{
NoCopy a(12); // Compiles - OK
NoCopy b(a); // rejected due to copy constructor - OK
anotherFunc(a); // passes by reference so no construction - OK
a = b; // rejected due to operator overload being private - OK

NoCopy c = 12; // 1 apparently this invokes an implicit copy constructor? - STUCK
aFunc(a); // 2 rejected due to pass by value? - STUCK
aFunc(12); // 3 another implicit copy constructor? How? - STUCK
anotherFunc(12); // 4 complies but I've no idea why - STUCK

return 1;
}


So my questions are how do the last four make any sense? I've given the reasons they fail as stated by the author in the comments.

1)NoCopy c = 12; - completely forgotten how this works. I am also confused as to how we can have any sort of implied copy contructor when we've explicitly made one inside the class and made it private. I thought that was enough to prevent the compiler from ad-libbing.

2)Hmmm... I guess here that passing by value invokes a constructor call to make a temporary object. Am I correct?

3)No idea how this can invoke an implied constructor called. Also no idea how an integer value can substitute for a class in an argument to a function?

4)Again as above in point 3. Can see how passing by reference prevents the constructor call to a temporary but not how an integer can replace a class in an argument function call.

Any help anyone can offer would be most appreciated!! Cheers! ;o)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by adder_noir
1)NoCopy c = 12; - completely forgotten how this works. I am also confused as to how we can have any sort of implied copy contructor when we've explicitly made one inside the class and made it private. I thought that was enough to prevent the compiler from ad-libbing.
If you don't want your constructor to be called implicitly, you need to write:
explicit NoCopy(int);
Quote:
2)Hmmm... I guess here that passing by value invokes a constructor call to make a temporary object. Am I correct?
Yes, pass-by-value is pass-by-copy.
Quote:
3)No idea how this can invoke an implied constructor called. Also no idea how an integer value can substitute for a class in an argument to a function?
Your constructor takes an int. The constructor is being implicitly called as in case number (1) to construct a temporary object. That object is then being passed-by-value, which should create a compiler error (due to copying being private).
Quote:
4)Again as above in point 3. Can see how passing by reference prevents the constructor call to a temporary but not how an integer can replace a class in an argument function call.
As above, the constructor is implicitly executed to construct a temporary object. That object is then being passed-by-const-reference.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement