2

I am trying to learn c++ and am following this online book and although mostly good, sometimes I feel things aren't explained that well.

On page 163, when talking about linked lists, he shows this code:

p_enemies = EnemySpaceShip* addNewEnemyToList( p_enemies );

It isn't completely filled in with context, but I believe p_enemies would be a declared pointer, and addNewenemyToList would be a defined function. What I am wondering is how the expression would work, and is this proper code. It doesn't make sense to me because it looks like a method declaration, having the return type right there, but it is there after an assignment. What is this?

No Context
  • 167
  • 1
  • 6
  • 2
    The only way this could work is if `EnemySpaceShip` is an object, in which case it would be multiplied by the result of the function call. Are you sure there were no braces that casted the result of the function? – chris Oct 30 '12 at 05:28
  • btw, the link to online book is currently a link to an attachment in your gmail, so we can't view it :P – Mike Trusov Oct 30 '12 at 05:30
  • 3
    @MikeTrusov, Seems to be this one: http://www.scribd.com/doc/105834938/Jumping-Into-c – chris Oct 30 '12 at 05:31
  • 1
    Ok, looking a bit before that in the book, casting would be useless. I believe it's supposed to be: `p_enemies = addNewEnemyToList( p_enemies );` – chris Oct 30 '12 at 05:36
  • 2
    Looks like an error in the book to me. I think it should be `p_enemies = addNewEnemyToList( p_enemies );` – Vaughn Cato Oct 30 '12 at 05:36
  • 2
    For what it's worth, we do have a [good book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I can't personally give an opinion on that one. – chris Oct 30 '12 at 05:37
  • 1
    p46: Here’s a very basic example of a compiler error message: `error: 'cout' was not declared in this scope`. If you see a message like this, make sure that you both have your include statement for iostream and `using namespace std;` at the top of your program!_ Not yet convinced. – Jonathan Leffler Oct 30 '12 at 05:45
  • 1
    I would not recommend this book. Explaining pointer arithmetics before standard containers? Well, if it was called "Jumping into C with some C++ bolted on" I would understand that. – n. m. could be an AI Oct 30 '12 at 06:34
  • Please, please, please, for the life of your future coworkers, STOP READING THAT BOOK, forget what you've seen there and pick one from the already mentioned [Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1) – Jan Hudec Oct 30 '12 at 14:37

1 Answers1

1

That code snippet does not make sense.

This is the only way I could see this snippet as valid:

p_enemies = (EnemySpaceShip*) addNewEnemyToList(p_enemies);

Result: If p_enemies is a pointer of the type EnemySpaceShip, this will assign the return value of the "addNewEnemyToList"-function to p_enemies after converting the returned value to a EnemySpaceShip pointer. If not, the line will fail.

John
  • 96
  • 3