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

Pages: 1-

* 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-23 1:20

your second function fails because poop.set should be poop->set.  thus, yes, there is a difference in how they compile.

Name: Anonymous 2006-10-23 1:34

>>1
anonymous@anonymous:~$ md5sum foo-1.o
2b974101befb97b5df7ef66d754e32ae  foo-1.o
anonymous@anonymous:~$ md5sum foo-2.o
18e8fbb079bb6a78ee61d8f1a61dda88  foo-2.o

Name: Anonymous 2006-10-23 1:48

>>2

Nope nope, when a pointer is an argument, the compiler bitches if I do use -> for it.  Every other occasion, I've had to use -> to access pointer members.  But for some reason, it (both Microsoft's C++ compiler as well as GCC, I haven't tried any other C++ compiler) seems to expect . for function-parameter pointer variables.

Name: Anonymous 2006-10-23 2:23

Erm

Disregard this topic... I had a serious brainfart.  Don't ask, lol.

Oh and I got off my ass and tried it with two tiny .cpp files that passed the reference with different syntax -- they both compiled into identical .s (asm) files.

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.

Name: Anonymous 2006-10-25 21:53

>>6
alternatively, just use const if you don't intend to change something.

Name: Anonymous 2006-10-25 21:54

>>6

Yeah, although & is also an address of sorts, although unlike pointers you can't accidentally pass an arbitrary value -- & lets you pass-by-reference only on existing objects.  So & is a bit safer.

As for communicating to people reading your source code, I do this by sticking to strict const-correctness.  If you see:

void Foo::nigger(const Poop& doodie)
{
...
}

you know doodie won't be changed, and I'll only do

void Foo::nigger(Poop& doodie)
{
...
}

if and only if doodie will be changed.

Name: Anonymous 2006-10-26 5:37

>>6
Who cares what Stroustrup thinks, the guy's a moron. I mean come on, he came up with C++.

Name: Anonymous 2006-10-26 5:55

>>6

Just because he invented it, doesn't mean we have to all use his style. Anyway, many others have contributed to it as well, it's not a one-man job.

Name: Anonymous 2006-10-26 7:16

>>6
If bjorn jumped off a cliff, WOULD YOU?

Name: Anonymous 2006-10-26 7:31

>>9,10,11 Point taken.

Name: Anonymous 2006-10-26 7:35

>>../../img

Name: Anonymous 2006-10-26 12:05

>>11
Yes, to make sure he's dead.

Name: Anonymous 2006-10-26 15:21

>>9
Signed. Signed. Signed. Signed. Signed!

Name: Anonymous 2006-10-26 18:37

>>6
in a different place? are you drunk? or do you often use pointers of pointers to move them all around the place? lol noob

Name: Anonymous 2011-02-04 17:53

Name: Sgt.Kabu돫䓮kiman诉ⶽ 2012-05-28 20:34

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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