01 Jun, 2011, JohnnyStarr wrote in the 1st comment:
Votes: 0
Got your attention! :grinning:

I am wondering if someone could display a very simple example of using references vs. pointers for object composition. I am still a little confused.

Here's an example of what I'm describing in Ruby for brevities sake:
class Brain
attr_accessor :man
def initialize(m)
:man = m
end
end

class Man
attr_accessor :brain
def initialize(b)
:brain = b
end
end

m = Man.new()

m.brain # mans brain
puts m.brain.man # self


I realize in ANSI C you would just use pointers, but I am choosing to use references since my embedded objects will be permanent. I also prefer the dot ( . ) operator as well vs. the (->) pointer notation.

Would someone be willing to provide a concise example of the above Ruby in C++?
01 Jun, 2011, Runter wrote in the 2nd comment:
Votes: 0
That's not valid ruby code. Maybe you mean like:

class Brain
def man; :man; end
end

class Man
attr :brain
def initialize; @brain = Brain.new; end
end

m = Man.new()

puts m.brain # mans brain
puts m.brain.man # self

Code validation and output.
01 Jun, 2011, Kaz wrote in the 3rd comment:
Votes: 0
JohnnyStarr said:
Got your attention!


Tragedy of the Commons
01 Jun, 2011, oenone wrote in the 4th comment:
Votes: 0
You have to do it in C++ using pointers, too.
01 Jun, 2011, JohnnyStarr wrote in the 5th comment:
Votes: 0
Meh.

struct Brain{
struct Man& man;

Brain( Man& m ) : man(m) {}

void do_brain_stuff(){}
};

struct Man{
Brain brain;

Man() : brain( *this ) {}

void do_man_stuff(){}
};

int main(){
Man m;
m.brain.do_brain_stuff();
m.brain.man.do_man_stuff();
return 0;
}
06 Jun, 2011, oenone wrote in the 6th comment:
Votes: 0
You have brain local to Man, so you can't reuse the same brain for multiple men…
06 Jun, 2011, Ssolvarain wrote in the 7th comment:
Votes: 0
If I turn my head to the side I can pretend they're odd-shaped glasses of Guinness.
07 Jun, 2011, JohnnyStarr wrote in the 8th comment:
Votes: 0
hahaha. funny stuff.
0.0/8