answersLogoWhite

0


Best Answer

No. The parameter of the copy constructor is always passed by reference, specifically, a const reference to the class.

Think about it. If it were passed by value, then the compiler would already know how to copy the object into the formal parameter, but the purpose of the copy constructor is to provide the code to copy the object, so its kind of a "cart before the horse" thing to think about call by value here.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can the parameter of the copy constructor be passed by value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why not sending arefrence to copy constructor will cause an infinite loop?

A copy constructor gets called any time an object needs to be copied. Unlike in some of the newer languages like Java, you can chose to pass objects either by reference or by value. When you pass by reference, only the address of the function is copied. However, if you pass by value, the whole object must be copied. In order to copy the object, the copy constructor will get called. If the copy constructor's parameter is not a reference, then the object will get passed by value. When the object gets passed by value, it needs to get copied so it will call the same copy constructor. Because the object is still being passed by value it'll call itself again to create a copy of itself. The copy constructor will continue to call itself until the stack overflows.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


Does Java support copy constructor?

No. Java does not support copy constructor


Why in copy constructor in c you use pass by reference?

Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on... (You would have infinite recursion because "to make a copy, you need to make a copy".)

Related questions

Why not sending arefrence to copy constructor will cause an infinite loop?

A copy constructor gets called any time an object needs to be copied. Unlike in some of the newer languages like Java, you can chose to pass objects either by reference or by value. When you pass by reference, only the address of the function is copied. However, if you pass by value, the whole object must be copied. In order to copy the object, the copy constructor will get called. If the copy constructor's parameter is not a reference, then the object will get passed by value. When the object gets passed by value, it needs to get copied so it will call the same copy constructor. Because the object is still being passed by value it'll call itself again to create a copy of itself. The copy constructor will continue to call itself until the stack overflows.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.


One of strategy in programming a form of parameter transmission in which a copy of value of a parameter is sent to procedure...what is it?

pass by value


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


Does Java support copy constructor?

No. Java does not support copy constructor


Why in copy constructor in c you use pass by reference?

Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on... (You would have infinite recursion because "to make a copy, you need to make a copy".)


Discrbe breifly about Copy constructor overloading?

A copy constructor usually refers to a constructor which takes an object, and returns a copy of that object. I can think of no way to overload the constructor without changing its functionality.


What is the advantage of user-defined copy constructor?

What is the advantage of user-defined copy constructor


How do you declare constructor in cpp?

More or less as you would any other function, except there is no return type (not even void) and an initialisation list can be placed between the declaration and the definition that follows it. The initialisation list allows your constructor to call base class constructors besides the default constructor as well as initialise member variables according to the parameters passed to your constructor. The constructor's name must be the name of the class. Note that if you don't declare any constructors, the compiler generates both a default and copy constructor. If any constructor is declared you lose the default constructor unless you declare one yourself. The copy constructor is always present but must be overridden if your class contains pointers to memory allocated to the class itself. If you don't, the compiler generated copy constructor will perform a member-wise copy of the member variables, resulting in a shallow copy of the pointers themselves, rather than a deep copy of the memory they point to. The copy constructor must accept a constant reference to the same class of object.


What is the parameter Y in copy y stands for?

The parameter Y in copy y stands for any parameter along the y intercept.


Difference between pass by value and pass by reference?

When you pass by value you pass a copy of the name. That is, the function's parameter is assigned the value of the name you pass. If the function parameter is a class type, then that class' copy constructor is called, automatically, which assigns the name's member value(s) to a new instance of the class. The function parameter is temporary, it has local scope, and will therefore fall from scope when the function returns. Thus any changes made to the local name within the function have no effect on the name you passed -- they are completely separate names. When you pass by reference you pass the name itself. To be precise, you pass the memory address of the name, its reference. This address is assigned to the function's reference parameter. Thus any changes made to the reference parameter's value will change the name you passed, since they both refer to the same memory address. Passing by reference is the most efficient method of passing objects (instances of a class) to functions as there is no overhead in copying the object. And if a reference parameter is declared constant then you can be assured the function will not alter the immutable members of the object (while there are ways around this, if a function really intends to alter a reference parameter then it should not be declared constant in the first place). Pass by value should only be used when the value is a primitive data type (generally equal to or smaller than a pointer), or when the function needs to alter the value but must not alter the name that was passed. Note that pointers are always passed by value, but since they can store a memory address they can be treated just as if they were references (assuming the pointer is non-zero of course). When passing pointers, the pointer and the address being pointed at can both be independently declared constant (that is, either, both, or neither can be constant). To pass a pointer by reference you must pass a pointer-to-pointer type, which is itself passed by value. This allows the function to not only modify the address being pointed at (making it point to another address), but to also modify the value at that address, just as if you'd passed the value by reference.


How do you pass an array to a copy constructor in c plus plus?

You cannot pass an array to a copy constructor. A copy constructor only accepts a constant reference to the object being copied, which must be of the same class as the object being constructed. An array is not an object of any class, and therefore cannot be used in any copy constructor. Although you cannot pass an array to a copy constructor, you can pass an array to a non-trivial constructor. It is not recommended, however, as there's no way to bounds-check the array being passed, which could result in an invalid object being created -- which is never a good thing. Even if you pass the array and its dimension(s) to the constructor, how can you guarantee those dimensions are valid for the array being passed? And what will you do if they are invalid? After all, you cannot veto the construction of an object once you've called its class constructor. Not knowing why you want to pass an array to a copy constructor, or how you intend to initialise the members via an array, makes it somewhat difficult to determine the best solution for you. However, I would consider using mutators instead of constructors. There's still the problem with bounds-checking but at least you won't have to deal with it during the object's construction.