Re: Is it legal code?
"gwowen" <gwowen@gmail.com> wrote in message
news:7963ef8c-f3ef-4351-b4ad-847b284fb19d@u6g2000vbh.googlegroups.com...
On Feb 20, 1:09 am, "Paul" <pchris...@yahoo.co.uk> wrote:
It follows that if an object(or derived object) does not exist then it is
undefined behaviour to call its respective nonstatic member function?
Therefore it must be true that a member function does not exist without
an
object.
No. That is simply not a syllogism. "X is undefined behaviour" is not
equivalent to "The constituent parts of X do not exist."
It is undefined behaviour to call strlen(const char *) on a pointer
that does not point to a valid NULL terminated string. That does not
mean that the strlen() function does not exist until a valid string
exists, and that it ceases to exist when all such strings go out of
scope.
According to the standard you cannot have any strlen without an object of ,
or derived from , string.
That is assuming strlen is defined as a non static member function of
string, in class string.
Setting aside the peculiar ontological implications of such a
deduction, you can (for example) take the address of "strlen()".
Consider with a more simple example:
class C_type{
public:
void foo(){};
};
int main(){
C_type* obj1 = new C_type();
obj1.foo(); /*foo existshere*/
delete obj1;
/*foo no longer exists*/
}
If we do not have an instance of C_type then we do not have any foo()'s.
The function definition will still exist someplace so if another C_type is
created another foo can be invoked.
I aggree it is not strictly true to say the function no longer exists
because it is possible to directly call the function with a function
pointer. But since this is not valid according to the C+ standard, it can be
said , as far as the standard is concerned, that the function does not exist
unless an object of the class type exists.