Why should the copy constructor accept its parameter by reference in C++?

In summary: UI code to more complex languages without first understanding basic concepts such as passing parameters by value or reference. As you continue to learn and explore these languages, it is important to understand these fundamental concepts before tackling more complex topics like copy constructors.
  • #1
shivajikobardan
674
54
TL;DR Summary
copy constructor
Technology news on Phys.org
  • #2
Not an expert in C, but the top answer simply says this:

1675358045333.png
 
  • Like
Likes pbuk, aaroman, jedishrfu and 1 other person
  • #3
shivajikobardan said:
They just throw a big wall of text. I
This is an incredibly weak criticism. If you had specific complaints, we could maybe address them, but "too much trouble for me to read" is on you, not us.

Your options are call by reference and call by value. Call by value works by creating a copy. Your can't do that without a copy constructor, so you can''t do that when writing a copy constructor.
 
  • Like
Likes Dale, phinds and Wrichik Basu
  • #4
Wrichik Basu said:
Not an expert in C, but the top answer simply says this:
But why? Can you explain this via this code (or any code of your choice)?
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Complex
{
private:
    int real;
    int img;

public:
    Complex()
    {
        real = img = 0;
    }

    Complex(int r, int i)
    {
        real = r;
        img = i;
    }

    Complex(Complex &c)
    {
        real = c.real;
        img = c.img;
    }

    void putComplex()
    {
        cout << real << "+i" << img<<endl;
    }
};
int main()
{
    system("cls");
    Complex c1;
    Complex c2(3, 5);
    Complex c3(c2);
    c1.putComplex();
    c2.putComplex();
    c3.putComplex();

    return 0;
}

instead of writing complex &c i f I wrote complex c, what difference would've occured?
 
  • #5
shivajikobardan said:
instead of writing complex &c i f I wrote complex c, what difference would've occured?
Have you tried putting a few print statements inside the constructors and then running the program yourself?
 
  • #6
Wrichik Basu said:
Have you tried putting a few print statements inside the constructors and then running the program yourself?
https://pythontutor.com/visualize.html#mode=edit
I've even tried this visualizer. Once I remove & the code doesn't run at all. Neither does the visualizer works. That's why.
 
  • #7
Minor point, but copy constructors aren't relevant to C. They are relevant in C++, so I've edited your thread title.
 
  • Like
Likes jim mcnamara
  • #8
shivajikobardan said:
I've read these answers, but most of them aren't satisfactory. They just throw a big wall of text. I get they're trying to say something about infinite recursion, but I'm failing to get proper explanation for it with diagrams, or even code.
The very first paragraph at the page you linked to says this:
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...
Hardly a wall of text, but what it's saying is that there is a difference in C and C++ between parameters that are passed by value and those that are passed by reference. Before you can understand relatively complex topics such as copy constructors in C++, you need to understand these more basic concepts.

I'm somewhat surprised that you have jumped from writing UI code that uses CSS and HTML and whatever into much more complex languages. You need to crawl before you can walk, and you need to walk before you can run.
 
  • Like
Likes Wrichik Basu and Vanadium 50
  • #9
A few point:
  • Why do you want the components of your complex numbers to be integers and not floating point?
  • Why do you not want to take advantage of the scope resolution features of the language? (e.g. Complex::Complex, and using namespace std.)
  • If you are wondering what would happen if you made a change to the code, try it! It has got to be faster than asking us. I suspect it will throw a compiler error, but try it.
 
  • #10
Write down a long random number on a piece of paper. Now try copy this number to another piece of paper without letting your finger slide over digits of the first number or event looking at them. Impossible right? You need to look at something (i.e. refer to it) in order to copy it.

Alternatively, in case you have a grasp on the difference between a value and a pointer to a value, then know that in C++ a reference can be thought of as a pointer that simply always have to point at something (it can't, like pointers can, legally point at nothing).
 
  • Like
Likes jim mcnamara, Dale and Wrichik Basu
  • #11
Mark44 said:
I'm somewhat surprised that you have jumped from writing UI code that uses CSS and HTML and whatever into much more complex languages. You need to crawl before you can walk, and you need to walk before you can run.
I must say you're underestimating css.
 
  • #12
Vanadium 50 said:
  • If you are wondering what would happen if you made a change to the code, try it! It has got to be faster than asking us. I suspect it will throw a compiler error, but try it.
It throws compiler error.
 
  • #13
Correct me if I'm wrong:
In int main, "complex c3(c2)" creates a copy of c2 and thus passes c2 to copy constructor.

In copy constructor, "complex(complex c)" copy constructor takes c=c2.

So what's the issue with this?

hello guys, please focus on the issue rather than other things. help me solve and learn it.
 
  • #14
shivajikobardan said:
It throws compiler error.
As it should. Do you understand the error message? Do you understand why it is better to say "The compiler gave an 'incompatible levels of indirection' (or whatever it said) error than just 'it throws a compiler error'.?
 
  • Like
Likes Dale
  • #15
Vanadium 50 said:
As it should. Do you understand the error message? Do you understand why it is better to say "The compiler gave an 'incompatible levels of indirection' (or whatever it said) error than just 'it throws a compiler error'.?
that's my question. you can try in compiler yourself.
 
  • #16
shivajikobardan said:
I must say you're underestimating css.
I know a little bit about CSS and a whole lot about C and C++. IMO C, and even moreso C++, are vastly more complex than CSS. So, no, I don't think I am underestimating the complexity of CSS.
 
  • Like
Likes aaroman and Vanadium 50
  • #17
Vanadium 50 said:
As it should. Do you understand the error message? Do you understand why it is better to say "The compiler gave an 'incompatible levels of indirection' (or whatever it said) error than just 'it throws a compiler error'.?

shivajikobardan said:
that's my question. you can try in compiler yourself.
@shivajikobardan, you completely missed the point of @Vanadium 50's comment. He already knew that defining a copy constructor the wrong way (i.e., without a reference to the object being copied) would cause a compiler error. His point was that it's better to specify exactly what the compiler error was than to merely say "it throws a compiler error."
 
  • Like
Likes Vanadium 50
  • #18
shivajikobardan said:
I must say you're underestimating css.
I think you are grossly overestimating CSS compared to actual coding languages.
 
  • Like
Likes Wrichik Basu and Mark44
  • #19
shivajikobardan said:
Correct me if I'm wrong:
In int main, "complex c3(c2)" first creates a copy of c2 and thus after that passes this copy of c2 to copy constructor.

In copy constructor, "complex(complex c)" copy constructor takes c=c2.

So what's the issue with this?
I corrected what was wrong above in red, and made more explicit what is happening.

The issue with this is the "first creates a copy" part, which by itself would call the copy constructor, which would require creating a parameter copy first, which would call the copy constructor, which would require creating a parameter copy first... and so on.

In theory this could be solved internally by not using the user defined copy constructor in this special case for the parameter to the user defined copy constructor itself, but rather some default copy constructor. But why bother introducing such special cases and exceptions, just to allow something that is inefficient anyway? You should always pass by reference to avoid unnecessary copies.
 
Last edited:
  • Like
Likes .Scott and shivajikobardan

1. What is a copy constructor in C++?

A copy constructor is a special type of constructor in C++ that is used to create a new object by copying the values of an existing object. It is invoked whenever a new object is created from an existing object, such as when passing an object as a function parameter or when assigning one object to another.

2. Why is it important for the copy constructor to accept its parameter by reference?

The copy constructor should accept its parameter by reference in order to avoid unnecessary copying of objects. If the parameter was accepted by value, a copy of the object would be created, resulting in additional memory usage and potentially affecting the performance of the program.

3. What is the difference between passing by reference and passing by value?

Passing by reference means that the function parameter is a reference to the original object, and any changes made to the parameter will also affect the original object. Passing by value means that a copy of the object is made and any changes made to the parameter will not affect the original object.

4. Can the copy constructor accept its parameter by value in C++?

Technically, yes, the copy constructor can accept its parameter by value in C++. However, it is not recommended as it can lead to unnecessary copying of objects and potentially affect the performance of the program. It is best practice to always accept the parameter by reference.

5. Can the copy constructor be used for objects of all classes in C++?

Yes, the copy constructor can be used for objects of all classes in C++. However, it must be explicitly defined by the programmer. If no copy constructor is defined, the compiler will create a default copy constructor that performs a shallow copy, which may not be suitable for all classes.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
7
Views
10K
  • Programming and Computer Science
Replies
3
Views
319
  • Programming and Computer Science
Replies
7
Views
2K
Replies
9
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Special and General Relativity
Replies
29
Views
1K
Back
Top