|
-
March 9th, 2005, 05:21 PM
#1
What is this?????
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?
-
March 9th, 2005, 05:35 PM
#2
Re: What is this?????
Well, that's not class specific. That the same in C. It is used to access a member of a class, structure or union.
Code:
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.
Code:
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):
Code:
cPerson = addressBook::getEntry(i);
when getEntry is a static member of the addressBook's type. But the correct usage is:
Code:
cPerson = CAddressBook::getEntry(i);
assuming that CAddressBook is addressBook's type here.
-
March 9th, 2005, 06:20 PM
#3
Re: What is this?????
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
-
March 10th, 2005, 03:32 PM
#4
Re: What is this?????
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++.
-
March 10th, 2005, 04:45 PM
#5
Re: What is this?????
 Originally Posted by soopa1
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,
Code:
addressBook->getEntry();
is the functional equivalent of
Code:
(*addressBook).getEntry();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|