CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    45

    Question 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?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Sep 2004
    Posts
    45

    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

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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++.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jan 2004
    Posts
    9

    Re: What is this?????

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured