> I think that one of the main reasons why C++ is hated by > so many people is because they want to translate the same > idioms as in other languages and they get a wrong view of > what's possible with it. C++ is a language that when you > master it, you can have a level of control you simply > cannot have with Java or C#. > But people tend to prefer things that get the job done > quickly. The learning curve for C++ is steeper than that > of Java, but with the time it pays what you invest in it, > from my point of view.
This is simply not true. Here is an example:
I have a list of objects.
In Java or C#, the objects are by reference, and therefore I do not need to worry about memory management.
The C++ gurus say "use an STL container", right?
Ok, I can use an STL container. But I want these objects to be used in another part of code, so I can't treat them as value objects: I have to store a reference in the STL container.
At this point, the C++ gurus have nothing to say, as they magically never had this problem.
One way to solve the problem is to use shared pointers.
Ok, I can use shared pointers. But in a piece of code that is part of a 3rd party library that does not use special pointer classes, raw pointers to my objects exist. And the library gives me back raw pointers to objects.
I can't use shared pointers here, because if I instantiate a shared ptr from a raw pointer and there is another shared ptr somewhere else, there are going to be two different reference counts of the same object, the object will be deleted by one of the shared ptr and the other will crash the application.
A solution for this problem is for my classes to contain an embedded reference counter, so all shared ptrs use this counter and therefore I don't have the problem of using raw pointers in 3rd party libraries.
Ok, I can do that by inheriting my classes from a common base class that contains the reference counter, but what if one of my classes already has a base class from a 3rd party library that does not inherit the reference counter?
I can use multiple inheritance! great, my classes that are derived from classes of 3rd party library can now be used with shared ptrs because they are also derived from class 'Shared' (e.g.).
But wait a minute. Does the shared ptr class use the appropriate conversion from Shared to T (where T is the reference counted derived class) or Shared has to be the first class in the base class list? I have to open the shared ptr class implementation to check that.
Ok, I can do that. Damn, I have to use another shared ptr implementation, because the one I have does a (T *)shared instead of 'static_cast<T *>(shared)' as it should. Let's download boost.
Ok, I downloaded boost. I know have to compile it. What? I need a special tool? bjam? what's that? damn, another tool...
Ok, I did that, and I examined the boost code, and it does the proper conversions, and I can finally share my objects in the different parts of my programs.
Everything ok?
Yes...I run the program with a memory checker, but to my surprise, there are heavy memory leaks. What has just happened?
Well, a Factory class of the 3rd party library creates instances of objects that contain pointers to my objects. But my objects contain pointers to instances of those objects as well! cycles are introduced, which create memory leaks. How to solve that?
Well, instead of storing the shared pointer to the object returned by the factory, I can create a weak reference class that becomes null when the object of the factory is deleted, and thus breaking the cycle.
Well, it works, but I had to change my classes once more to inherit from boost::trackable in order to use weak pointers.
Everything ok? well, not quite yet!!!
Now my program crashes because the weak pointer becomes null, where in the previous version that used only shared ptrs did not...I now have to redesign my program again!!!
Or I could do the thing in Java and have spare time to watch the football match...
> > Microsoft operating systems are not written in C++. I studied the leaked sources of W2K a few years ago, and it's all C. Microsoft Office is in C++ (i.e. was), that's why it crashes.
C doesnt crash and C++ does? thats the most stupid thing I have hear in my whole life
> > But shared_ptr recently became part of the standard, and it's not supported by any compiler yet. And therefore every C++ library I know of does not use shared_ptr.
Inform yourself
> > Why C++ is more powerful? it's not. In fact, Java is vastly more powerful than C++, and the complexity of Java projects can't be approached by C++.
lol?
shut up please, let this topic die along with your comments
Flat View: This topic has 31 replies
on 3 pages
[
«
|
123
]