Click to See Complete Forum and Search --> : What is this?????
soopa1
March 9th, 2005, 04:21 PM
I was looking at some code a guy at school was writing and he had something I never seen before. Which Is this: ( ->)
For example:
cPerson = addressBook->getEntry(i);
What is that? Could the same code be written like this:
cPerson = addressBook.getEntry(i);
or this
cPerson = addressBook::getEntry(i);
I'm kinda new to classes.
Can someone give me a quick run down of what that is and what it is used for?
cilu
March 9th, 2005, 04:35 PM
Well, that's not class specific. That the same in C. It is used to access a member of a class, structure or union.
struct foo
{
int i;
void something();
};
foo f; // f is an object of type foo
f.i = 0;
foo* f1 = new foo; //f1 is a pointer
f1->something(); // can't use f1.something()
:: is the scope resolution operator. You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
If you have nested local scopes, the scope resolution operator does not provide access to identifiers in the next outermost scope. It provides access to only the global identifiers.
int amount = 123; // A global variable
int main() {
int amount = 456; // A local variable
cout << ::amount << endl // Print the global variable
<< amount << endl; // Print the local variable
}
You can use it like this (or almost):
cPerson = addressBook::getEntry(i);
when getEntry is a static member of the addressBook's type. But the correct usage is:
cPerson = CAddressBook::getEntry(i);
assuming that CAddressBook is addressBook's type here.
soopa1
March 9th, 2005, 05:20 PM
Ohhhh..I see. It's actually related to a pointer. I was thinking it was a replacement or another way of writing the scope resolution. Thanks man
Marc G
March 10th, 2005, 02:32 PM
Maybe it would be a good idea to read some kind of introduction to C/C++, because -> is pretty basic in both C and C++.
kwatz
March 10th, 2005, 03:45 PM
Ohhhh..I see. It's actually related to a pointer. I was thinking it was a replacement or another way of writing the scope resolution. Thanks man
To be explicit,
addressBook->getEntry(); is the functional equivalent of
(*addressBook).getEntry();
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.