Thread: Related to Pointers

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Related to Pointers

    I have three questions, both related to pointers. (or so i think)...

    First is ->. I see this writen alot. For example.

    Code:
    m_Console->SetPosition(Position);
    If somebody could explain to me what is going on here, for some reason, nowhere is giving a solid definition, or I'm just not looking hard enough(which is possibly the case).

    Second is 'this' in an expression such as:

    Code:
    return * this;
    I understand I didn't give much code, but i do believe it is sufficient to answer the question of, what exactually could be going on here? I understand that 'this' is directly related to classes in some way.

    Finally, what exactually is the keyword 'operator'. I'm not quite understanding what it does. Example:

    Code:
    Vector operator + (Vector & OtherVector);
    This code being inside a class. Could somebody please explain to me what exactually could be going on here.

    Thank you very much for your help.
    Last edited by Extol; 02-27-2003 at 05:16 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    your first portion of code, -> is a reference of a pointer...in that case, m_Console would be the pointer...if you point to a structure, class, union etc, you can access the different functions and variables of them by using ->.....so in that case, that is a pointer to a class that has a function called SetPosition, and it is calling it, and it takes 1 parameter

    the 2nd piece of code simple returns a pointer variable called this (i think ?)

    3rd line of code, your guess is as good as mine

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >the 2nd piece of code simple returns a pointer variable called this (i think ?)

    inncorrect,
    'this' is a pointer used inside a class to make reference to that particular instance of the class. for example,

    Code:
    class thing {
    // stuff goes here
      int var
    
      int func(thing param) {
        return this->var + param.var;
      }
    };
    // this->var refers to the var variable of the object making the function call
    when you 'return *this' you are returning that class object. 'this' is a pointer so 'return * this' will dereference the pointer and return the actual object.

    >Finally, what exactually is the keyword 'operator'. I'm not quite understanding what it does. Example:

    'operator' is a keyword used when overiding operators. in your example, the class is defining what to do when you try to add 2 vectors using '+'

    ie)
    vector + anotherVector

    i think i got that all right, someone correct me if im wrong. (ive been programming java for the last 3 hours, such things can corrupt my delicate c++ lovin mind... lol, j/k)

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    you can use *this inside a class, like they said, to access the class inside.

    Very useful for overloads

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    learn somethin new every day thx for correcting me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 12:54 PM
  4. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 01:24 AM