C++ makes me cry.

Started by
43 comments, last by daerid 19 years, 6 months ago
Quote:Original post by thedevdan
It is becoming painfully apparent that I can't use vectors on actual data; that I will need to use vectors of pointers. This is because I will be using interfaces extensively. Should I use boost's shared_ptr? What other options are there? Surely they don't manage memory all themselves?


Since nobody has brought this up yet, I figured I should mention it. Unless you have containers(vector, list, etc) of simple intrinsic types(int, float, etc) you should almost always prefer containers that hold pointers to your objects instead of containers of actual objects. The reason for this is that the STL makes a lot of copies of whatever the containers hold. It makes a copy when you pass something in, it makes a copy when you get something out, and it may make copies at other times, like when it re-sizes itself.

Now, whether you should use a container of raw pointers or a container of something like a shared_ptr has less to do with the container itself and more to do with how you intend to manage the data it holds. If you really feel that you need to be able to create/destroy this data in an ad-hoc manner any place in your application, then shared_ptr may be the way to go. If that's not the case, then raw pointers should be fine.

With that being said, whenever I need to access data at many places in my application I personally prefer the approach of having some kind of class that holds and manages this data for me. The other parts of the application can ask to look at the data, but they are not allowed to create/drestroy it directly. I think this makes it much easier to track down problems because it all happens in one place, instead of being scattered about your application.

One last reminder, make sure your classes destructors are virtual. Otherwise, as I'm sure you know, you'll run into problems if you go to delete pointers to your base class.

-John
- John
Advertisement
Quote:Original post by Tron3k
I don't think it's the right thing to call C# a crutch. Is C++ a crutch because it lets you write constructors and destructors that are called automatically, instead of you having to call them on your own? No. C++ lets you think on a higher plane of thought than C because the programmer is free from having to worry about some unimportant details. Similarly, C#, with its garbage collection, allows the programmer to think on an even higher level and thus make better and more innovative code. By the way, tests show that C# is almost as fast (and sometimes even faster) than C++. The only problem really is the overhead of the .Net framework.


I would make a different comparison. Calling C# a crutch vs. C++ is like calling C a crutch vs. ASM. Is not every programming language above assembly a crutch?

There is a fine line between being a crutch and adding functionality however. :)

Quote:I would make a different comparison. Calling C# a crutch vs. C++ is like calling C a crutch vs. ASM. Is not every programming language above assembly a crutch?

There is a fine line between being a crutch and adding functionality however. :)


The difference is that C++ adds functionality without limiting your freedom in any way. In C++, you can implement anything you want, in any way you want, its as simple as that. You cant garbage collection? implement it! You want to perform pointer arithmetic? go ahead. You don't want to use classes? No problem. In C++, you can even create your own code stream and execute it (on-the-fly compilation).

The only real weakness of C++ is you, the programmer. Because in order to program efficiently in C++, you have to know what you're doing. I met many people who learned to "program" with visual basic and were very much scared of C++. I took a visual basic class once (I swear I had no choice) and I understood why: Visual basic is not logical. Everything is done for you, and nothing seems to follow logically. You just have to know the "language" by heart, and you can do one or two things "easily".

C++ is a language for the intuitive programmers who have a clue about the internal workings of a computer and operating system. C++ is for those who have a clue about what they want to program... Do I use C++ because I am afraid of using C? No. I use C++ because I know how to implement a C++ compiler in C.

So, back to the point, I fully agree with antareus:

Quote:C# is not a panacea for poor software engineering.

Looking for a serious game project?
www.xgameproject.com
C++ is far from intuitive.

C is pretty close, but C++ has a steep learning curve.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Quote:Original post by antareus
C++ is far from intuitive.

C is pretty close, but C++ has a steep learning curve.


I learned programming starting from the basics. I first learned about the basics of how a microprocessor works, I then learned procedural languages, and quickly jumped to C++.

I found it very intuitive. Indeed, when you have an idea of how a microprocessor works, the concept of a pointer seems absolutely natural. Object-oriented design is also a natural way of thinking. In fact, if you look at the Quake 2 source code, you will see that they simply implement inheritance in C. They have structs containing data and function pointers. So a monster can contain all its states, and pointers to functions that define its behavior (in function of a struct containing all its states).

What mostly scares people about C++ is the syntax and the pointers. The syntax, just like any language, is a matter of getting used to... And the pointers, or references, whatever you call them, are an essential concept of pretty much any programming language that works. The syntax of C++ is actually quite nice when you think about it (and thats why so many languages are copying it). You get rid of useless keywords, like the then after the if. You can create code blocks, you can line up your code in multiple ways, for clarity... And you can make comments that span over multiple lines.

Looking for a serious game project?
www.xgameproject.com
Quote:Original post by Max_Payne
I learned programming starting from the basics. I first learned about the basics of how a microprocessor works, I then learned procedural languages, and quickly jumped to C++.

C++ is a procedural language.
Quote:
I found it very intuitive. Indeed, when you have an idea of how a microprocessor works, the concept of a pointer seems absolutely natural.

C++ is, as already mentioned, far from being intuitive.
It sets a lot of traps and requires a good amount of knowlegde about how the language works.
Quote:
Object-oriented design is also a natural way of thinking.

It's one way to design programs. I'd be careful with such general statements. There are situations where OO is more obstructive than helpful. It's a Good Thing™, however, that you don't have to use it with C++, though.

Quote:
And the pointers, or references, whatever you call them, are an essential concept of pretty much any programming language that works.

How many programming languages did you ever use?
Quote:
The syntax of C++ is actually quite nice when you think about it (and thats why so many languages are copying it).

Actually C++ uses the same syntax as C.
Abnd when I think about C++ syntax I ask myself why there are so many beginner's stumbling over it. With Pascal its almost like - if it compiles correctly it most certainly does exactly what I expected. As opposed to C-like syntax where there are many little traps waiting for you to step into them.

C++ might be not that hard to learn - but it sure is very hard to master. Big projects require a good amount of software engineering and this should ideally be language-agnostic. The programming language is a tool, not a solution. Therefore it's good to know and use more than just one to enable you to choose the right tool for the job.

Quote:Original post by Max_Payne
What mostly scares people about C++ is the syntax and the pointers. The syntax, just like any language, is a matter of getting used to... And the pointers, or references, whatever you call them, are an essential concept of pretty much any programming language that works.
I disagree. C++ syntax is a relic, is inconsistent (why do we have terminating semi-colons on class/struct declarations but not on other block statements?) and is fairly primitive. Pointers aren't a strict necessity either. Other than twiddling individual bits, there isn't much specific benefit to them. C++'s type system has its pros and cons as well; some people prefer strong, dynamic typing while others prefer strong, static typing. C++ is weakly typed.

Quote:The syntax of C++ is actually quite nice when you think about it (and thats why so many languages are copying it).
No. So many languages copy C++ syntax because it is an extension of C syntax, which is the most popular and influential programming language on the planet (sorry, LISPers; C wins this battle). It has nothing to do with merit.
Just as an aside: I've been trying to program in pure C and it literally takes the joy right out of programming. God I hate pure C. C++ is like sweet nectar compared to it.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Quote:Original post by Tron3k
Just as an aside: I've been trying to program in pure C and it literally takes the joy right out of programming. God I hate pure C. C++ is like sweet nectar compared to it.

There are a lot of programmers who will tell you the opposite.
Try to imagine you would have started with C#...
Quote:Original post by petewood
Quote:Original post by alnite
I allocate/deallocate objects in one place, and pass constant pointers around (to prevent unintended deallocation).


It's okay to delete a constant pointer.

Oh, I was talking about pointers to constant objects.

This topic is closed to new replies.

Advertisement