0

I'm new to C++ and now I'm stuck with this problem about pointers and references that I'm not able to understand.

I've read a little bit about pointers and references but the notation is killing me.. It's just really confusing.. I need some help, with some plain simple explanation, no advanced or optimized obscure techniques here please, just plain and simple explanation of what's going on here to understand how this pointers and references work.

The case is the following:

Inside a Main class there is a ConnectionHandler object, this object is instantiated with an empty constructor and it is not yet in a usable state. In the same Main class there are also two other objects InputListenerTask and NetworkListenerTask.

Both InputListenerTask and NetworkListenerTask receive the same ConnectionHandler instance.

At some point inside InputListenerTask, the user will enter the ip, port, user, pass and this information needs to be setted to the ConnectionHandler.

All this is done inside the InputListenerTask class, and while InputListenerTask keeps running it needs to be passing this ConnectionHandler instance to several other classes that need to use this to send data over the network, also NetworkListenerTask will also start functioning and using the same connection, passing it around to it's helper classes.

I think I understand how to pass by reference from class A to class B, but it starts getting confusing when I need to store the reference as a field in B and pass it again by reference from B to C..

J. Bend
  • 299
  • 2
  • 3
  • 14
  • 4
    I guess you should show us some code. – Kit Fisto Jan 13 '14 at 17:34
  • You shouldn't be dealing with network code if you are trying to understand pointers and references. Network code has plenty of side-issues and implementation details. Find some simpler examples. – Andrey Mishchenko Jan 13 '14 at 17:36
  • 2
    IMO it is easier to first learn to deal with *pointers*, ignoring the existence of references, preferably in the context of (the much simpler language) C rather than C++. Once you understand pointers references are easy to pick up. – Andrey Mishchenko Jan 13 '14 at 17:37

2 Answers2

1

You can just pass the reference along:

void f1(int& a) {
   cout << a;
}

void f2(int& b) {
   f1(b);
}

int c;
f2(c);

Since a reference is an alias for the actual object is does not matter whether you pass an object or a reference to the function.

Kit Fisto
  • 4,385
  • 5
  • 26
  • 43
  • 1
    Why pass a by reference to just print it. I feel your example should change the value of a in functon `f1`. Other than that, seems legit to me. – Brandon Jan 13 '14 at 17:42
  • @CantChooseUsernames there is no reason to pass an `int` by reference if you won't be changing its value, but for other (larger) data types passing by `const` reference avoids the overhead of copying to satisfy the C++ pass-by-value semantics. – Andrey Mishchenko Jan 13 '14 at 17:44
1

So in your Main it'd look like this:

ConnectionHandler ch;
InputListenerTask ilt;
NetworkListenerTask nlt;

ilt.foo(ch);

In InputListenerTask you'd have:

InputListenerTask::foo(ConnectionHander& handler){
    ...
    SomeOtherClass s;
    s.doSomethingWithCh(handler);
    ...
} 

and in your SomeOtherClass you'd have:

SomeOtherClass::doSomethingWithCh(ConnectionHandler& ch){
    ...
    ch.x = 42;  // Assuming ConnectionHandler has a publicly accessible field x
    ...
}

Just think of the & reference notation as saying "Anything you change in this variable is going to affect whatever you passed in".

FuriousGeorge
  • 4,561
  • 5
  • 29
  • 52
  • This is pretty much what i need, I just forgot to write that the InputListenerTask gets the ConnectionHandler as a construction argument so really has a field that stores the reference to ConnectionHandler and then passes it around.. How would you do this? – J. Bend Jan 13 '14 at 17:57
  • So in `InputListenerTask` you'd have `ConnectionHandler& myHandler;` as a field, and for the constructor you'd have `InputListenerTask::InputListenerTask(ConnectionHandler& h):myHandler(h){ ... }` See http://stackoverflow.com/questions/16634181/uninitialized-reference-member-in-c – FuriousGeorge Jan 13 '14 at 18:40
  • This link is a little bit better http://stackoverflow.com/questions/4589237/c-initialization-lists – FuriousGeorge Jan 13 '14 at 18:58