Current Usage of Copy and Move Constructors

I was following a course from Udemy. But I lost my interest in the c++ language 3 months ago because of these copy and move constructors. As far as I know, they are used with pointers and in classes. It's hard for me to learn them and probably because of the instructor's methods and techniques.

The question is, are they still common things in C++? Can I pass through them? A lot of people say not to use pointers but should I learn it?

I'm so confused right now and we will try to learn java and c sharp in college this year. So I have to be precise about it. I love C++ in general and I'm willing to try the Unreal Engine and beyond. Before I tried a third-party library SFML and it was quite interesting and exciting for a beginner.
Copy and move constructors are constructors. What is the purpose of a constructor? To initialize object when it is created. User defined types have constructors. User defined types are classes. You can't redefine "constructor" of simple type, like double.

RAII (Resource Acquisition Is Initialization) is a programming idiom that is often recommended for C++, Java, and C#. The important point is that variables are assigned known value already when they are created.

Have you seen "linked list" examples? Many have a bit like this:
1
2
3
4
Node* p = new Node;
p->data = data;
p->next = root;
root = p;

However, the Node is a user defined type and can have custom constructor.
With help of such the above could be written:
root = new Node( data, root );
This part of code is simpler, because the p->data = data; p->next = root; is neatly in constructor.


The copy and move constructors initialize the new object with value of same type. Since such copying is very common, compiler generates them automatically, except in certain special cases. That version is just a simple "shallow copy".

1
2
3
4
5
int x = 42;
int* y = &x;
int* z = y;
int* w = new int {*y};
delete w;

Both z and y point to x. You can modify value of x via both pointers. The z is a "shallow copy".

The w does not point to x. It points to a different int that also has 42 as initial value. The w is a "deep copy", because also the pointed to object is a distinct copy.

You need to implement copy and/or move constructors only if your class is such that the default shallow copy is not appropriate.


The difference between "copy" and "move" is that if you move construct, the original is not kept intact; the move is just an optimization for situation, where you have to create a copy but don't actually need the original after that.


Modern C++ has in its Standard Library many custom types and functions. When you use them, you don't need to have pointers in your code explicitly. Those custom types probably do have pointers internally, do have custom copy and move constructors, and so forth, but we don't have to worry about those details.

Is it good to understand pointers? Yes.
Are they beginner material? Not in modern C++.
bydrachen wrote:
A lot of people say not to use pointers but should I learn it?

It's generally a good idea to avoid pointers if possible. People coming from other languages often overuse pointers when they start to learn C++. But sometimes you might have to use pointers. Even with "smart pointers" you often need to implement a copy constructor to handle copying correctly.

keskiverto wrote:
the move is just an optimization for situation, where you have to create a copy but don't actually need the original after that.

Old versions of C++ didn't have move constructors. There are some use cases where they are not just an "optimization" (e.g. when implementing a "move-only class") but most of the time you don't have to implement a move constructor. If you don't define a move constructor, and it's not implicitly generated, then it just means it will fall back to using the copy constructor instead.
Last edited on
... are they still common things in C++?
Yes, but often indirectly.

Can I pass through them?
Yes, to begin with.

A lot of people say not to use pointers but should I learn it?
It is worth learning them eventually, but there's so much to learn when starting C++, it's worth ignoring them for a while.

It's worth noting the following:
* C++ is a performance language, and allows precise control of its environment. If you don't care about performance, there are many higher level languages and domain specific languages that are more flexible and easier to use. C++ is necessarily the way it is.

* Type is really important, and C++ start off as C language with user defined types. This means the language helps out with type checking and conciseness. These constructors are part of defining how a type behaves under copy or move. Move is an optimization under user control.

* Learning how to use an implementation without an introduction to the concept is alway painful and feels pointless and something to be avoided. Pointers are a good example of this. It's probably best to understand the idea of "indirection" in computer science first. There is Butler's quote, "All problems in computer science can be solved by another level of indirection". Once you understand the concept, the implementation should make more sense. The wikipedia page on indirection worth reading.
Last edited on
I was following a course from Udemy.


The best free on-line C++ learning resource is:
https://www.learncpp.com/

For a book, I suggest:
Beginning C++20: From Novice to Professional by Ivor Horton
https://www.amazon.co.uk/Beginning-C-20-Novice-Professional/dp/1484258835/ref=sr_1_9

It's hard for me to learn them and probably because of the instructor's methods and techniques.


Why? What are the issue(s)? What are you having difficulty with?
It's hard for me to learn them and probably because of the instructor's methods and techniques.

C++ is complex and unforgiving with a very steep learning curve. To rely on a single source is not beneficial.

seeplus mentioned two good sources for learning C++, one free and online.

The book is worth the price -- Amazon US link: https://www.amazon.com/gp/product/1484258835/ -- there is also a github site that has all the example/exercise code available for download: https://github.com/Apress/beginning-cpp20

I've been self-learning C++ for years and decades. Started before C++98 was standardized. Having free online resources like Learn C++ and cppreference are recent additions I wish had been available way back then.

I don't know what your OS or compiler are, but if you are Windows I suggest using Visual Studio 2022. The Community edition is free for individuals.

https://visualstudio.microsoft.com/vs/community/

VS 2022 requires x64 Windows, if you have x86 (32-bit) Windows you can install VS 2019.

https://apps.microsoft.com/store/detail/visual-studio-community-2019/XP8CDJNZKFM06W

One thing to note if you get Visual Studio, 2022 or 2019, the C++ workload is not default installed. It has to be manually selected. Either on initial install or modifying a current install.

There are two game-related optional workloads also optionally available. One that uses the Unity game engine; the other DirectX, Unreal and/or Cocos2d.

Another reason to get Visual Studio, it is currently the only compiler that is 100% C++20 compliant.

Good luck, keep on pushing past the frustration, write code until your fingers bleed. :Þ

What used to be "I don't understand" should one day will become clear. :)
Last edited on
Topic archived. No new replies allowed.