-1

I am beginning development on a substantial personal project and wanted to ask a question regarding data members before doing so.

I am cognizant of the big differences between references and pointers. However, in my recent research, I have not found much (if any) clarification on the differences between data member values and pointers.

Consider the following two class definitions:

class A
{
private:
    const std::string someString_;
};

class B
{
private:
    const std::string *someString_;
};

What are the differences/nuances between the member data of classes A and B? Informed answers and relevant articles would be highly appreciated.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
the_mackster
  • 153
  • 2
  • 9
  • 2
    A::someString_ is a string. B::someString_ is the address of a string. You have to create a string, take its address and store it in B::someString_. – Thomas Sablik Jan 20 '20 at 02:36
  • @ThomasSablik that makes sense. Does A::someString_ share any similarities to what happens when a function parameter is passed in by value, or are those matters completely unrelated? – the_mackster Jan 20 '20 at 02:40
  • Can you elaborate your last question? It's not clear. – Thomas Sablik Jan 20 '20 at 02:42
  • 3
    I would kindly suggest that you take a few more hours to re-read an intro on pointers in C++. `const std::string` is the type of a constant string. `const std::string*` is the type of an address to a constant string. You typically want the former (A), or just `std::string` (without the const) if you except to modify the string later. You would use the latter (B) only if the string is already allocated somewhere else and you want to point to this other string. This is more dangerous, as you have to guarantee that your B object outlives the string it refers to. – Boris Dalstein Jan 20 '20 at 02:42
  • @ThomasSablik Ignore that question, as it was nonsensical. – the_mackster Jan 20 '20 at 02:46
  • Think of an analogy: What is the difference between a city, and a roadsign that directs you to a city? That's analogous to difference between a member of type T and a member that is pointer to T. – eerorika Jan 20 '20 at 02:48

1 Answers1

-1

Similar question has been made here.

Basically, by using value you make a copy and by using pointer you hold a memory address.

A copy means that you already have an instance of the variable type and just copy its contents when you do an assignation.

A pointer means the variable can hold the memory address of an instance or null (none instance or invalid state). You can edit the value or the contents of a pointer. By modifying the value you're holding another memory address, by modifying the contents of a pointer you're actually editing the contents at that memory address.

In fact, the class A you showed, creates a std::string when it is instantiated. And the class B just has an address to a std::string.

You can play with the A's someString_ from the A's constructor. But, you should make a new std::string or pass a reference to a std::string in the B's constructor before attempt to modify the contents of B's someString_.

segoqu
  • 19
  • 3