1

Recently I've been learning C++ after only using web programming so far things have been going quite well working through the cplusplus tutorials. One thing I'm struggling to get my head around though is the use of pointers referencing objects in a data structure. Basically:

string mystr;
movies_t amovie;    // create new object amovie from structure movies_t
movies_t* pmovie;   // create a new pointer with type movies_t
pmovie = &amovie;   // reference address of new object into pointer

cout << "Enter movie title: ";
getline(cin, pmovie->title);
cout << "Enter year: ";

getline (cin, mystr);

(stringstream) mystr >> pmovie->year;

cout << endl << "You have entered:" << endl;
cout << pmovie->title;
cout << " (" << pmovie->year << ")" << endl;

Can be written just as easily as:

string mystr;
movies_t amovie;

cout << "Enter movie title: ";
getline(cin, amovie.title);
cout << "Enter year: ";
getline(cin, mystr);

(stringstream) mystr >> amovie.year;

cout << endl << "You have entered:" << endl;
cout << amovie.title;
cout << " (" << amovie.year << ")" << endl;

I understand their use in arrays, but am struggling to grasp why using pointers would be preferable than referencing the values themselves from a structure.

Mike8580
  • 45
  • 5
  • I think the only point in this sample is to show that a pointer is used to hold the address of an object. I don't see any other real value in using `amovie` and `pmovie`. – Marius Bancila Sep 14 '13 at 11:48
  • 2
    In this case, there is no reason to use pointers to structures. – Nemanja Boric Sep 14 '13 at 11:48
  • Quite right, avoid using pointers until you can see why they are (occasionally) needed. Then it will be clear. Hint: sometimes you need to dynamically create data structures. C++ has lots of built in data structures, string, vector, map etc. but just occasionally you need to roll your own. – john Sep 14 '13 at 12:59

1 Answers1

2

I understand their use in arrays, but am struggling to grasp why using pointers would be preferable than referencing the values themselves from a structure.

They’re not. Pointers should only be used if you cannot reference the variable directly for whatever reason (e.g. because the value to be referenced may change).

Apart from that, your use of the C-style cast here is certainly creative. But don’t do this. C-style casts are generally not acceptable in C++. Use a static_cast here:

static_cast<stringstream>(mystr) >> amovie.year;

Or at least use a function-style cast:

stringstream(mystr) >> amovie.year;

… but actually the whole line of code (including the declaration of mystr) is completely useless. Just read the value directly:

cout << "Enter year: ";
cin >> amovie.year;
Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • This whole casting thing confuses my here: Can you really cast a `string` to a `stringstream` and get working code? Also with the function-style case here: Is `stringstream(mystr)` a cast of `mystr` to `stringstream` or is that calling the `stringstream` constructor with `mystr` (which will work just find)? – dornhege Sep 14 '13 at 12:38
  • 1
    @dornhege It's a really weird way of saying `stringstream(mystr)`. IOW, casts use constructors when applicable. But if I encountered such a cast in production code, I would review-reject it well into last week. – Angew is no longer proud of SO Sep 14 '13 at 12:56
  • 1
    @dornhege Technically, `stringstream(mystr)` is a function-style cast, and semantically 100% to the C-style cast `(stringstream)mystr`. Both end up calling the appropriate constructor, because it exists. If no applicable constructor existed the cast would either fail or do something very weird, which is why people advise against this usage. – Konrad Rudolph Sep 14 '13 at 14:39
  • "Do something very weird" seems like the best argument I've heard so far to not cast this way... – dornhege Sep 14 '13 at 15:08