anon235370
anon235370

Reputation:

Thinking in C++?

The programming language I use has been Java. I've been transitioning to C++ which has been somewhat rocky. The "rocky-ness" isn't in the learning but more along the lines of "Thinking in C++".

I've seen many people say that you should learn C first (I technically know it already), and then I see people say no skip C and go straight to C++. Once again this isn't a "learning" problem; it's a "thinking in" problem; I know the syntax to C++, I know OOD in C++, etc.

For example this code in SDL I can explain it as if I know it but I cant really "take advantage" of pointers:

SDL_Surface* hello = NULL;

Should I be programming in C to get "used" to this? What should I be doing to get used to pointers if I've never used them coming from a language such as Java?

Upvotes: 19

Views: 7341

Answers (11)

John
John

Reputation: 11

This question has nothing to do with thinking in C++. You just need to learn more C++. It's good that you already know Java, because just about everything important in Java is directly present in (and indeed borrowed from or influenced by) C++, so you just need to learn more C++ to learn how to "map" those Java design ideas into C++ implementations.

Do not learn C to learn C++. That's a stupid myth. They are two different languages. Learn C++ to learn C++.

Upvotes: 1

utnapistim
utnapistim

Reputation: 27365

Should I be programming in C to get "used" to this?

No, you shouldn't. C used to be a good introduction to C++ when C++ was mostly thought of as "C but with classes". C++ has evolved so much from that mindset that writing C code in C++ makes for very very bad C++ code.

What should I be doing to get used to pointers if I've never used them coming from a language such as Java?

Knowing how to use pointers in C++ implies several key areas (and you should probably learn them one after the other):

  • pointer arithmetic (adding and substracting pointers and integers, usage of zero pointer, pointers and arrays, operations with pointers); this is the part C++ shares with C.

  • pointers to functions (this part is also shared with C).

  • pointer casting (static_cast, dynamic_cast, ...). This part is C++ specific. You could also define your own cast type and experiment with that a bit (and have a look at boost::lexical_cast as an example).

  • pointers to member objects and member functions.

  • RAII for pointers and various smart pointer implementations: circularly-linked pointers, reference-counted pointers (boost::shared_ptr), unique pointers (std::auto_ptr), scoped pointers.

  • wrapping function pointers in functor classes (see std::tr1::function or boost::function).

  • object references and how they are like and unlike pointers; Where to use references and where to use pointers.

  • the similarities between pointers and iterators.

  • using operator* and operator-> to simulate pointer-like behavior for an object (and when not to abuse this).

  • pointers and constness / mutability: what is the difference between a pointer, a const pointer, a pointer to a const value and a const pointer to a const value (and when to use each), and similar using mutable pointers.

Current best practices in C++ advice against using pointers in your code unless they are managed automatically by your code (through RAII for example).

Upvotes: 9

Owen S.
Owen S.

Reputation: 7855

I say focus on the topics you're having problems getting your head around. Pointers are for the most part the same in C and C++, so pick whatever resource will help you the most.

My girlfriend had trouble in pointers when starting out in C++, and she recommends a book called Understanding Pointers in C.

What's to get about pointers that's specific to "thinking in C++"?

  • Pointers can be set to NULL (i.e. 0). This can be fabulously useful to model situations where you might have a value.
  • Pointers can be repointed to new objects – as opposed to, say, references, which only get initialized once and cannot be repointed after initialization.
  • Pointers can be implicitly upcast (from derived classes to base classes).
  • Pointers can be explicitly downcast from base classes to derived classes. You can do this with static_cast if you happen to know what the derived class really is! People generally do this with dynamic_cast for the extra runtime sanity check.
  • Pointers can be cast to and from void* if you want to pass a pointer to someone but you don't want them knowing the kind of object that you're pointing to. In other words, pointers can be made opaque.
  • Pointers and arrays are nearly equivalent.

Here are some things related to pointers that are unique to C++ vs. C:

  • C++ has the concept of references, which can be used in many of the situations pointers are used in, but are much more difficult to screw up (in terms of dereferencing an invalid address) than pointers.
  • C++ has the concept of pointers to member functions that are a bit different from pointers to functions, which C also has.
  • In C++, you can define your own objects which behave like pointers – i.e. that you can dereference using the * unary operator. C++ libraries use this feature to implement smart pointers, which are a whole category of objects that behave like pointers but which also have some layer of access or ownership management built into them.

Upvotes: 2

Tom
Tom

Reputation: 663

A programming language is just a tool. The bottom line is, the more you know the better. C is a very good language to learn IMO, for the very same reasons one of the co-founders of stack overflow (Joel Spolsky) mentions in this article: http://www.joelonsoftware.com/articles/CollegeAdvice.html. I read this article before I graduated and made it a point to take his advice on learning C before I graduated.

At the end of the day, software written in any language is run on (or by) a computer chip. It's very important to understand (if you want to be a 'good' programmer anyway) what the cost is (in cpu time and programmer time) of the mechanisms you are using in whatever language you are using. High-level languages are great for programmers in some cases because they allow you to implement powerful end-user features more quickly than lower level languages such as C. However, lower-level languages in general provide better performance. The only thing you as a programmer need to consider is 'what is the right tool for the job?' You don't use a hammer to drive in a screw, blah blah insert other cooker cutter analogy here, etc.

One thing that high-level languages tend to hide from the programmer is pointers. Pointers is a very important concept to be familiar with whether you program in a language that hides them from you or not. C is a great language to learn pointers (or C++ for that matter). Recursion is another big one, but I don't think any language can really hide this concept... it's a generic problem solving concept whereas pointers is more of a mechanical detail.

Lastly, my degree did not actually require me to take a compilers course. I could have ducked it, but since it is one of the most notoriously difficult classes, I felt I would be less of a man if I didn't take it. I did horrible grade wise, but I learned so many important things in that class it was well worth the proverbial rock I tied to my GPA by taking it. If you learn how a programming language is actually implemented then you have a huge advantage vs just knowing how to work with 'language X.' Any programming language takes time to learn, even if you do have all the fundamentals in the world. Random jerks everywhere will try to tell you how easy it is to learn 'language X,' but these people are usually just insecure and want to make themselves feel good by acting like they were born with an ability to program in 'language X' when really they have just put in the hours to learn it. Nobody is born knowing this stuff, just give yourself time to learn but be smart about it. Develop a systematic method of learning a new language (my strategy always starts with writing 'hello world' in the language) and you will do fine. END RANT.

Upvotes: 3

rwong
rwong

Reputation: 6162

If you are already working in a C++ project on a daily basis, chances are that you are already getting used to using pointers. It just takes longer time for the knowledge and recurring patterns to sink in. (If I remember correct a famous C++ guru says it takes 6 months to a year to become productive.)

A good debugger, especially one which shows the original source code and the compiled assembly code at execution time, will help you understand better.

To echo Evan Thinking in C++?, C++ is a multi-paradigm language. To think in C++ is mainly about thinking in paradigms. For example, libtiff is entirely based on function pointers and COM is entirely about OOP using nothing but pointers, vtables and reference counting. MFC wraps a lot of things into macros and tables.

At the beginning of a project, you have a chance to choose the right paradigm. Sometimes it is limited by the project requirements especially if it needs to interface with an external environment. Once the project gets going, it will be difficult to switch. Often the choice was made for a good reason.

The only way to learn about a paradigm is to work in a project that uses it.

Upvotes: 0

Ashish
Ashish

Reputation: 8529

If you are really thinking of mastering pointers concept , I will suggest this book:

Understanding Pointers in C by Yeshwant Kanitkar .

Its very lucid and organized book that will give you the deep insight of pointers starting from scratch to the applications of pointers.

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 57902

If you understand the syntax, then the only way to become "fluent" in a language is to use it.

Using C is a good way to keep things simple and get close to the "metal", but it isn't the same as C++, so ultimately I would advise using C++ if you wish tolearn C++. (i.e Don't learn Latin if you want to be fluent in Italian, even though Latin will give you many insights, the two languages are different beasts, and the thought processes involved are correspondingly different)

Pointers are very easy, once you "get" them. A very good idea when using pointers is to use the "p" prefix notation. For each pointer (level of indirection) add a "p" to the front of your variable name to remind you that it is a pointer:

Vehicle vehicle     = an actual vehicle
Vehicle *pVehicle   = a pointer to a Vehicle
Vehicle **ppVehicle = a pointer to a pointer to a Vehicle

To access the vehicle you have to dereference once for each pointer, i.e:

vehicle.SetName("Ford Focus");       // Directly access an object instance
(*pVechicle).SetName("Ford Focus");  // Dereference once
(**ppVehicle).SetName("Ford Focus")  // Dereference twice

There are three ways to dereference (just different syntaxes that generate identical code):

(*pVehicle).DoSomething()
pVehicle->DoSomething()
pVehicle[0].DoSomething()

When you use arithmetic on pointers they operate in Vehicle units, not in bytes, i.e

pVehicle++;   // Move to the next vehicle record, not just the next byte

And finally, for each

pVehicle = new Vehicle

you need to balance your code with:

delete pVehicle;
pVehicle = NULL;

(edit) Oh, and almost forgot the most important one: ALWAYS check that your pointer is valid before you try to use it!

if (pVehicle != NULL)
    ...use pVehicle

I find the "p" notation invaluable as you never forget that something is a pointer, and the number of p's tells you how many dereferences are needed. Once I adopted it, I stopped writing NULL dereference/access violation bugs.

That's all you need to know about pointers. Now you just have to use them a lot to gain fluency.

Upvotes: 13

Omnifarious
Omnifarious

Reputation: 56048

In my opinion, C++ as a language is so complex that understanding of it is still evolving. I'm still discovering new programming techniques. And C is no longer a good introduction to C++. In fact, becoming too steeped in C will teach you bad C++ habits and will tend to blind you to some of the really useful things in C++. For example, in C++, I consider pointers to be more important than in Java, but not nearly so much as in C.

There is no magic book you can read or formula you can follow to learn to think in C++. You have to practice with the concepts. Write your own programs that use them, even if they are just toys.

Here is my personal list of important C++ ideas that you should practice with and grow used to using:

  • Using swap (and in C++0x the move constructor and assignment operator) to write code with a strong exception guarantee.
  • Template metaprogramming (templates are basically a functional language inside of C++ who's output is a C++ program).
  • The RAII idiom (RAII stands for resource acquisition is initialization) - this is something in C++ that is fundamentally different from Java, and very important to understand.
  • And, related to RAII, the concept that objects of non-primitive types are routinely created without using new. You create objects directly on the stack or refer to them by inclusion rather than by reference. This is a huge, but subtle difference from Java.
  • Iterators - what the non-Javaesque use of pointers has turned into for the most part
  • And really, pointers that aren't used as iterators are just the same as variables of non-primitive types (i.e. stuff derived from Object) in Java, except for the lack of garbage collection.

Upvotes: 1

Evan Teran
Evan Teran

Reputation: 90422

I don't think that learning C will help with your "thinking" in c++". Generally, you just need to get used to certain C++ concepts and why they are useful.

Key ones are smart pointers (more generally RAII) and templates which are huge.

Also keep in mind that c++ is a multi-paradigm language, not strictly OOP. Often the best solution isn't very object oriented at all (the STL is a great example of templates being better design than an object based one).

Upvotes: 6

lavinio
lavinio

Reputation: 24309

It's true that pointers are a big deal, and C and C++ share that concept.

But C++ is not just C with objects.

There are two free books that you can download from Planet PDF by Bruce Eckel called Thinking in C++ (volumes I and II) that will really help you get the sense of C++.

A lot of C++ also is the toolkit, whether it be the standard library (std) or MFC or the classes from the anti-grain geometry library or whatever. That too makes a difference.

If you need C++, go for C++. Learning C first is like learning Spanish so you can learn Portuguese, instead of just aiming for Portuguese.

Upvotes: 13

Adrian Petrescu
Adrian Petrescu

Reputation: 17969

You know, there's a very well-regarded book that's actually entitled "Thinking in C++" :) Even better, it's free! It should help you grok things like pointers on a deeper level -- it's explicitly geared towards that.

Upvotes: 3

Related Questions