On Sat, 25 Mar 2006 23:18:30 +0100, salmonella <asd@asd.com> wrote:
[color=blue]
>Bob Hairgrove wrote:[color=green]
>> On Sat, 25 Mar 2006 16:48:04 +0100, salmonella <asd@asd.com> wrote:[color=darkred]
>>>void main(void)[/color]
>>
>> This should be:
>> int main()
>> // or:
>> int main(void)
>>
>> Returning void from main invokes undefined behavior in C++.
>>[/color]
>You are right. It is not a good practice (even in such a simple code)[/color]
"Bad practice", however, can also be code that does not invoke
undefined behavior, but (for whatever reason) is used from time to
time anyway. Undefined behavior is something which should always be
avoided.
[color=blue][color=green][color=darkred]
>>>{
>>> MyClass myObject;
>>> ptr = MyClass::fun;[/color]
>>
>> Should be:
>> ptr = &MyClass::fun;
>>[/color]
>IMHO both methods are OK. I think the name of the function is it's
>address at the same time.[/color]
This is true for static member functions as well as stand-alone
functions. For non-static member functions, section 5.3.1 paragraph 3
of the C++ standard seems to require the "&". But it doesn't surprise
me that MSVC lets you get away with it.
[color=blue][color=green][color=darkred]
>>> cout << myObject.*ptr << endl;[/color]
>>
>>
>> Should be:
>> cout << ptr << endl;
>>[/color]
>It doesn't show the address of the function - it allways output "1"
>(that is the problem)[/color]
That's because pointers to members are a kind of offset into the class
structure, not an absolute address. And there is only one instance of
a member function per class -- not per object. That is why it is
impossible to get a real address for such a function without resorting
to platform-specific tricks (e.g. using inline assembly to read the
value of the stack register).
--
Bob Hairgrove
NoSpamPlease@Home.com