-2

I am new to C++ and one of the things that seems interesting, but quite confusing sometimes, are pointers and references.

To me there is no book on C++ explaining them enough, so I grabbed one on C language (Understanding and Using C Pointers: Core Techniques for Memory Management that goes quite deep on the topic.

I know C is subset of C++ but at the same time they are two different languages.

So here is my question: is the way they are used same in both the languages, or are there any (significant) differences?

Will all I learn about C pointers have the same "validity" in C++?

  • https://stackoverflow.com/questions/4305673/does-c-have-references – stark Nov 16 '18 at 19:13
  • 1
    There aren't any major differences between pointers in the two languages. References exist only in C++ (not to be confused with passing function parameters "by reference", which can be done with pointers and references alike). – HolyBlackCat Nov 16 '18 at 19:13
  • 2
    Idiomatic C++ strongly prefers direct ownership and smart pointers over raw `T *`. – melpomene Nov 16 '18 at 19:15
  • The pointers are probably not explained that deeply in C++ books because C++ code tends to avoid using "naked" pointer, preferring collections and smart/automatic pointers. – Suma Nov 16 '18 at 19:16
  • 2
    @melpomene there is a perfectly legal non-owning T* which is idiomatic in C++, at least until Core Guidelines are really used everywhere. – SergeyA Nov 16 '18 at 19:16
  • @SergeyA Sure, but "Core Techniques for Memory Management" in C will look nothing like typical C++ code. – melpomene Nov 16 '18 at 19:18
  • While C++ and C are closely linked, C is not strictly a subset of C++. – Christian Gibbons Nov 16 '18 at 19:20
  • You can do basically all the same things with pointers in C++ as you can in C, **However**, learning how to use pointers by learning C will teach you a C mindset, which is _very_ different and far more error-prone than what modern C++ has to offer. Using pointers as freely in C++ as you would in C will easily lead to a plethora of bugs and maintenance nightmares. – alter_igel Nov 16 '18 at 19:23
  • Speaking of the core guidelines, [here's a link to what it has to say about resource management](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-resource) (memory being an important resource). – user4581301 Nov 16 '18 at 19:33
  • 2
    >However, learning how to use pointers by learning C will teach you a C mindset, which is very different and far more error-prone than what modern C++ has to offer.< Maybe that is so. But - **without** learning about C-style pointers and memory management, one has almost no chance of understanding references and other highly abstracted language constructs in more modern languages, leading to a plethora of misguided code and plentiful adaptation of boiler-plate code that one does not really understand. – GermanNerd Nov 16 '18 at 19:53

1 Answers1

0

Almost all you learned about C pointers will be valid in C++. You can try and see the differences in compiler errors (if you cannot find any book explaining them enough).

C is not really a subset of C++.

Nathan Xabedi
  • 1,097
  • 1
  • 11
  • 18