And to clarify the function exists even though the object is not allocated, because the function is one for every class used by all objects. Like:
in your case this will point to nowhere but since it is not used the program might not crush. If you were to use "num" inside the function (try it) then you have more chances to crush it.Code:void Virtual(Base* this) { cout << "...."; }
is it possible to pass "this"?
it is implicitly passed...
i will try your idea now
is it that not if the functions are of the objects of the same class?the function is one for every class used by all objects
so they share one function..is that what you mean?
EDIT:
i used num..
just print what it held..it didn't crash
same with assigning...i guess i need more chances..
Last edited by Eman; 01-30-2011 at 04:49 PM.
You ended that sentence with a preposition...Bastard!
Um, it's possible, if you are calling a function that requires a pointer-to-object and want to pass it a pointer to the current object. You don't use it for member functions.
I think his point was that the code doesn't live inside the object; the code lives as a function with the rest of the code; so it's not as though you were trying to run code at an invalid memory address, just looking at invalid memory. (And since we are using a once-valid pointer, it is likely---though not certain---that looking at (or fetching a value from) this memory is not going to cause a segfault. It will give us bogus data, but it probably won't crash.) If we had actually considered this a derived object and tried to look up the vtable (or whatever is used for virtual function dispatch), then bad things might happen if the memory had ended up being overwritten.is it that not if the functions are of the objects of the same class?
so they share one function..is that what you mean?
EDIT:
i used num..
just print what it held..it didn't crash
same with assigning...i guess i need more chances..
oh...I have no idea what you mean by "the code doesn't.."I think his point was that the code doesn't live inside the object; the code lives as a function with the rest of the code; so it's not as though you were trying to run code at an invalid memory address, just looking at invalid memory.
once-valid pointer?
I thought we agreed that it doesn't point to anything..only at run time?
unless it points to a valid address, but it doesn't create a static bind to a function call.
so b[0] did point to something.
You ended that sentence with a preposition...Bastard!
Your objects live somewhere in memory. But nowhere in that chunk of memory is the instructions for the function. Those instructions live elsewhere (with the rest of the program, not with all the variables).
It pointed at something for approximately 0.0002 seconds (between the temporary object being constructed and then being destructed at the end of the statement). But the pointer still retains it's value, and still holds the address where that temporary object was constructed.