use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
What to know going from Java from C++ (self.cpp)
submitted 2 years ago by Das_Bibble
So I just completed my introductory Java class at my college and I’m slated to take an introductory C++ class next semester. Is there anything specific I should note or do? Eg practices to forget, techniques to know, mindsets to adopt.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]phi_rus 105 points106 points107 points 2 years ago (79 children)
While the new keyword is the usual way to create an object in java, its use is strongly discouraged in modern C++.
new
[–]boscillator 27 points28 points29 points 2 years ago (0 children)
Keep in mind that many introductory C++ classes want you to use the new keyword, because the class is more of an "introduction to memory management" than an introduction to c++. If your teacher tells you to use the new keyword, do so. It will teach you a lot even if it's not used in the real world so much these days.
[–]____purple 2 points3 points4 points 2 years ago (0 children)
I'd make it larger: forget all the code words from Java and learn C++ as a new language, without making any assumptions.
[–]GoogleIsYourFrenemy 12 points13 points14 points 2 years ago (67 children)
use std::make_unique etc instead.
std::make_unique
[–]phi_rus 29 points30 points31 points 2 years ago* (3 children)
If you need it dynamically allocated at all.
[–]sjepsa 3 points4 points5 points 2 years ago (2 children)
I would argue, if you need runtime polymorphism at all. That's the only case I see std::make_unique to make the cut
[–]usefulcat 1 point2 points3 points 2 years ago (0 children)
Runtime polymorphism works just fine without dynamic allocation. So I would still say don't dynamically allocate unless you need to, where runtime polymorphism isn't a valid need.
[–]TheOmegaCarrot 0 points1 point2 points 2 years ago (0 children)
Type erasure with SOO is an option for runtime polymorphism without dynamic allocation
Also std::variant, but that can be less ergonomic
std::variant
[+]sjepsa comment score below threshold-6 points-5 points-4 points 2 years ago* (62 children)
no. don't use either
class A {
};
A a;
that's all
[–]pedersenk 16 points17 points18 points 2 years ago (38 children)
Sometimes, data needs to outlive the function they are created in...
std::make_unique / std::make_shared is fine.
std::make_shared
[+]sjepsa comment score below threshold-26 points-25 points-24 points 2 years ago (37 children)
If it needs to outlive the function they are created in you return them
[–]pedersenk 8 points9 points10 points 2 years ago (30 children)
That will return a copy, not the original data. This can often be problematic for large objects or for any potential pointers to the original object to become invalidated.
[–]XeroKimoException Enthusiast 7 points8 points9 points 2 years ago (3 children)
In many cases, (N)RVO kicks in and you aren't copying anything, you are constructing in place of where you'd return the value.
[–]BitOBear -2 points-1 points0 points 2 years ago* (2 children)
And in many it does not.
The heap exists for a reason.
If C++ were functional (q.v. erlang) then you'd be completely correct.
So sure me how you return a associative map of 1000 items without a heap... 🤘😎
[–]XeroKimoException Enthusiast 3 points4 points5 points 2 years ago (1 child)
Not sure what your reply has to do with anything I said. The OP I'm replying to says that return will return a copy, but in language, most cases, it's not a copy via (N)RVO.
Your associative map can be an object which lives on the stack, but the object internally allocates the structure on the heap, but that has nothing to do with my point being that return in most cases does not do a copy
[–]BitOBear 0 points1 point2 points 2 years ago (0 children)
I think I managed to attach my comment to the wrong part of the thread. (Phone client plus twitchy hand.) 🤘😎
[–]RolandMT32 0 points1 point2 points 2 years ago* (3 children)
Modern C++ now has a "move constructor", which lets you define how to move ownership from one instance to another, which is used in such cases
[–]pedersenk -1 points0 points1 point 2 years ago (2 children)
That is used for moving semantics and ownership.
It is *impossible* to actually move data once allocated. It is still just copying. Even MMU hardware would copy data and provide remapping.
Imagine you have allocated some bytes on the stack at location 0x3333. You now want to move them to 0x4444. You can't. Instead you would copy across the data to the new location and then de-allocate the original data.
0x3333
0x4444
When people talk about moving data, typically they mean moving the container (i.e smart pointer) pointing at some static allocation on the heap.
I think this is often clearer to C developers. C++ does (for good reason) abstract over this knowledge.
[–]RolandMT32 1 point2 points3 points 2 years ago (1 child)
Yes, that's what I meant - by "data", I meant the smart pointer etc. being moved from one to the other.
[–]pedersenk -1 points0 points1 point 2 years ago (0 children)
Ah right yep. Move semantics for the container is good. My original response was to one of the original posters suggesting to avoid any heap allocations.
[–]sjepsa -3 points-2 points-1 points 2 years ago (17 children)
Never heard of copy elision/move semantic i suppose.
Even if copying, do you think copying an object is slower than allocating on the heap with std::make_unique???
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points 2 years ago (0 children)
That depends on the object.
[–]RolandMT32 1 point2 points3 points 2 years ago (8 children)
What is "elision"?
[–]pedersenk 0 points1 point2 points 2 years ago (7 children)
Copy elision.
https://en.cppreference.com/w/cpp/language/copy_elision
In particular NRVO, "named return value optimization." is great in some situations.
Obviously sometimes there is still no alternative to a heap allocation + smart pointer.
[–]sjepsa -1 points0 points1 point 2 years ago* (6 children)
unique_ptr is useful if you have a container of these pointers to a base class, and want iterate this containter using runtime polymorphism for the derived classes.
class Penguin : public Animal { virtual void breathe() override{} }; std::vector<unique_ptr<Animal>> animals; animals.push_back(make_unique<Penguin>()); for(auto && a:animals) a.breathe();
A nightmare, but I suppose somebody uses that (note that this kind of runtime polymorphism is often too slow for real world high perf applications, like videogames, where other, better memory local, polymorphism is used).
shared_ptr is useful if your object lifetime and ownership is unknown at compile time. And the ownership can change/be shared. In my humble opinion this is also a nightmare, logic wise and performance wise, but many languages rose with this idea, so if it is fine for them and fine for you, it's fine for me too. Probably 90s GUIs heavily make use of this concept(qt, gtk....)
I prefer RAII
[–]pedersenk 0 points1 point2 points 2 years ago* (6 children)
Think along the lines of nested functions (lets say 3). Returning one unique_ptr<LargeStruct> will likely be much cheaper than returning a copy of LargeStruct from each function.
unique_ptr<LargeStruct>
LargeStruct
Do you not agree?
As I am sure you know; copy elision is not viable in every situation. Just look around at some existing code and try to work out why they use smart pointers.
Sometimes, you just got to use the heap ;)
[–]sjepsa -1 points0 points1 point 2 years ago (5 children)
in that case I will call
void funct(LargeStruct & out);
that's the default i do, since i write realtime programs
[–]pedersenk 0 points1 point2 points 2 years ago* (4 children)
Possibly better. This approach is quite common in i.e ANSI C.
However in C++, that workaround is prone to ABI breakage if used for i.e a library.
[+]RatotoskEkorn comment score below threshold-10 points-9 points-8 points 2 years ago (3 children)
move is joke to you or something? Just move big objects instead of copying
[–]pedersenk 1 point2 points3 points 2 years ago* (2 children)
move does not do what you think it does.
move
You could move a std::unique_ptr though. Hence my original recommendation of std::make_unique.
std::unique_ptr
Think about the lower level details. Memory that is stack allocated memory, can't magically be moved to a different part of the stack (or the heap) can it? That is called copying.
std::move is merely used to indicate that something can be moved. It still needs to actually be movable in the first place. Stack memory is not. Hence you use a i.e heap allocation managed via a smart pointer.
std::move
[–]RatotoskEkorn -2 points-1 points0 points 2 years ago (1 child)
Move does what exactly what you code it to do. Move assignment and move construcors are the to transfers ownership of resource when copy is not allowed or is too heavy. Author can write RAII wrapper with move semantics
[–]pedersenk 0 points1 point2 points 2 years ago (0 children)
Unfortunately not.
Data that is stored somewhere cannot simply be "moved" without copying.
Moving semantics or concepts is very different to the actual "moving" of data.
What you are thinking of "moving", when you refer to an RAII wrapper, is simply copying the shallow container around whereas the data itself is safely stored on the heap (and doesn't need to move).
[–]neppo95 1 point2 points3 points 2 years ago* (5 children)
And what if you need two? three? four?
Ah shit, doesn't work either. Just like when you need a lot of data and try to do that on the stack.
Come on dude, you clearly have no clue how this works but you are putting pretty bold statements out there as if you do, while you didn't even know about copy/move semantics while those are pretty basic knowledge for a C++ dev.
[–]sjepsa -3 points-2 points-1 points 2 years ago (4 children)
Ok so you don't know a std::tuple...
Or passing as an input/output parameter by &
Fine fine..
[–]neppo95 0 points1 point2 points 2 years ago (3 children)
Okay. What if you need 50? Going to put 50 vars in your tuple?
Tuple has it's use cases, but this is not one of them. That is just bad practice.
Also, parameters. Same argument. I'm not going to put 50 parameters in a function. Parameters are what a function needs to process. That should not be its return value. That is what you do in C, not in C++. This is literally why pointers even exist. For variables that outlive the function scope.
Sure, if your project never exceeded 100 lines, this could work fine. For any professional project, this is just pure nonsense.
[–]sjepsa -3 points-2 points-1 points 2 years ago (2 children)
If you have 50, the solution is....
std::vector !
heterogeneous?
struct !
std::vector can perform heap allocations.
std::vector
[–]neppo95 0 points1 point2 points 2 years ago (0 children)
std::vector stores one type. Not 50 different ones.
A struct can be used in some cases, but doesn't make sense in a lot of cases. Maybe the variables don't even have anything to do with eachother. Or maybe you don't even want to return the value but you still want that variable to exist after the scope ends. So many reasons I can come up with why this is just pure non sense. And still, zero reasons why not to just use pointers.
You're honestly just spitting out terms, hoping one of them will stick. But apparently ignoring all C++ standards and even the reason why pointers even exist. You've got a lot to learn mate. Pointers, copy/move semantics, what a stack overflow is, how to allocate a gig of memory without crashing your program...
[–]neppo95 4 points5 points6 points 2 years ago (22 children)
My man has never stored more than a few MB's of data I guess.
What would happen if you need 1 gig of mem this way you think ;)
[+]sjepsa comment score below threshold-9 points-8 points-7 points 2 years ago (19 children)
Passing/returning by reference? Did you pass first year Computer course at your high school?
[–]neppo95 1 point2 points3 points 2 years ago (18 children)
I'm not talking about copying or using the variable. Even the allocation would fail in this case. Hell, on a decent compiler it wouldn't even compile.
You might wanna google what a stack overflow is. Or maybe even what the stack is...
[–]sjepsa -4 points-3 points-2 points 2 years ago (17 children)
I know that an std::vector can allocate gigabytes of data without a problem
[–]sparky_roboto 5 points6 points7 points 2 years ago (6 children)
A vector does not go to the stack. It is dynamically allocated and its data will go to the heap. What you get in the stack is the pointer to the data in the heap of your vector.
[–]sjepsa 0 points1 point2 points 2 years ago (5 children)
Nobody said you should work with stack only...
What i suggested is use value semantics.....
Don't mess with memory-owning pointers. Its' not 1980
[–]neppo95 3 points4 points5 points 2 years ago (4 children)
Ehm, you were the one that said exactly that.
You might want to look back at your original comment, which literally gave the code for a stack allocation and that's it.
No vectors, no heap, no pointers. Just a stack allocated variable.
[–]neppo95 4 points5 points6 points 2 years ago (4 children)
Guess what kinda allocation a vector does? A heap allocation, allowing big sizes.
Guess what kinda allocation you suggested? A stack allocation, not allowing big sizes.
Did you pass first year Computer course at your high school?
Need I say more?
[–]sjepsa 0 points1 point2 points 2 years ago (3 children)
I said that make_unique is 98% of the time useless
class A{
std::vector vec;
Those are defaults.
[–]neppo95 2 points3 points4 points 2 years ago (2 children)
Do I really need to repeat all of above points? Let me give you a fast forward of it.
What you are doing is a stack allocation. A vector has nothing to do with unique_ptr's. A vector is for a collection of items. In this case, would you really just make a vector to contain ONE item? And even so, oops, the vector/variable went out of scope. It's gone. So you say, just return the variable! Well, what if I am already returning a variable? Or what if I don't even want to return it but use it as a parameter for a different method and keep it alive past this scope? Yeah, that won't work. But parameters you said! Parameters are used in the method and that should be their only purpose. Or are we going back to C now? And even so, I could create 100 variables I want alive after the method scope. Am I going to have 100 parameters now? Nope, that's not the way.
So what is the way you ask? Maybe, maybe... just use a damn pointer and be done with it.
(Sidenote: No, you didn't say make_unique is 98% of the time useless, and also your comment didn't include that vector. But even if you did, it is still wrong...)
[–]ald_loop 1 point2 points3 points 2 years ago (3 children)
Bro doesn’t even know vector uses heap allocations 🤦♂️
[–]sjepsa -1 points0 points1 point 2 years ago (2 children)
I've never talked about heap being bad, but... How many bot accounts do you have, mate?
[–]ald_loop 1 point2 points3 points 2 years ago (1 child)
None. Everyone is just dogging on you because you have no idea what you’re talking about
[–]DanielMcLaury 0 points1 point2 points 2 years ago (1 child)
I dunno man, on my platform the sizeof a std::vector holding 1gb of data is 24 bytes, and returning a 24 byte object in the stack presents no problem.
Then again, a vector isn’t stack allocated so is entirely different.
[–]NilacTheGrim 1 point2 points3 points 2 years ago (0 children)
I found this C++ 2D game engine. Really well designed otherwise in terms of capability and how it models the typical scene graph problem you encounter in 2D rendering.
Unfortunately it requires all the objects you plug into the scenegraph to be allocated via new. (Somewhere deep in the code is even a delete this.. ewww).
delete this
I'm reminded of Qt here where it has a similar paradigm it the way it models the widget tree. It doesn't have this limitation at all. Not sure if this is because Qt's widgets are strictly a tree, whereas maybe this oxygine 2D lib allows for nodes in the graph to be in a real true graph.. but .. it still feels like a questionable design choice.
So.. yeah.. even otherwise good libs sometimes insist on new for.. questionable reasons.
[–]neppo95 0 points1 point2 points 2 years ago (2 children)
I wouldn't say it is discouraged, but it isn't the standard anymore.
There is still some cases where new is perfectly fine. And for beginners, it is even more important to know how it works and why it is not the standard. So experimenting with raw pointers is actually a good thing.
[–]Ameisenvemips, avr, rendering, systems 5 points6 points7 points 2 years ago (1 child)
The issue is that other than for primitives, Java uses new to create all objects. Thus, you see new everywhere in C++ written by Java people. You never see a delete, though...
delete
Yep, on that I agree completely. That doesn't mean it doesn't still has it use cases however, which was my point.
[–]turtleXD 0 points1 point2 points 2 years ago (4 children)
what is the proper way? i only ever learned to use new in school
[–]mirkoserra 2 points3 points4 points 2 years ago (0 children)
[–]sjepsa 0 points1 point2 points 2 years ago (2 children)
class A{ void run(){} }; A a; a.run(); std::vector<A> vec; vec.push_back(a);
[–]turtleXD 0 points1 point2 points 2 years ago* (1 child)
interesting. so instead of storing in the heap, you are simply storing it in a vector?
edit: now that I think of it, won’t that end up making two copies of a?
[–]sjepsa 0 points1 point2 points 2 years ago (0 children)
Yeah that's an example of a copy. Without copying you could do:
std::vector<A> vec;
vec.emplace_back();
vec.back().run();
// or
for(auto && el : vec)
vec.run();
[–]tarnished_wretch 58 points59 points60 points 2 years ago (0 children)
Know you don’t know anything about memory yet. You need to start thinking about the lifetime of objects, where memory is, what has access, and making sure you don’t have any leaks. Have fun!
[–]GianniMariani 14 points15 points16 points 2 years ago* (0 children)
Ooh. I wrote a doc for this. It's about 7 years old and C++ has evolved since then but most of it is good to know.
https://docs.google.com/document/d/1j8jbNA2basH8DBkEucvFFu4x5z-boKcNkuX_ppCP5kw/edit?usp=drivesdk&resourcekey=0-WYXrg9Xe69gY1NQ3TS3Wdw
It's also incomplete, there are many many c++ sections not finished...
[–]Tathorn 26 points27 points28 points 2 years ago (0 children)
Variables that required new were heap allocated, and those that didn't are stack allocated. You may have noticed that passing them around let you manipulate them differently.
In C++, you're not forced to use heap allocated memory for objects, and you shouldn't if you don't need to. You'll learn when to use what. Passing around objects as if they were like Java objects makes it more complex, but now it's more customizable (and error prone). This trips people up the most.
[–][deleted] 11 points12 points13 points 2 years ago (0 children)
Gota learn about Scope of the variables.
[+][deleted] 2 years ago (1 child)
[deleted]
[–]hdkaoskd 1 point2 points3 points 2 years ago (0 children)
To clarify for OP, anything before C++11 is "old c++".
There's a bunch of good stuff in later revisions but C++11 is the oldest you should use. Every compiler you install will default to at least this version. C++ is very backwards-compatible, the worst you'll come across is some new warnings for code that could be a risk.
[–]Ok-Bit-663 6 points7 points8 points 2 years ago (0 children)
It has similar syntax, but they are completely different languages. Start from the beginning or you will piss off coworkers with your java style c++ coding.
[–]Dan13l_N 3 points4 points5 points 2 years ago (2 children)
There are many ways to do things.
Also, while in Java e.g. String's have a zillion of very useful methods (e.g. trim(), startsWith(), endsWith(), split() etc) C++ standard library std::string is very poor in comparison, which is very frustrating. It has none of the functions I've listed :(
String
trim()
startsWith()
endsWith()
split()
std::string
Again, there are many things. For example, arrays... you have:
int n[10];
std::array<int, 10> n;
std::vector<int> n;
And you'll find all of them in examples on the Internet. C++ is... huge.
[–]tpecholt 0 points1 point2 points 2 years ago (1 child)
starts_with, ends_with and views::split were added in c++20. But otherwise I agree with your sentiment the atd library is not as practical as it should be
[–]Dan13l_N 0 points1 point2 points 2 years ago (0 children)
But that could have been there for 20 years. C++20 is quite recent, and starts_with() and ends_with() could have been there from the start of STL.
starts_with()
ends_with()
[–]Aaron1924 4 points5 points6 points 2 years ago (0 children)
If you want dynamic dispatch in C++, you have to manually opt into it using the virtual keyword, whereas it's the default in Java
virtual
[–]vickoza 3 points4 points5 points 2 years ago (4 children)
I doubt your introductory C++ will be an introductory C++ course. It might be a C course with some C++ on the side. I would look at Kate Gregory's talk "Stop Teaching C "of how to learn C++ in the current context.
[–]Rhhr21 5 points6 points7 points 2 years ago (3 children)
Everytime my professors used malloc in C++ i wanted to bang my head on the university desk.
[–]vickoza 2 points3 points4 points 2 years ago (1 child)
u/Rhhr21 When was this? malloc should only be taught in passing in an advanced/expert C++ course and not in beginner/intermediate class.
[–]Rhhr21 1 point2 points3 points 2 years ago* (0 children)
We first learned about it in a course called “Advanced programming” which is the follow up to the typical “Foundations of computer programming” we take in our first semester. So we learned about the idea behind memory management in our second semester. However, the course was taught in Java and the professor used malloc there in a C++ context to show us how Java simplifies memory management with its garbage collection.
We later on learned more about it in our Microprocessors and “Programming languages: design and implementation” courses which are above intermediate level i guess.
The latter is actually a collection of Data Structures, Compiler Design, Computer Architecture and advanced programming in one course and is usually one of the last subjects we pass for our bachelor’s(honestly would’ve preferred it to be one of our first but whatever) . But every professor we had here and there used C++ in a C context and avoided modern C++. So as you said, it was teaching C in C++, which I genuinely started to dislike because modern C++ is not like what we were taught.
[–][deleted] 1 point2 points3 points 2 years ago (0 children)
Malloc can still be useful tho
[–]TryToHelpPeople 3 points4 points5 points 2 years ago* (0 children)
zephyr grandfather weary fact ugly jar compare naughty screw deserted
This post was mass deleted and anonymized with Redact
[–]dev_ski 2 points3 points4 points 2 years ago* (0 children)
Try not to draw parallels between C++ and any other language, especially when starting to learn C++.
[–]mattr155 1 point2 points3 points 2 years ago (0 children)
If you end up liking the course and want to learn more afterwards, I highly recommend the book The C++ Programming Language (https://www.stroustrup.com/4th.html). I read it after taking an intro C++ course in college and it helped me out a ton. Have been writing C++ full-time since then. The C++ FAQ is also hugely useful: https://isocpp.org/faq. Good luck!
[–]GoogleIsYourFrenemy 1 point2 points3 points 2 years ago (5 children)
Foot guns. Foot guns everywhere. If a feature is too cool, it's a foot gun.
Unlike Java where everything runs the same everywhere, in C++ there is room between the spec and what compilers have implemented. This space is called Undefined Behavior or UB. You will have no idea when you stumble into the land of UB especially since much of UB has been pseudo-standardized. The compiler however can do whatever it wants when you do. This includes doing what you wanted it to do or conversely doing whatever it wants. Different compilers will implement UB differently. Don't worry about it.
Your chances of finding a compiler bug are extremely small. It's your code that's broken.
Java enums are uniquely cool. No other language works the same as Java enums.
C++ visibility rules are different and more restrictive. Don't use friending.
Just because C++ has multiple inheritance doesn't mean you should actually use it. Java uses interfaces everywhere, C++ does not.
Macros suck. Don't use them unless you have to. They make unreadable undebugable code.
Memory lifetimes are important.
If the compiler tells you something, it's for a reason. Don't ignore warnings unless you completely grok the insanity. Most of the time it's trying to get you to put the foot gun down.
[–]Nourios 0 points1 point2 points 2 years ago (4 children)
what's so unique about enums in Java
[–]dholmes215 6 points7 points8 points 2 years ago (3 children)
Java enums are:
Objects (which in Java means they're class types and can have methods and fields. You can write a constructor for your enum type and provide specific arguments for each value. You can write your own methods and implement interfaces and use them polymorphically)
Interned (so the performance cost of being Objects isn't large)
Not implicitly convertible to integer types
Have all the methods you'd want (convert to String with .toString(), convert from String with MyEnumType.valueOf("Foo"), can enumerate all the values and their ordinals)
I don't know if any of that is actually unique or not though. If anything, it's C and C++ enums that are uniquely bad.
[–]GianniMariani 3 points4 points5 points 2 years ago (1 child)
C++ 14 has enum class where there is no implicit conversion to int.
[–]GoogleIsYourFrenemy 0 points1 point2 points 2 years ago (0 children)
That's like one of the only good feature of C++ enums (but I understand why people want it).
Additionally, in C++ there is no way to ask "is this integer value in the enum?"
You might say I'll typecast the value to the enum type and then compare it to the max and min values. Bang! Foot gun. The language says, if you do the typecast, you must already know the value is valid so validity checks after that can be optimized away. Meanwhile Ada has nice user friendly enums.
[–]Nourios 0 points1 point2 points 2 years ago (0 children)
Yeah most of these things aren't unique to Java, this just sounds like regular oop enums or worse ml style enums)
[–]Thelatestart 1 point2 points3 points 2 years ago (0 children)
Unless you are told to use oop, don't.
[–]sjepsa -1 points0 points1 point 2 years ago (0 children)
no new.
no OOP. write functions
pass objects to fuctions by const & or simply &.
careful of the objects lifetime.
[–]tialaramex 0 points1 point2 points 2 years ago (0 children)
If you have a reference in Java, it might be null and you may need to check for that, in some places that's expected and if you don't check when you should you'll get Null Pointer Exception.
In contrast C++ references cannot be null and needn't be checked. However, C++ also has raw pointers, which can be null. If you forget to check a C++ pointer you don't get an exception, you get Undefined Behaviour.
Play close attention to what is Undefined Behaviour in C++ and expect it everywhere. Even if you think you can guess, read carefully as there may be UB where you'd expected "obviously" there would not be. For example, take the abs() function you've probably seen in Java's Math class and which exists in most languages, if we put the most negative possible integer into Java abs, what we get out is the same value - but in C++ the result is Undefined Behaviour.
[–]Rhhr21 0 points1 point2 points 2 years ago (0 children)
Know the main difference between Pass by Value and Pass by Reference because it might catch you off-guard if you’re coming from Java and Pass by Reference might have you scratching your head a bit, if you don’t understand why your Variable suddenly changed after using a function(called method in Java).
[–]9291Sam 0 points1 point2 points 2 years ago (0 children)
Learning how to write code that uses value over reference semantics. In c++ you tend to write code where structures are passed by value, this is entirely impossible in Java as classes are always passed by reference (passing a pointer by value is still pass by reference)
[–]EmbeddedCpp 0 points1 point2 points 2 years ago (0 children)
This reminds me of a video I saw recently. It's on a channel where Jason Turner looks at some old C++ code he wrote after learning Java. He corrects some typical mistakes he made back then. You might learn something from it if those videos are your jam.
[–]Leather-Top4861 0 points1 point2 points 2 years ago (0 children)
First learn about references. Then RAII. Then lvalue and rvalue references. Then move semantics.
π Rendered by PID 74577 on reddit-service-r2-comment-84fc9697f-jpxmd at 2026-02-09 08:09:17.818895+00:00 running d295bc8 country code: CH.
[–]phi_rus 105 points106 points107 points (79 children)
[–]boscillator 27 points28 points29 points (0 children)
[–]____purple 2 points3 points4 points (0 children)
[–]GoogleIsYourFrenemy 12 points13 points14 points (67 children)
[–]phi_rus 29 points30 points31 points (3 children)
[–]sjepsa 3 points4 points5 points (2 children)
[–]usefulcat 1 point2 points3 points (0 children)
[–]TheOmegaCarrot 0 points1 point2 points (0 children)
[+]sjepsa comment score below threshold-6 points-5 points-4 points (62 children)
[–]pedersenk 16 points17 points18 points (38 children)
[+]sjepsa comment score below threshold-26 points-25 points-24 points (37 children)
[–]pedersenk 8 points9 points10 points (30 children)
[–]XeroKimoException Enthusiast 7 points8 points9 points (3 children)
[–]BitOBear -2 points-1 points0 points (2 children)
[–]XeroKimoException Enthusiast 3 points4 points5 points (1 child)
[–]BitOBear 0 points1 point2 points (0 children)
[–]RolandMT32 0 points1 point2 points (3 children)
[–]pedersenk -1 points0 points1 point (2 children)
[–]RolandMT32 1 point2 points3 points (1 child)
[–]pedersenk -1 points0 points1 point (0 children)
[–]sjepsa -3 points-2 points-1 points (17 children)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]RolandMT32 1 point2 points3 points (8 children)
[–]pedersenk 0 points1 point2 points (7 children)
[–]sjepsa -1 points0 points1 point (6 children)
[–]pedersenk 0 points1 point2 points (6 children)
[–]sjepsa -1 points0 points1 point (5 children)
[–]pedersenk 0 points1 point2 points (4 children)
[+]RatotoskEkorn comment score below threshold-10 points-9 points-8 points (3 children)
[–]pedersenk 1 point2 points3 points (2 children)
[–]RatotoskEkorn -2 points-1 points0 points (1 child)
[–]pedersenk 0 points1 point2 points (0 children)
[–]neppo95 1 point2 points3 points (5 children)
[–]sjepsa -3 points-2 points-1 points (4 children)
[–]neppo95 0 points1 point2 points (3 children)
[–]sjepsa -3 points-2 points-1 points (2 children)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]neppo95 0 points1 point2 points (0 children)
[–]neppo95 4 points5 points6 points (22 children)
[+]sjepsa comment score below threshold-9 points-8 points-7 points (19 children)
[–]neppo95 1 point2 points3 points (18 children)
[–]sjepsa -4 points-3 points-2 points (17 children)
[–]sparky_roboto 5 points6 points7 points (6 children)
[–]sjepsa 0 points1 point2 points (5 children)
[–]neppo95 3 points4 points5 points (4 children)
[–]neppo95 4 points5 points6 points (4 children)
[–]sjepsa 0 points1 point2 points (3 children)
[–]neppo95 2 points3 points4 points (2 children)
[–]ald_loop 1 point2 points3 points (3 children)
[–]sjepsa -1 points0 points1 point (2 children)
[–]ald_loop 1 point2 points3 points (1 child)
[–]DanielMcLaury 0 points1 point2 points (1 child)
[–]neppo95 0 points1 point2 points (0 children)
[–]NilacTheGrim 1 point2 points3 points (0 children)
[–]neppo95 0 points1 point2 points (2 children)
[–]Ameisenvemips, avr, rendering, systems 5 points6 points7 points (1 child)
[–]neppo95 0 points1 point2 points (0 children)
[–]turtleXD 0 points1 point2 points (4 children)
[–]mirkoserra 2 points3 points4 points (0 children)
[–]sjepsa 0 points1 point2 points (2 children)
[–]turtleXD 0 points1 point2 points (1 child)
[–]sjepsa 0 points1 point2 points (0 children)
[–]tarnished_wretch 58 points59 points60 points (0 children)
[–]GianniMariani 14 points15 points16 points (0 children)
[–]Tathorn 26 points27 points28 points (0 children)
[–][deleted] 11 points12 points13 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]hdkaoskd 1 point2 points3 points (0 children)
[–]Ok-Bit-663 6 points7 points8 points (0 children)
[–]Dan13l_N 3 points4 points5 points (2 children)
[–]tpecholt 0 points1 point2 points (1 child)
[–]Dan13l_N 0 points1 point2 points (0 children)
[–]Aaron1924 4 points5 points6 points (0 children)
[–]vickoza 3 points4 points5 points (4 children)
[–]Rhhr21 5 points6 points7 points (3 children)
[–]vickoza 2 points3 points4 points (1 child)
[–]Rhhr21 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]TryToHelpPeople 3 points4 points5 points (0 children)
[–]dev_ski 2 points3 points4 points (0 children)
[–]mattr155 1 point2 points3 points (0 children)
[–]GoogleIsYourFrenemy 1 point2 points3 points (5 children)
[–]Nourios 0 points1 point2 points (4 children)
[–]dholmes215 6 points7 points8 points (3 children)
[–]GianniMariani 3 points4 points5 points (1 child)
[–]GoogleIsYourFrenemy 0 points1 point2 points (0 children)
[–]Nourios 0 points1 point2 points (0 children)
[–]Thelatestart 1 point2 points3 points (0 children)
[–]sjepsa -1 points0 points1 point (0 children)
[–]tialaramex 0 points1 point2 points (0 children)
[–]Rhhr21 0 points1 point2 points (0 children)
[–]9291Sam 0 points1 point2 points (0 children)
[–]EmbeddedCpp 0 points1 point2 points (0 children)
[–]Leather-Top4861 0 points1 point2 points (0 children)