OK great, here's where I'm up to. I read a bit further on and it seems the author is using this example to show how not to perform assignment. I'll elaborate. The class 'String' has the following operator overload defined within it:
String& String::operator = (const char* str){ if(!str) str = ""; char* tmp = strcpy(new char[strlen(str)+1], str); delete [] s_; s_ = tmp; return *this;}//added this bit for clarityprivate: char* s_; // s_ is a private data member
So from what you guys are saying the '::' guarantees that the default namespace will be used, so no chance of a user defined implementation of 'new' can occur. Seems that according to the author:
(::operator new(BUFSIZE));
will just produce a pointer to an uninitiated piece of memory with BUFSIZ bytes. This is new to me as I've never seen the keyword 'new' used outside of defining a new array dynamically for example:
int* myPointer = new int[5];
I guess this code line would produce an 'initialised' pointer right?
ANyway moving on, this is the line where the author states there'll be trouble:
String *names = static_cast<String*>(::operator new(BUFSIZ));names[0] = "Sakamoto"; // THIS LINE HERE
Ok.Now I'm not sure why he's used 'names[0]' when he could have just used 'names'. I always thought a pointer's identifier pointed to the first element in an array anyway. I don't even know where he's used the '[]' operator here for that matter I thought 'names' was just a pointer to one class instance of class String. Hmmm. Could use some help with that one to be honest if possible.
Ok so thus far I'm kind of with him. If you use the second line of code shown above you run the String class's assignment operator overload and end up running an array delete (delete []) on an uninitialised pointer that is just pointing to whatever - so I guess this runs the risk of undefined behaviour and is therefore a no no.
Ok but.... what I don't get is how you can run the String class's assignment operator overload when you don't actually have an
instance of that class an existence yet. So far as I can see all there is, is a pointer to type String which is being made equal to a rough pointer cast to a String pointer. I don't see how anywhere this makes the String class's inner workings available, so:
Can you access a class's member functions through a pointer to a class even though there is no actual class object in existence?
Sorry this dragged on, but I did quite a bit of thinking about it and you guys did take the time to reply, so I though I'd honour that by showing some enthusiasm :oD