Reputation: 57227
I've got a program with a treeview class, a console class, and an options class.
I'd like to pass the treeview object to the options object, and be able to access dynamic values inside of the treeview (file lists and so on).
I've tried passing by reference, and it compiles, but through a few debug messages, I can tell it's not the same object, so the values are all empty.
Options panel Init
header:
public:
void Init (HWND, PnlConsole&, PnlTree&);
...
private:
PnlTree tree_;
PnlConsole console_;
...
Options panel Init
function:
void PnlOptions::Init(HWND hwnd0, PnlConsole& console0, PnlTree& tree0) {
tree_ = tree0;
console_ = console0;
...
Instantiation of classes in main
file:
PnlTree pnl_tree;
PnlOptions pnl_options;
PnlConsole pnl_console;
Call to Init
inside main
function:
pnl_options.Init(hwnd0, pnl_console, pnl_tree);
I've been working at this for a long time (as some people have read on my previous questions) and it's very frustrating. Can someone help me to get this working?
Upvotes: 1
Views: 2522
Reputation: 9527
From above code, pnl_console and pnl_tree passed to init seems to be structures, so they will exist only for time, when block with Init function will exist.
In your Init function, you are passing reference via &, but assigned it to structure. I would recomend, pass it with *
void PnlOptions::Init(HWND hwnd0, PnlConsole * console0, PnlTree * tree0)
PnlTree * pnl_tree;
PnlOptions pnl_options;
PnlConsole * pnl_console;
pnl_options.Init(hwnd0, &pnl_console, &pnl_tree);
Upvotes: 2
Reputation: 121971
console0
and tree0
are being passed by reference into Init()
but the assigments within the function result in copies of the arguments, due to the types of tree_
and console_
.
It is not possible to change the types of tree_
and console_
in this context because Init()
is not the constructor and reference types must be assigned immediately (in the constructor initializer list).
A solution would be to make the types pointers and take the addresses of the arguments. Note that there is a lifetime requirement in that the objects referred to by console0
and tree0
must exist for as long as the PnlOptions
requires them.
Upvotes: 4