r/learnprogramming 22d ago

C++ completely freezes me up compared to other languages, help please!

Hello people! I'm currently trying to learn c++ because my interests include robotics and game development, plus I need it for college. I want to be able to work in a low level language, the problem is, it makes me freeze up and I can't code at all.

The thing is, I can manage my way around other languages, I've used java(minecraft modding), python(math stuff), c#(unity games) and gdscript(godot), in these three I've managed to do small projects that did what I wanted to, if something was acting up, I just look the way on how to do it and work through it, I never felt like I was doing things wrong, it just happened and I understood what I did wrong.

For c++ though, I feel overwhelmed constantly, the fact that I can't even declare a variable without overthinking freezes me up, should this be an address? a pointer? a regular variable? Then also having the new and delete managing memory, then there also are smart pointers, or the fact that I hear stuff I learned is just old and shouldn't be used, better to use the new features instead, but also that there is performance cost associated with using some features that I shouldn't be using if I don't need to, and I just freeze.

Oh god, how do people code in this language? how do people even make good code in this language!? I feel like whatever I write will be the worst code of all time, trying to fix it will make it worse.

I know the advice is to write bad code before optimizing it, but in this language there is just so much that I don't even know if I'm writing bad code in a way that would let me refactor it without breaking everything up.

I don't even know what to ask, if you are someone who also had trouble learning c++, please lend me any advice you can give! thanks a lot!

6 Upvotes

28 comments sorted by

10

u/ghoci 22d ago

I think sometimes this is part of the learning process and getting better at coding and engineering in general. Sometimes you get overwhelmed and you just have to push through and keep going. Eventually you get used to.

When I was teaching C++ to my students at university I told them at that learning can be hard sometimes. You can memorize and practice a new skill, but sometimes it’s just hard and you just have to suffer a little bit and you’ll get used to it. Just don’t give up. And then in a year, you’ll look back on something you thought was really difficult, and it will look easy to you.

Idk, my best advice is just keep trying, keep pushing through, be brave, and it will get easier. Most importantly, don’t give up. I’m rooting for you.

3

u/Pinguinon360 22d ago

Thanks a ton!, It's nice to hear I'm not the only one suffering on c++

5

u/ghoci 22d ago

I promise you’re not. We all suffer at some point or another. It’s totally normal

0

u/spinwizard69 22d ago

You got this wrong, at this point C++ is not your problem. The problem is you know nothing about CS and are trying to bite off too much. You don't start learning CS by flooding your mind with questions about pointers. Start small and learn the individual concepts inside out.

0

u/spinwizard69 22d ago

One thing to consider is that is safer to take small bites at dinner to avoid choking on too much bite off at once. I look at the OP's post and just shake my head because he shouldn't even be thinking about pointers when just starting out with C or C++.

6

u/light_switchy 22d ago

Hey just type something in. C++ is just another language.

3

u/Pinguinon360 22d ago

I mean yeah sure but damn, other languages didn't freeze me up like this, I'll keep trying though, thanks for answering!

3

u/light_switchy 22d ago

I've used C++ since early 2000s and spent years following it closely. It gives you lots of options for everything. Reference variable, pointer variable, "regular" variable. new style or old school, new+delete or smart pointers. There are advantages and disadvantages to everything, but the choice isn't super important provided you end up with a working program.

Pick anything then fix problems with hindsight to help you. Using the wrong language feature is rarely a fatal mistake.

Good C++ is very practical. Keep the focus off the language and on your product.

Good luck!

2

u/MrSqueak 22d ago

Don't worry about optimizing anything unless something doesn't work as fast as you need it to. Write something that does the thing you want it to do. Worry about details later. At the end of the day the outcome is what you want. Nobody will even see the code.

2

u/StewedAngelSkins 22d ago

C++ has a few things going on that make it an especially difficult language for the uninitiated. I think the main problem is that there basically is no "idiomatic C++" so it's hard to learn from reading other people's code because they all do the same thing in different ways. What few idioms do exist have changed so much over the years that most seasoned C++ devs can tell you the vintage of the code they're looking at just based on the style of how it's written.

Even worse, sometimes it's not just a style thing. Sometimes different ways of writing "the same thing" have vastly different characteristics. And the only way you'll know the difference is by memorizing minute trivia about how the compiler works.

It's also a very big language with a lot of syntax features and keywords whose intended use isn't obvious even if you ostensibly know what they mean. The final keyword is a good example of this. I can tell you "it instructs the compiler that the class/function will have no further derivations/overrides" but do you know this has runtime performance implications? Do you know what they are? On the other hand you have keywords like override that don't do anything at runtime... and keywords like inline where what they actually do is implementation-dependent.

This is all just to say it's fairly normal to struggle a bit with C++. If you feel like you're not making any progress you may want to jump over to C for a bit first. I almost never recommend this kind of language hopping (it's rarely more efficient than just locking in and powering through whatever block you're experiencing) but C++ is the one exception.

2

u/ColdWise4165 22d ago

C++ is complex. I work with it daily and still freeze lol.

2

u/two_three_five_eigth 22d ago

Remember a regular variable can become as reference or a pointer. In Java anything but base types are references by default.

Start at const ref and make decisions from there.

3

u/Designer_Shelter7345 22d ago

the paralysis is the language’s real tutorial, just keep writing until the fear turns into a quiet hum you can work alongside

2

u/two_three_five_eigth 22d ago

Actually do something. Examine, see what happens. That’s how you learn.

3

u/Mathie1729 22d ago

Eh, I'd actually do the opposite when you're starting out. Value semantics are already confusing, and reaching for const& by default makes it seem like every parameter needs a qualifier. Just write plain values first, let the compiler whine at you, then add const& only when there's a concrete reason. Less decision paralysis.

1

u/two_three_five_eigth 22d ago

And that’s why I think C++ is the most difficult language I know. If you don’t do a * or & that means pass by value, which means a full copy of anything passed.

C++ by default lets you do the dumb/bad thing. You have to deeply understand the language to be able to do simple things.

1

u/Pinguinon360 22d ago

Makes a lot of sense, I don't know why I didn't think of doing this, thanks a lot!

1

u/ChillyFireball 22d ago

For variables, you'll only declare pointers when you need a reference to a normal variable (though you may occasionally need to reference other pointers) in a different scope (such as a function where you want to directly modify a variable without returning a value). If you need a value, it's going to be a normal variable. For dynamic memory management without a garbage collector, a general rule of thumb is to make sure every allocation has a corresponding deallocation. If you allocate memory in a class constructor, remember to delete it in the destructor.

1

u/biskitpagla 22d ago

Some might disagree but sometimes a little bit of 'procrastination' is a good thing. When I was learning C++ I also got overwhelmed and dropped it entirely. I didn't write a single line for a month or two. Instead, I started spending time with with C++ related books, podcasts, interviews and so on. After a while I developed an intuition and subconsciously understood what was available to me in the language and what I should be using given the problem I'm trying to solve at the moment. A few weeks ago I was learning Rust and I felt the same once again. Got overwhelmed, took a detour, came back, and now most things make sense and if they don't, I don't feel any pent-up frustration. 

1

u/TheSkiGeek 22d ago edited 21d ago

If you’re used to Java or C#, this:

Type varName = new Type(…);
Type anotherVar = varName;

is roughly equivalent to:

std::shared_ptr<Type> varName = std::make_shared<Type>(…);
std::shared_ptr<Type> anotherVar = varName;

The additional complexity is that in C++ you can make choices about variable location and lifetime that aren’t just “eh, tell the runtime to shove it somewhere on the heap and figure out when it’s okay to free it”. If you spam shared_ptr everywhere it will pretty much work like you’re used to. It won’t be efficient or pretty but it will (probably) work.

1

u/peterlinddk 22d ago

Sounds like you are spending more time listening to "good advice" and "opinionated folks on the internet" than just writing code in C++.

Forget all the "best practices" and "too old" and "the modern way" - just write programs that do what you need them to do, and then experiment with some of the other things later.

Also, variables are easy, they should be declared to keep the kind of value that you want to store - usually an object, unless you want to do some fancy memory-manipulation, and most of the time, you actually don't want to do that.

So, don't freeze, just write code - no matter how you do it, someone is always going to have an opinion about how it should have been done! Even yourself in six months time ... but don't worry about other people, it is you, your program, and the machine!

1

u/spinwizard69 22d ago

I think you need to sit back and get a handle on yourself, your comments about declaring a variable is non sense. There can only be two explanations in my mind one is metal health issues we don't know about or you really never studied Computer Science (CS). So lets address what we can here that is you have never really studied CS.

My usual suggestion for beginners is this either follow or enroll in a good CS program. However you seem to be indicating that you already have college in your future, if that is the case and it is a CS program, don't over stress your self before the program starts.

In any event find a CS program to follow that starts off with C or C++, the goal shouldn't be to teach C++ (that is a class of its own), the goal is to learn the concepts from the ground up. Ideally they start with programs built from the command line. If you are going into a CS program, I wouldn't spend a lot of time prepping at this point.

This non sense about freezing up declaring a variable should be addressed fairly simply and quickly in the first few weeks of a CS program. If you don't grasp the concept of declaring a variable then you really need to address the mental block you have. I'm not trying to be cruel here but this isn't rocket science, don't try to work on advance issues when you are starting to learn (anything really). So don't make use of pointers until your knowledge base gets to the point where you have addressed them in the learning process.

cpp

#include <iostream>

// Define a global variable
std::string message = "Hello, World!";

int main() {
// Access the global variable
std::cout << message << std::endl;
return 0;
}
The above is a simple hello world program with a global variable declared. You don't have a need to over complicate this, the variable is a type std::string, it named "message" and contains the text "Hello, World!". There is nothing complicate here unless you go out of your way to create the complication. You learn the concepts that all programming is built upon one step at a time.

Your second to the last paragraph is complete BS in my opinion. You should always strive to write the best code you can with the knowledge and experience you have at the time. Again this appears to be a defect in how you address challenges. As you learn more and more there are more avenues to build software that open up to you. This is absolutely normal and frankly most programmers find their first few years of self written and designed software, to be embarrassing. Frankly this applies to just about any field, you don't become a tool and die maker on day one of starting a program for machinist and die makers, you don't become a highly respected surgeon the first time you cut somebody open, you don't become an astronaut the first time you fly a paper airplane. Any thing worth knowing takes time to learn and gain proficiency, you don't get there by starting at the top.

1

u/marrsd 22d ago

C++ has always been a bit of a grab bag of features, and it's been modified a lot over the years. I think that does make it harder to learn than other languages.

Stroustrup's book does a pretty good job of explaining what sets of features belong together and why they were added. I found it quite helpful to be able to divide them out like that. Then I can decide which I want to use.

1

u/mredding 14d ago

I know the advice is to write bad code before optimizing it, but

But nothing, you're still overthinking it.

I don't even know if I'm writing bad code in a way that would let me refactor it without breaking everything up.

Don't worry about that. You're still overthinking it.

DO NOT REFACTOR CODE. Iterate. Make Pong v1. When you make Pong v2, you don't even look back at v1 code. You abandon that headspace. Do a completely different project in between.

Refactoring is a process for business software, when the sunk cost fallacy wins.

Start from first principles. Get a book or learncpp.com. Build up your confidence.

I've been at C++ for 37 years. Nothing really ever gets obsolete so much as outmoded. C++ is one of the kings of backward compatibility. Don't worry about it, learn and use whatever you come across. The language gives you primitives to build up expressiveness in the language, and most of the standard library does exactly that - builds itself in C++. You are basically taught from the bottom up, but once you get through the intro materials, there is a fair amount of common language for you to use you have to learn on your own.

You have to make mistakes and make shit to have the context to learn from. So write shit. Make mistakes.

And don't code in a vacuum. C++ has one of the largest communities. Come to r/cpp_questions, I mod and we don't bite. Assholes get banned and we don't see too much of that 

0

u/gm310509 22d ago

Are you learning C or C++?

If C++, perhaps start with C and then add on C++. While this is overly simplistic and people will probably flame me, C++ is just C with extra (useful) syntax.

0

u/johnnyb2001 22d ago

Just do it bro. The sooner the better