Thread: "this" keyword question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    "this" keyword question

    Hello all and thanks in advance for reading!
    I have been playing around with classes and now want to dive in the 'this' keyword. I have been looking over the internet and I cannot get a good description of exactly what it does. So here are a few of my questions...

    If I have a the following (syntax maybe wrong)
    Code:
    class x
    {
    public :
       int a;
       int b;
       int c;
       int d;
    
    int setIntegers(int a, int b, int c, int d)
    };
    
    int x::setIntegers(int a, int b, int c, int d)
    {
       this->a = a; 
       this->b = b;
       this->c = c;
       this->d = d;
    
      return(this);
    }
    What exactly gets returned by this. Also lets say instead of return(this) I have another class that has function addAll() and I call the following:
    Code:
    //Instead of return(this) within setIntegers function
       y->addAll(this);
    
    y::addAll()
    {
    z = a+b+c+d;
    //return(z);
    //NOTE: This will not return anything, it will only store z as a private int.
    }
    First is that syntax right with the pointer? And secondly what would the parameters need to be for the addAll function.
    Also am I calling the function correctly? By calling returning the this does it properly send out all of the varaibles...

    Furthermore ( I know long post sorry ><; )

    lets say within class x I have the following code instead...

    Code:
    x::setIntegers(int a, int b, int c, int d)
    {
       this->a = a; 
       this->b = b;
       this->c = c;
       this->d = d;
    }
    x::processVariables()
    {
      y->addAll(this);
    //This is linking into the code snippet above.
    }
    Would it do the exact same thing yet now the values are set and can be sent at any request?

    I hope all of my questions are clear and if need be I can elaborate. Thank you all again!

    **EIDT**

    I should also mention that this is not code I am working on but for example purposes. I believe understanding this example for me should be enough to help me accomplish my primary goal.
    Last edited by Toonzaka; 08-12-2010 at 09:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... what is the return type of those functions? Perhaps you should decide that instead of asking what is the type of this.

    But if you do want to know, think: this is a pointer to the current object, but you cannot modify the this pointer itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    Return Type

    As I mentioned the syntax might not be exaclty correct... The return type is an int. Also I understand definition and as mentioned I have researched this enough to have seen the definition plenty of times. My question is more application oriented. With that in mind now, is there anything wrong in terms of the application of 'this'.

    Can I have a function that sets the variables passed in returning nothing. And then have another function which uses the this keyword as illistrated from my code example to add the variables and store that result? I will edit my first post..
    Last edited by Toonzaka; 08-12-2010 at 09:19 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, so why is the return type int? Why not just make it void?

    Quote Originally Posted by Toonzaka
    Can I have a function that sets the variables passed in returning nothing.
    Yes, but making the return type to be void.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    16
    For my real application I do not have to worry about return types they are already set up within the function that will be doing the processing.

    But its good to know that I can do that! Thanks for helping, all I really needed was someone to say "yes your right on your assumption" or "no your a damn fool and I pitty you"

    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about "extern"-clarification
    By Programmer_P in forum C++ Programming
    Replies: 23
    Last Post: 08-30-2009, 11:37 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. I have a Question about memory usage for C.
    By bobthefish3 in forum C Programming
    Replies: 34
    Last Post: 12-24-2001, 04:37 PM