Re: Frasncis Glassboro wrote.
"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:dY6dnRGj2q1NELXQnZ2dnUVZ8qqdnZ2d@giganews.com...
On 08/01/2011 16:07, Paul wrote:
"Garrett Hartshaw" <ghartshaw@gmail.com> wrote in message
news:ig8rhp$qtj$1@speranza.aioe.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/07/2011 11:35 PM, Paul wrote:
"Garrett Hartshaw" <ghartshaw@gmail.com> wrote in message
news:ig8jo6$age$1@speranza.aioe.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/07/2011 08:55 PM, Paul wrote:
"Garrett Hartshaw" <ghartshaw@gmail.com> wrote in message
news:ig855p$ec1$1@speranza.aioe.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/07/2011 02:06 PM, Paul wrote:
"James Kanze" <james.kanze@gmail.com> wrote in message
news:7148a492-1586-4421-bc6c-4afa38770f53@w29g2000vba.googlegroups.com...
On Jan 7, 2:07 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
[...]
I can understand the concept you express but
a) how do you get the address of a member function?
&ClassName::functionName
Concretely:
struct C { void f(); };
void (C::*pf)() = &C::f;
b) what happens if this member function is virtual?
It works correctly. That's why pointer to member functions are
often larger than any other pointer types (but there are other
ways of solving the problem).
What would your pointer point to ?
That's the compiler writers problem, not mine:-).
It certainly is a problem for the compiler, and perhaps the program
too.
Especially if you didn't initialised the empty pointer.
Let me put it another way, where would you get the address for the
virtual function?
You cannot do this with virtual functions and you are wrong
to suggest it works correctly.
It does work, and I've done it. More than once.
It simply can't be done as the concept of virtual functions only
lives
in the world of objects.
Please show some basic code. I guarantee you cannot.
#include <iostream>
class C1 {
public:
virtual void f() {
std::cout << "C1::f" << std::endl;
}
};
class C2 : public C1 {
public:
virtual void f() {
std::cout << "C2::f" << std::endl;
}
};
int main () {
void (C1::*p)() = &C1::f; //create a pointer to a member function
C1 a; //create a object of type C1
C1 * b; //create a object of type pointer to C1
The challenge put forward was to invoke a virtual function without
creating an object. Here you have created an object.
I applaud your abilities nonetheless, assuming the code is correct.
b = new C2(); //allocate a object of type C2
(a.*p)(); //call the member function pointed to
//by p, with &a as this
(b->*p)(); //call the member function pointed to
//by p (virtually), with b as this
return 0;
}
This code prints the following.
C1::f
C2::f
The very nature of virtual functions require objects and perhaps you
have demostrated this.
This was what i was referring when I initially stated 'it won't work
with virtual functions':
"You can take the address of a member function (and assign it to a
function pointer of the proper type), and it will *not* be tied to
any
specific object."
The (virtual) member function was *not* tied to a specific object,
as it
(the same function) was used by two different objects. If it was *a
part
of* the object, you would not be able to have a pointer to it.
With:
(a.*p)(); //call the member function pointed to by p, with &a as this
(b->*p)(); //call the member function pointed to by p (virtually),
with b as this
There are two functions here.
As I understand it you seem to think there is only one function . ????
Ok, there are two functions, C1::f and C2::f, and the pointer really
points to an offset into a vtable (the vtable being part of the object),
which contains the address of the function actually called (not part of
the object). However, if we had another object of (dynamic) type C2
(e.g. C1 * c = new C2(); ), and call the function ( (c->*p)(); ), then
we have 3 objects ( a, b, c ), and only 2 functions ( C1::f, and C2::f
). This would not be possible if the function itself was part of the
object.
No, you cannot use a virtual function without an object.
A virtual function is basically a pointer that exists within an object,
a virtual function never actually exists as a function hence *virtual*
function.
It is impossible to invoke a virtual function without the existence of
an object. The object contains the pointer (that is the virtual
function).
You are wrong yet again; you obviously do not know C++. The address of a
virtual function is contained within a vtable; it doesn't exist within an
object; all that exists within an object is a pointer to the vtable if the
object is of class type and the class contains at least one virtual
function. Objects do not contain member functions (virtual or not);
classes contain member functions (virtual or not).
I don't know what type of implementation you are trying to define here, but
on my implementaion a function is a process that begins when an instruction
is loaded into a cpu register,
A function is normally invoked on the stack and uses stack pointers and/or
offsets to reference any objects within its scope.
A virtual function is a psuedonym for the function that is represented by
the pointer inside the object.
A virtual function does not have an address, a function is not a region of
memory as you seem to think.
A class is a precompile time entity, a function is a runtime entity.
To suggest that a function belongs to a class is complete nonsense, becuase
the class does not even exist when the function is invoked.
You seem to be talking about a function definition , not a function.
This is C++, not Java!.
Hope this makes sense to you.
Some peope are obviously confused between a precompile time
entity(class) and an object.
A function is not contained within a class anymore than it is contained
within an object, with:
wanda.blowBubbles();
This statement contains an object and a member function, there is no
class here. This member function does not exist until it's invoked on an
Wrong yet again; member functions exist within the code segment before any
objects are created at runtime.
Utter bullshit.
Functions exist in a cpu not in a region of memory.
An object is a region of memory, a function is not a region of memory.
And remeber an object is definately not a function :)
Please don't state I am wrong *again* as if I have previously been proven
wrong about something .
It is apparent you have strange ideas about functions living in memory and
it is indeed more likely to be you that is wrong.
object. The function is exclusively tied to the object, (*not the
class*), the class is stored on some file on an old dusty computer on
the other side of the world.
After the code is compiled a class no longer exists , a class is a
precompile time entity in C++, a class in C++ is not the same as Java
class.
I don't know what is wrong these people who cannot understand this ,
they are obviously very very ignorant and confused, please don't be one
of them
I suggest you read a C++ book or two and stop trolling your ignorance in
this newsgroup wasting people's time.
/Leigh
The level of intelligence displayed by your insult is your problem.