C++ - When to use pointers/references?

Started by
21 comments, last by Trienco 17 years, 11 months ago
The posts about using refs where you have to have valid pointer and pointers for the possiblity of null is a correct answer. You can choose to be as flexible as you want but try to stay with a rule or style to avoid confusion.

Ask the question, is it an abstract collection or concrete instances. I use refs when passing a helper or adaptor class to a new class instance or function. I use Pointers when working with collections or where the object is not always instantiated.

Advertisement
Quote:Original post by izhbq412
Quote:Because the code flow with gotos is very hard to understand to someone who isn't intimately familiar with the code?

Do you speak from experience or did you read that in a book? Because I don't find using gotos for breaking out of a nested loop difficult to understand at all. Although I do prefer a private (and hopefully) inline function if I'm using C++, not C. Sometimes.

Sorry for the offtopic, but he started it ;)


I find the change in code flow annoying.

I find the situations that often demand a goto (multiple nested loops going on at once, with special exceptions to exit) difficult to hold in my head as a single concept.

Finally, I find that gotos lead to fragile code -- because you can easily skip past variable initialization.

So when I see a goto and am trying to understand what is going on, I'm usually in an already multiple-nested procedure, and now I have to figure out if any undefined behaviour slipped through.

Heck, why don't they just throw an exception to exit a loop? Because locally you have no guarantees where either the goto or exception is going to land. Sufficient comments may make up for this, naturally. :)

Can you tell me what the following does without compiling it?
// prints the note on construction and destruction:struct CtorDtorNote {  const char* note;  CtorDtorNote(const char* note_):note(note_) {    printf("Ctor {%s}\n", note);  }  ~CtorDtorNote() {    printf("Dtor {%s}\n", note);  }};// play with gotos!void func() {  CtorDtorNote no_loop = "start of func";  {for (int i = 0; i < 10; ++i) {    CtorDtorNote in_loop = "first note";    {for (int j = 0; j < 10; ++j) {      CtorDtorNote in_loop2 = "second note";      if (j ==2) goto OUT_OF_LOOP;    }}  }}  CtorDtorNote no_loop2 = "strange days";  OUT_OF_LOOP:  return;}
Quote:Original post by NotAYakk
Finally, I find that gotos lead to fragile code -- because you can easily skip past variable initialization.


Uhm, maybe that's just a special feature of my compiler, but afaik C++ is supposed to be "goto-safe". Goto isn't some disguised asm-op that's free to ignore all rules. Initializing a variable in a part that can be skipped by goto will get you a compiler error. Goto'ing out of some scope WILL free all variables local to this scope. Using goto isn't any less problematic than using new and forgetting delete. But yes, if you have more than the casual goto in rare cases and start peppering your code with them, I wouldn't want to read it.

Quote:So when I see a goto and am trying to understand what is going on, I'm usually in an already multiple-nested procedure, and now I have to figure out if any undefined behaviour slipped through.


Or... you could simply expect your compiler not to suck and throw an error, if something that actually IS part of C++ (and not some virus that smuggled itself into the standard) is used in an illegal way.

Quote:Can you tell me what the following does without compiling it?


if (j ==2) goto OUT_OF_LOOP;


Destructors will be called for each object created inside the loops you are breaking out of. If they aren't, throw away your compiler.

CtorDtorNote no_loop2 = "strange days";OUT_OF_LOOP:


Your compiler will tell you, that the initialization is skipped by the goto, just like it would inside a switch block. Again, if it doesn't, get another compiler.

And if you DO need this no_loop2 for something in the meantime, you can do the same as you would in a switch block. Introduce an artificial scope and do {CtorDtorNote no_loop2 = "strange days";} and everything is fine (except for the brackets looking weird if they are just standing around in the middle of nowhere).
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement