How to output a map from a class with operator overloading

Pages: 1234
Sometimes its hard to figure out why a certain way of doing things is useful, or most importantly, when it should be used and how.


That comes from experience and knowledge!
even though im only learning C++ to make a game


You might be interested in:
Beginning C++ Through Game Programming (4th edition) by Michael Dawson
https://www.amazon.co.uk/Beginning-C-Through-Game-Programming/dp/1305109910/ref=sr_1_15
AWESOME! thanks for that link im going to check that out right away.
I do own the book, purchased in 2017, for learning some of the basics of C++ at the time of the book's writing it is decent.

That book has a copyright of 2014, it is very much out of date regarding C++. C++ has changed a lot since C++11 was standardized, though. And will get more out-dated as time goes on.

A US Amazon link for the book:
https://www.amazon.com/gp/product/1305109910/

A more recent (2019) C++ game programming book available on (US) Amazon:
https://www.amazon.com/Beginning-Game-Programming-program-building/dp/1838648577/

(UK Amazon link:https://www.amazon.co.uk/Beginning-Game-Programming-program-building/dp/1838648577/)

I can't say if it is worth getting, I haven't purchased any Packt books before.

Learning C++ by creating games doesn't normally utilize all of the C++ toolbox, the language has a LOT of features. One of the biggest drawbacks to this book's approach is using C library functions for generating random numbers. C++ has the <random> library.

If you are using a recent compiler and compile as C++17 or later, I do suggest you do so, there is a C++ algorithm function that was deprecated in C++14 and removed in C++17. std::random_shuffle.

https://en.cppreference.com/w/cpp/algorithm/random_shuffle

Update on the Packt book: the publisher has a special deal for an online/eBook version. US$5.

https://www.packtpub.com/product/beginning-c-game-programming-second-edition/9781838648572

M'ok, so I purchased it, downloaded the eBook in epub and pdf. There is also a github link the source code from the book:

https://github.com/PacktPublishing/Beginning-Cpp-Game-Programming-Second-Edition

The book looks to be mainly for graphical C++ games, on Windows using SFML. Well, not exactly console based C++ as the other C++ game programming book, for $5 it's not a bad deal.
A more recent (2019) C++ game programming book available on (US) Amazon:


Yes - but that book uses the SFML 3rd party library for multi-media
https://www.sfml-dev.org/
it is very much out of date regarding C++


Yes. Many decent C++ books published in the past are now out-of-date regarding usage of C++. Look no further than the celebrated books by Sutter and Meyers (the Exceptional and Effective series). Even the books by Stroustrup (except the Tour book) are now out-of-date with no updates expected in the foreseeable future (if at all). Writing a book is hard and time-consuming work and for a C++ book, won't make you a millionaire. For those select few who do manage to write a 'good' C++ book, they may 'move-on', get other interests, decide the rewards don't justify the time spent etc etc. So their book isn't updated. That's not to say that these 'older' books are necessarily bad, but the reader should be aware the C++ syntax etc used may in places be out-of-date (eg std::random_shuffle). IMO their usage should be supplemented by an up-to-date C++ book.
Thanks for the links, Ill check that book out for sure, and thats ok that its using the SFML library, cause thats the graphics library im going to be using. I have it already setup in VS 2022, although i somehow messed it up and have to set it up again. Ive used SFML for years but never really built anything with it but i am familiar with it.
seeplus wrote:
that book uses the SFML 3rd party library for multi-media

I did mention that at the end of my post.

One really cool thing showed up when I tried out the first exercise of the book. Since I use vcpkg to manage 3rd party library installs with Visual Studio 2022, I had installed SFML a while ago, I didn't need add any SFML directories into the project's settings as shown in the book's text. I simply added the required SFML #include and vcpkg silently did all the includes as needed for the project. VS now recognizes any installed 3rd party library as if it were a VS native header, auto-complete drop-down shows the possible headers available, etc.

@Ch1156, I really suggest you look into getting and using vcpkg. It is a library package manager that makes getting and updating 3rd party libraries MUCH easier. You can even integrate the installed packages into VS so you don't have to worry about adding library paths to your project. Updating an already installed library is just a couple of console commands.

https://vcpkg.io/en/index.html

You don't need to have Visual Studio to use vcpkg. If you know how to use cmake you can use the package management features.

If you have any questions about vcpkg please ask. :)

A post I made about why I like and use vcpkg:

https://cplusplus.com/forum/lounge/284973/

Re: updating books.....

One nice feature of leanpub eBooks is once a book is purchased any updates made are free. Updating an eBook is slightly easier than one destined for a dead-tree edition. Several of my purchased 100% completed eBooks have had updates since my purchase.

leanpub has books for purchase that are still "work in progress", at a decent price depending on how complete the book is.
Sorry for the late reply, i was very sick. Yeah ill check out that plugin, i really hate updating and manually adding the paths to my project, every time i want to make a new SFML project i have to set it all up, so any plugin that can make that simple is worth its weight in gold to me.

I have been reading through the learncpp site and taking notes, but I would like to skip to pointers before going back to where i left off and continuing my studying. I know pretty much most of the stuff ive been reading so far. Id like to see a small program that uses pointers. Im trying to write one but im a little bit stumped as to what exactly I could write that needs them so id like to ask for some help with that. Id like the program to have a practical real world use of a pointer, and not one that just uses one just to illustrate. If i can see how pointers are genuinely used in programs, I feel i can learn better that way on how i can use them, but I dont want to look at other peoples code just yet, as i feel that would be a little too overwhelming just yet.

does my original code need any use of pointers? Can i see some examples of maybe a few small classes that use them? I understand what pointers are and what they do, i just have never used them and am unsure how they would be used in practicality.
Last edited on
Well the often-used 'example' in classes is to write your own string class (and without using cstring functions).

Pointers are used extensively with polymorphic classes. However in modern C++, managed memory (std::unique_ptr, std::shared_ptr/std::weak_ptr) are preferred.

https://en.cppreference.com/w/cpp/memory

Note. You may come across std::auto_ptr in some older C++ code. This has now been superseded by unique_ptr/shared_ptr. You can't now use auto_ptr since C++17.
About pointers:

https://www.learncpp.com/cpp-tutorial/introduction-to-pointers/

Pointers are a very powerful programming tool, ones that require very precise handling or memory can easily get messed up during run-time.

https://www.learncpp.com/cpp-tutorial/dynamic-memory-allocation-with-new-and-delete/

C++ introduced Smart Pointers, as seeplus pointed out, to handle the messy details of memory management for the programmer. It is still possible to make a mess of memory management using smart pointers but it is a lot harder.

I suggest you read all of Learn C++ Chapter 9 -- honestly reading ALL of Learn C++ wouldn't be a bad investment of your time -- there's a lot of useful and related information in the chapter. Pointers and references are different yet related C++ critters you need to know about.

https://www.learncpp.com/cpp-tutorial/introduction-to-compound-data-types/
yeah im going through the entire site and taking notes. Got some good notes, about 40 pages worth so far and im only on chapter 6.3. Some of this stuff makes watching paint dry seem fun.
So im reading the end of chapter 8, and i have a question about templates

https://www.learncpp.com/cpp-tutorial/function-templates-with-multiple-template-types/

Its the "Abbreviated function templates" part at the very end. So basically its saying that if i want my template to have more than one type i should just do something like this:

1
2
3
4
auto max(auto x, auto y)
{
    return (x > y) ? x : y;
}


Instead of typing out this?:

1
2
3
4
5
template <typename T, typename U>
auto max(T x, U y)
{
    return (x > y) ? x : y;
}
The two bits of code define the same function template, so there's no reason to type out the longer version.
Last edited on
auto function param was brought in with C++20 as part of concept support. A concept can precede auto to restrict the type. eg

1
2
3
auto max(std::integral auto x, std::integral auto y) {
	return (x > y) ? x : y;
}


requires that x and y must be integral type.
Using std::integral as a parameter designation can lead to some rather weird looking code, though it is perfectly normal and legal C++:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <concepts>

auto add(std::integral auto x, std::integral auto y)
{
   return x + y;
}

int main()
{
   std::cout << add(0, 'a') << '\n';
}

Change the 'a' to Boolean true and it still compiles, the output is displayed as being one.

Using a core concept descriptor with multiple parameters/single type is not as abbreviated as when having differing types, but it is IMO less cluttered looking than the full template she-bang.

C++20 and auto makes for some nice coding. :)
1
2
3
4
5
6
7
8
#include <concepts>

namespace ext
{
    constexpr decltype(auto) max( auto&& x, auto&& y )
        requires std::common_with< decltype(x), decltype(y) > && requires { x<y ? 0:0 ; }
    { return x<y ? y : x ; }
}
Yow, that there is one mouthful.
Topic archived. No new replies allowed.
Pages: 1234