Object passed by reference not working as expected - c++

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?

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.

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);

Related

C++ class filler syntax error

My class is as follows:
class stats {
public: int strength,
perception,endurance,charisma,inteligence,agility,luck,health,stamina,mana,karma;
};
As far as I know, there shouldn't be anything wrong with it, unless I need to set up a constructor and destructor.
I create my object using the following line:
stats* mainstat=new stats;
And I have the following function to "fill" objects of the said class:
void statfiller(stats* object, int table[]){
object->strength=table[0]; object->perception=table[1];
object->endurance=table[2]; object->charisma=table[3];
object->inteligence=table[4]; object->agility=table[5];
object->luck=table[6]; object->health=table[7];
object->stamina=table[8]; object->mana=table[9];
object->karma=table[10];
}
So, until then, no problem. At least, until the following:
I create a table with the data to fill, then feed it to my fill function.
int tablet[10]; tablet[0]=5; tablet[1]=5; tablet[2]=5; tablet[3]=5;
tablet[4]=5; tablet[5]=5; tablet[6]=5; tablet[7]=50; tablet[8]=50;
tablet[9]=50; tablet[10]=0;
statfiller(mainstat*,tablet);
When I do this, a compiling error comes up, stating the syntax of my function is incorrect.
Why is it so? Do I need to use pointer(*) or address(&)? Is there something I'm missing?
Odds are, the solution is very simple, but at the moment of typing this, I just don't see what's wrong with it ^^;
Solution to this problem was the following:
The function's syntax is "void statfiller(stats* object, int table[]) ", where the stats* object serves as reference, pointer to an object of stats class.
In the function's call "statfiller(mainstat*,tablet);", the mistake I made was calling a pointer of a stat object (in this case mainstat) instead of just putting in the object.

Was it possible to get a pointer to member from an instance of an object?

I was porting some legacy code to VS 2015 when this compiler error halted the build:
error C3867: 'OptDlg::GetFullModel': non-standard syntax; use '&' to create a pointer to member
Going to the corresponding file and line, I saw this:
Manager mgr = GetDocument()->GetManager();
OptDlg dlg;
...
mgr->SetFullModel(dlg.GetFullModel);
if ( dlg.GetFullModel )
mgr->SetSymm(...
GetFullModeland SetFullModel are the getter/setter pair for a member variable in two different classes:
class Manager {
...
bool GetFullModel() { return m_bFullModel; }
void SetFullModel(bool bFlag) { m_bFullModel = bFlag; }
....
};
class OptDlg {
...
void GetFullModel() { return m_bFullModel; }
void SetFullModel(bool bValue) { m_bFullModel = bValue; if ( bValue ) m_bInside = 0;}
Yep, something's wrong. Was dlg.GetFullModel supposed to be a pointer to a member function? I thought those use the class name, not an instance. Not to mention what that would mean for execution semantics...
C++ is still relatively new to me, so I tried Google. It had a lot on function pointers, but they all looked different from what I had:
&OptDlg::GetFullModel // Standard-compliant
vs
OptDlg::GetFullModel // The "normal" way to mess up getting a pointer to member, it seems
vs
dlg.GetFullModel // ?
Is dlg.GetFullModel just another way of getting a pointer to member function? If not, what is the "standard C++ version", if there is one? Is this just another one of those VS 6 "extensions"?
&OptDlg::GetFullModel // Standard-compliant
If your parameter types were supposed to be taking member functions, that's what you'd use. But they take booleans. It looks like you're just missing parentheses on your function calls, and it should be:
mgr->SetFullModel(dlg.GetFullModel());
if (dlg.GetFullModel())
mgr->SetSymm(...
Probably someone was ignoring warnings (or didn't have them on) and hence a pointer value (being produced through whatever shady means) was always being interpreted as non-NULL, hence boolean true.
Is this just another one of those VS 6 "extensions"?
It would appear to be the case, although this comment is the only documented evidence I can find it was an intentional/advertised "feature". Don't see any formal announcement of it being added or taken out.
It strongly looks to me like someone mis-typed dlg.GetFullModel() (which would call the function), not that they were trying to get a member function pointer.
Presumably the legacy compiler let it slide, taking the address of the function without using & and converting the non-null function pointer to bool (with value true) to pass into the set function.

How to pass argument to function - dealing with reference

I have some problems with C++.
I build application using GAlib library (it's a C++ Library of Genetic Algorithm Components - http://lancet.mit.edu/ga/).
In one of examples (full code of example: http://lancet.mit.edu/galib-2.4/examples/ex6.C) author create function that initializes tree:
void TreeInitializer(GAGenome & c)
{
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
// destroy any pre-existing tree
child.root();
child.destroy();
// Create a new tree with depth of 'depth' and each eldest node containing
// 'n' children (the other siblings have none).
int depth=2, n=2, count=0;
child.insert(count++,GATreeBASE::ROOT);
for(int i=0; i<depth; i++){
child.eldest();
child.insert(count++);
for(int j=0; j<n; j++)
child.insert(count++,GATreeBASE::AFTER);
}
}
He invoke function in that way:
genome.initializer(TreeInitializer);
and He don't pass an argument. When I trying pass argument to that function by changing declaration for example:
void TreeInitializer(GAGenome &, int deph);
Compiler shows me errors. I don't know how to invoke this function properly. I know it's related to reference but passing (or not passing) argument in that way is new to me.
How to pass more arguments to that function?
The line genome.initializer(TreeInitializer); does not call TreeInitializer - instead, it passes the pointer to that function to genome.initializer, so it can call it with whatever arguments it needs/multiple times. If you want TreeInitializer to accept more arguments, you need to modify the initializer to accept a different type of function. It is probably defined as
void initializer(void (*f)(GAGenome&))
{
// ...
f(genome);
// ...
}
You need to change the argument type to void (*f)(GAGenome&, int) and change the lines that call f.
On the other hand, if you can't modify the initializer function but want to be able to specify the depth, then the best you can do is make a global variable that you set to whatever depth you need and have TreeInitializer use that variable. It isn't a clean solution if you think global variables are evil, but it's your only choice if the initializer doesn't let you supply any "context".
What you what to do in
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
is a casting from a GAGenome to GATreeGenome . If you haven't a casting operator for this case, it won't work...

How to register member function with lua-function parameter using luabind?

I need to register a member function using luabind which is supposed to take a lua-function as parameter. For any normal function I would usually just do this:
int SomeLuaFunction(lua_State *l)
{
luaL_checkfunction(l,1);
int fc = luaL_ref(l,LUA_REGISTRYINDEX);
[...]
}
Luabind however uses the parameter list, so I'm unsure how to tell it I'm expecting a function:
void Lua_ALSound_CallOnStateChanged(lua_State *l,boost::shared_ptr<ALSound> pAl,<function-parameter?>)
{
[...]
}
lua_bind(luabind::class_<ALSound COMMA boost::shared_ptr<ALSound>>("ALSound")
.def("CallOnStateChanged",&Lua_ALSound_CallOnStateChanged)
);
(Only the relevant part of the code is shown here, lua_bind is using luabind::module)
lua-example of what I'm trying to accomplish:
local al = ALSound() -- I'm aware this wouldn't work since I haven't defined a constructor
al:CallOnStateChanged(function()
[...]
end)
Perhaps there is a way to add additional functions to an already registered class without luabind? Any suggestions would be appreciated.
If you want to be able to have a function that takes Lua objects as parameters, you should have the function take a luabind::object as a parameter. Then you can check to see if it's a function and call it if it is.

Receiving assert failure on Reference Call

(Disclaimer: I have removed the Qt tag in case the problem is in my syntax / understanding of the references involved here)
I have a foreach loop with an object Member. When I enumerate through the list and try to access a member field, the debugger stops and I get a message:
Stopped: 'signal-received' -
The assert failure is:
inline QString::QString(const QString &other) : d(other.d)
{ Q_ASSERT(&other != this); d->ref.ref(); }
I have checked if the member is NULL, and it isn't. I have tried re-working the code, but I keep failing on this simple call.
Some thing's I missed out. MemberList is a singleton (definitely initialized and returns a valid pointer) that is created as the application launches and populates the MemberList with Members from a file. When this is created, there are definitely values, as I print them to qDebug(). This page is literally the next page. I am unsure as to how the List items can be destroyed.
The code is as follows:
int i = 0;
QList<Member*> members = ml->getMembers();
foreach (Member* mem, members)
{
QString memID = mem->getMemberID(); // Crash happens here
QListWidgetItem *lstItem = new QListWidgetItem(memID, lsvMembers);
lsvMembers->insertItem(i, lstItem);
i++;
}
The Member classes get is as follows:
QString getMemberID() const;
and the actual function is:
QString Member::getMemberID() const
{
return MemberID;
}
The ml variable is received as follows:
QList<Member*> MemberList::getMembers()
{
return MemberList::getInstance()->memberList;
}
Where memberList is a private variable.
Final answer:
I decided to rework the singleton completely and found that I was not instantiating a new Member, rather reusing the previous object over and over. This caused the double reference. S'pose thats pointers for you. Special thanks to Troubadour for the effort!
If mem is not null it could still be the case that the pointer is dangling i.e. the Member it was pointing to has been deleted.
If Member inherits from QObject then you could temporarily change your QList<Member*> that is stored in ml (assuming that's what's stored in ml) into a QList< QPointer<Member> >. If you then get a null QPointer in the list after calling getMembers or at any point during the loop then the object must have been destroyed at some point.
Edit
As regards the singleton, are you sure it's initiliased properly? In other words does MemberList::getInstance() return a valid pointer or just a random uninitialised one?
Edit2
Since we've exhausted most possibilities I guess it must be in the singleton somewhere. All I can suggest is to keep querying the first item in the list to find out exactly where it goes bad.