Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

* vs & in C++

Name: Anonymous 2006-10-22 23:00

With a function

void foo(Doodie& poop)
{
  poop.set("diarrhea");
}

is there any difference at all in how it compiles vs

void foo(Doodie* poop)
{
  poop.set("diarrhea");
}

?

It's also strange that you use . instead of -> when you pass-by-reference via * even though pointer members are typically accessed with -> and not .

Name: Anonymous 2006-10-25 18:01

Just as an aside, Bjarne Stroustrup says in his book that he prefers pointers when a function changes object foo:
#include <string>
#include <iostream>

class foo
 {
   public:
   foo (std::string s) : bar(s) { }
   ~foo () { }
   std::string bar;
 };

void setbar (foo *foo)
 {
   foo->bar = "goodbye 4chan\n";
 }

int main ()
 {
   foo a;
   std::cout << a.bar;
   setbar(&a);
   std::cout << a.bar;
   return 0;
 }
By passing an address you sort of communicate to the reader that object foo has changed or is in a different place after the function returns.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List