C++ for C programmers — lesson 1 (classes, constructors, methods, templates, is it efficient?)

21
3



Are you familiar with C, or with languages based on C? This video gives a quick introduction to C++: a very small core set of things that are new in C++ compared to C. [Subtitles/Captions available]

Topics covered, in order:
– Bidirectional linked list in C
– What is a class (it is a struct)
– Methods
– Struct is a type (typedef is not needed)
– Constructing and destructing
– Template classes
– std::list is already in STL (C++ standard library)
– performance impact (none at all)

Twitter:
Patreon: (alternatives at
Twitch:
Homepage:

Things I forgot to mention:
– methods (member functions) do not increase the size of the struct/class at all. They literally work just the same way as the original C code in this video works, even though they’re defined in between the struct’s { and }. Memory usage per class instance is not increased (and scalability is not hurt), no matter how many methods the class defines.

Topics intentionally omitted for clarity and brevity, even though they would be good style for this program:
– copy constructor / assign operators
– member initialization in constructor
– public and private
– std::string rather than const char*
– const references rather than T verbatim
– exception safety

It’s been a while since I last published videos containing English speech. I think I’ve taken a step backwards towards robotic speech, due to lack of practice. Sorry about that. English is still not my native language!

Same lesson in HTML format:

Nguồn:https://wijstaanvooronzegrondrechten.org/

21 COMMENTS

  1. sir, i have some questions 1) why char * works as a strings and char var[] works as array of strings and similarly char * works as array of strings ? i searched many ways but didn't got satisfied answer.
    2) which pc is best for all level programming (ram, intel's processor operating system etc) because i have to change my existing one it hangs quite well.

  2. Waiting for part 2, 3, etc. It would be excelent to make a "simple" (a room, a player and some stuff or objet) engine like quake, with real 3d in visual express c++ or c sharp, what do you think?

  3. Hi Bisqwit! It would be great to see the continuation of the series of these videos! And by the way, where can I get the Joe editor for windows, working in cmd?

  4. This is a brilliant demonstration. Maybe it was "cherrypicked" but upgrading to a template was astonishingly simple and powerful

  5. I don't use C++ because when you take away all of the features that I don't use, it's basically C.

    1. "using namespace" is considered evil, so why have namespaces? Use "_" instead of "::".

    2. RTTI is useless, so I turn it off with "-fno-rtti".

    3. Classes are just structs. No need for em.

    4. Rather than writing move/copy constructors for everything, why not just opt IN to construction using an "init" function or macro that constructs it like `#define foo_of(x, y) ((struct foo) { x, y })`.

    5. I avoid destructors, because it's sometimes unclear when they're going to be called. They cause more problems than they solve imo. Just look at unique pointers. Ugh. No thanks. Calling "free" isn't rocket science.

    6. If my naming convention is to label private members with a "m_" or "_" prefix, there's no point to using public/private for encapsulation outside of aggressively enforcing policies that are prone to change. Signaling intent is enough.

    7. Using public/private to control which methods are overridden with inheritance is stupid when you already have overloadable functions. In C, just encode part of the type information in the name, like "update_player(…)". This is how C++ does it, just crazier, like "_Z6updateC6playerv".

    8. I use templates as little as possible because they generate mountains of code, are slow to compile, and often difficult to read. Also, if I'm going to have to put code in my headers anyway (due to necessary inlining or linking issues), why not use macros and static functions? It works just as well as templates. Often, I'll use void pointers and function pointers like how bsort and qsort work.

    9. I avoid exceptions because it violates the high-level structure of the code. Everything becomes a potential point of error, including the object creation/destruction. I'd rather pass around an error callback or utilize a return value.

    10. I don't like type inference in a statically typed language. Some would make the case that you don't have to type the name twice when using a constructor, so you can do "auto foos = std::vector<foo>();", and these people apparently don't know that you can do "std::vector<foo> foos();".

    11. I don't like standard iterators, though the for-loop syntax is nice. It's MUCH easier imo to have a function that takes a "yield(value)" function as an argument. The code is waaaaaayy simpler.

    12. I don't like standard vectors. No arguments about quality or performance, just I have a better library in C that uses macros instead of templates and interoperates seamlessly with arrays.

    At this point, the only things I'm losing are the implicit lvalue / rvalue conversions you get from having references, scoped enums (enum classes), and initial struct values. These are things I can live without.

    Perhaps the biggest argument of all is that I absolutely HATE reading other people's C++ code. That's not a testament to bad programmers btw, it's a testament to bad syntax.

  6. I posted a comment here earlier regarding your implementation of the linked list and a question I had regarding it, but I deleted it because I figured it out. Basically, when I read your code I didn't see your constructor and thus I didn't see that you null-initialized the *first and *last pointers. Thus, I was confused when I read this line

    if(newnode->prev) newnode->prev->next = newnode;

    Since I didn't see the constructor and the null-initialization of the first and last pointers, I assumed this if statement would evaluate to true and then proceed to dereference garbage. All is good now, though!

  7. I would love to see a 'C for C++ programmers' video. Most of us who learned C++ often rely too much on the nifty features and algorithms of its standard library and we turn into babies whose pacifiers have been stolen when we use C without these abstractions.

  8. Thanks for it!. I'm learning C++ because most of the documentation of opengl is written with this. But I only know C.

    Why is necessary use auto in C++? Why isn't better just declare the real value of that variable?

  9. me after study my ass off of C and think to myself "now i finna understand something on that finnish guy's videos" came here, 20 secs, strong headaches, don't understand shit after 5 lines of code.

  10. Ey, bisqwit. Your windows fonts look really smooth. You can even notice it in the video. Is it because of the 4k screen and windows scaling options?
    I just found out your channel and it's awesome

  11. Well, dear Sir, that was the most gentle way to say "Aw, cut the crap already!" to the all the people telling that "C is soo much faster and efficient than C++". Even John Carmack admitted he didn't use C++ in the first place because he didn't knew better (source: http://fabiensanglard.net/doom3/interviews.php#qc++). Well played Sir, well played!

LEAVE A REPLY

Please enter your comment!
Please enter your name here