Post by g***@mvwfw1.marshallwindows.comPost by Chris Uzdavinis (TeamB)Because foo::SetValue is a static member function, it has no "this"
parameter. You cannot call non-static member functions without a
"this" parameter, unless you're explicitly calling it on an object.
Ok, what exactly is a "this" parameter?
When you call a member function on an object, there is an invisible
parameter that the compiler injects into the call so that the function
knows which object it's being called on.
If you want to conceptually look at the generated C++ code as if it
were C code, you could think of it like this:
class X
{
public:
X() : data_(0) { }
void foo(int x) { data_ += x; }
private:
int data_;
};
int main()
{
X x;
x.foo(10); // x::data_ == 10
x.foo(20); // x::data_ == 30
}
Old C++ compilers were implemented by outputting C code. Perhaps the
above code would look something like this:
struct X
{
int data_;
};
void X__initialize(X * this) { this->data_ = 0; }
void X__foo(X * this, int x) { this->data_ += x; }
int main()
{
X x;
X__initialize(&x);
X__foo(&x, 10); // x::data_ == 10
X__foo(&x, 20); // x::data_ == 30
}
The point is, the funciton X::foo() has a this parameter that the
programmer didn't write, but that was injected by the compiler. EVERY
member non-static function has such a parameter, and it holds the
address of the object it was called on. (That way a single
implementation of the function can be used with every instance of the
class.)
Static member functions do not have a this pointer passed into them
because they are not called on objects at all. They are just called,
very much like a free-standing function that is using the class as a
namespace, and that has special access to the private bases, members and
data.
Post by g***@mvwfw1.marshallwindows.comChris, I'm quite sure that answer is full of meaning for someone who
actually _knows_ C++. To me however, it's the equivalent of "Well ya gotta
morfle the megablum before the ixylplik transmorgraficates itself." :)
LOL. Sorry.
Post by g***@mvwfw1.marshallwindows.comfoo *bar;
(or maybe foo new *bar; or foo new bar;)
You're right, that creates an instance of an object. When you call a
member function the "this" pointer holds the address of the object you
called it on. In this case, if you created an object with a "new"
expression and then called a member on it, the "this" pointer would be
the address returned by new.
Post by g***@mvwfw1.marshallwindows.comI guess I don't understand why I can't call a member function within a member function.
Here is (I hope) a clear example of why the static member function
cannot call a non-static member.
class Y
{
public:
Y() : data_(0) {}
void foo() { data_ += 10; }
static void g() { foo(); } // whose data_ gets updated?
private:
int data_;
};
int main()
{
Y::g(); // note: no object of type Y is involved.
}
Post by g***@mvwfw1.marshallwindows.comWhen I try to directly access the "bal" variable within SetValue, I
get an unresolved external error and that REALLY blows my mind. How
can something defined within a class and referenced within the SAME
class be called "external"? It makes about as much sense as trying
to make gym shoes out of yogurt.
A static member variable is kind of like a global variable. One
exists period, with the main difference being it's contained in the
scope of the class rather than in the global namespace. Compilers
tend to treat global variables and static variables in very similar
ways. They are (or can be) created at approximately the same time,
destroyed at the same time, and have the same kind of issues
associated. Your access to them is one of the main differences, since
you have to go "through" a class to get to static members, and if
they're private (and you're not a friend, etc.) you're not allowed to
touch them.
Post by g***@mvwfw1.marshallwindows.comPost by Chris Uzdavinis (TeamB)Why is SetValue a static member? I'm suspecting you don't want it to
be. If you really need it to be, then you'll have to also figure out
a way to have a foo instance on which you can call SetBal().
That was how the example I'm working with (Lua) had it set up. If I remove
[C++ Error] unitMain.cpp(23): E2034 Cannot convert 'int (LuaAccount::*)(lua_State *)' to 'int (*)(lua_State *)'
Ah, I think I see. Lua is using it as a callback, and since lua is
written in C, it expects a C++ free-standing function (or static
member function) to receive the callback.
You probably will have to keep that a static member. My guess is that
the instance you're interested in seeing is identified in the
Lua_state parameter you're given. Perhaps you can keep a hash table
(or map) of object indexed by that identifier. The lua callback
receives the call, looks up the object, and then calls a member on
that object.
Post by g***@mvwfw1.marshallwindows.comThe 'foo' class I posted originally is a much trimmed down version of what
I'm actually working with. I just wanted to highlight the problem I was
having and not post a lot of code that wasn't relative to that problem.
Thanks. Appreciated!
--
Chris (TeamB);