Sign in to follow this  

Minimalist C++?

This topic is 4351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working with C but I want to get the big picture stuff on C++ without having to delve into the myriad arcane corners of the language. What are the absolute core features of C++ (beyond the C base) to get the most bang for your buck? That is, what are the "can't really do without" parts of C++ that add the most power, etc. Or should C++ be an all-or-nothing sort of affair? Of course different people will have different responses but that's ok.

Share this post


Link to post
Share on other sites
I'm not sure I understand the question, but the features of C++ that make me want to use it OVER C are the following:

exceptions
templates
single-inheritance OOP (I don't like to truck with multiple inheritance)
class methods
function (and operator) overloading

There isn't alot more in C++ that's not in C, but this should illuminate, that while C is C++, there is alot of C++ that's not C.

Besides exceptions and templates, there is nothing in C++ that can't be easily emulated or worked around in C (easily meaning straight-forward, not that it's actually easy to implement VFTs).

Even templates can be managed with the C preprocessor, but the result lacks strong typing, which in my opinion is 50% of the reason to use them in the first place.

But, where does it get you? my old prof. used to say, that C++ is as fast as C + epsilon (where epsilon is very small until you use multiple inheritance). But using the features that I've mentioned, my C++ code is typically half the length of my equivalent C code. This is measuring primary implementation code, not all the support libraries.

Finally, creating support libraries is very comfortable with C++, encapsulation using namespaces and classes is a breeze.

Hope this is enlightening

Share this post


Link to post
Share on other sites
Quote:
Original post by Ned_K
I'm working with C but I want to get the big picture stuff on C++ without having to delve into the myriad arcane corners of the language. What are the absolute core features of C++ (beyond the C base) to get the most bang for your buck? That is, what are the "can't really do without" parts of C++ that add the most power, etc. Or should C++ be an all-or-nothing sort of affair? Of course different people will have different responses but that's ok.


Templates provide a replacement for macros. They provide:
* Type safety
* They can be properly debugged.
* They dont have to be inlined and instantiation can be controlled.

STL means that you'll never have to write another dynamic array again.

OO features such as classes and inheritence. Although C++ is a multi-paradigm language the OO parts it provides are a very natural way of coding.

There are exceptions to all of these but they hold true for 95% of cases.

Like anything C++ is a tool and to say C++ is slow is just a workman blaming his tools. Get a good C++ book, there are loads out there. I could recommend a few, to start: Meyers, Effective C++ and Alexandrescu, Modern C++ Design.

Share this post


Link to post
Share on other sites
Ned_K, I think you have the right idea about learning C++. So many people complain about it being so hard to learn when they try to memorise every possible aspect of the language before they're even comfortable with it. Meanwhile, C++ is designed to be C with extra features you can use if you feel like it, and ignore otherwise.

First thing you need to know for C++ is to write functions and expressions. The basic C stuff. A few things are nicer in C++, such as the fact that you can overload functions. This means that you can have two functions with the same name, but with different argument lists.

Then it's nice to start using classes. Easy - just like structs, but with methods and privacy. And constructors and destructors.

Then you can make you can make your life a little easier by using std::string for strings instead of character arrays.

At this point you probably will benefit from the standard template library. Learn how to use std::vector, std::list, std::set, std::map - they all work basically the same way, so it's hardly any work. Learn iterators too, so you can use these things in for-loops.

Use what you know until you're comfortable. Then, when you feel like it, try out inheritance. Not too hard. If you want polymorphism, simply make your methods virtual. Don't forget virtual destructors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ned_K
What are the absolute core features of C++ (beyond the C base) to get the most bang for your buck? That is, what are the "can't really do without" parts of C++ that add the most power, etc.


Without a doubt, the feature I can't be without in Cplusplus that is not available and cannot be emulated in C, C#, Java, or any other language I've programmed in (not to say I've used 'em all) is something handily referred to as RAII.

RAII requires the use of objects, which are instantiations of classes, and takes advantage of the object constructor and destructor to guarantee certain invariants are true during the lifetime of the object, and that certain pre- and post-conditions are met.

For example, an object encapsulating a file I/O stream can use RAII to guarantee that while the object exists, the file stream is opened and writeable or readable, and that when the object is destroyed the file stream is flushed and closed.

So, if you want to start using Cplusplus, learn the basics of classes, constructors, destrutors, and assignment operators (the rule of three), understand class invariants, and master the use of RAII.

From there, learn how to use exceptions to guarantee class invariants, then learn the standard library (streams, strings, containers, iterators, algorithms, in that order) so you can do something useful with your objects.

After that, learn about inheritance, virtual functions, and how to do Java-style multiple inheritance in Cplusplus. Learn about functors and smart pointers.

The esoteric thing you don't need to learn right away are how to write templates and do metaprogramming, RTTI (run-time type identification), and the more arcane things like fancy ways of newing objects (custom new operators, placement new) etc. Most of boost or tr1. Cplusplus0X.

Share this post


Link to post
Share on other sites
For many years, C++ compilers weren't very good. Hence people tended to avoid features that would cause problems in portable programs:

- RTTI
- Exceptions
- Nontrivial templates

Now, C++ compilers are a lot better than they used to be. They're more consistent and more features are implemented "correctly".

I still don't think it's necessary to know every feature of C++ - to be honest, I don't think very many C++ programmers do.

If you read "Thinking in C++", you'll see that some features, especially OO, constructors, destructors, RIAA and exceptions, are quite helpful to the language and make things a lot easier, especially when it comes to error handling.

Templates are useful in some circumstances, but I find that I don't implement them very often, I just use the standard ones (i.e. STL) instead.

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Quote:
Original post by Ned_K
What are the absolute core features of C++ (beyond the C base) to get the most bang for your buck? That is, what are the "can't really do without" parts of C++ that add the most power, etc.


Without a doubt, the feature I can't be without in Cplusplus that is not available and cannot be emulated in C, C#, Java, or any other language I've programmed in (not to say I've used 'em all) is something handily referred to as RAII.

I wouldn't go so far as to say it "can't be emulated". It's quite easy to have RAII in C#, for instance, by simply having a using statement:

using (ResourceObject resObj = new ResourceObject(@"C:\texture.tga"))
{
// do stuff
}
// resObj is destroyed and properly disposed of

Share this post


Link to post
Share on other sites
Other than MFC there is little reason for me to use C++. I mostly use C because that's all I need in most cases. The minimalist C++ would be object based programming.

Share this post


Link to post
Share on other sites
Quote:
Original post by JD
Other than MFC there is little reason for me to use C++. I mostly use C because that's all I need in most cases. The minimalist C++ would be object based programming.


MFC is not affiliated with C++. MFC is a Windows only set of classes (a feature of c++) used for Windows-only development. C++ can and has been used on many, many platforms and hardware environments. It is a general purpose systems programming language.

Share this post


Link to post
Share on other sites

This topic is 4351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this