Class Variables

This is a discussion on Class Variables within the C++ Programming forums, part of the General Programming Boards category; Oke, I got a function in a class, this function takes the arguements: testfunc(int a,int b); Now these variables (int ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Class Variables

    Oke,
    I got a function in a class, this function takes the arguements:
    testfunc(int a,int b);
    Now these variables (int a,int b) exists in the class as well, how
    can i adress the variables in the class?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    ClassName::MemberName from within the member function

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    If the function and members are of the same class then you don't have to do anything special, just use the members in the function like you would anywhere else, unless the members and function arguments have the same names. If that's the case then you could have problems, so be explicit.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class test {
      int a, b;
    public:
      void function(int a, int b)
      {
        this->a = a;
        this->b = b;
      }
    
      void print()
      {
        cout<< a <<endl<< b <<endl;
      }
    };
    
    int main()
    {
      test t;
    
      t.function(1, 2);
      t.print();
    }
    *Cela*

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    yeah, use the 'this->' variable
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    and if you dont use this it will search for the variable
    in the class first and then in the passed arguments?

  6. #6
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    No, I don't think so - it's a matter of SCOPE it will probably just use the local variables.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    hmm, so 'this->' is a way to assure that it uses the local function
    variables, and how can i assure it uses the class variables?

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Travis Dane
    hmm, so 'this->' is a way to assure that it uses the local function
    variables, and how can i assure it uses the class variables?
    No, you're confused.

    the keyword 'this' represents a pointer to the object you are currently in, so

    this->membername

    says "the member of the current object called membername"

    In other words, that refers to the MEMBER not the variable local to the function. To use the one local to the function you just use the variable name -- the most local variable with the same name is the one that's used.

    The other way you can access the object's member (IE the one not passed to the function), is how I mentioned with the scope resolution operator

    ClassName::MemberName

    which will have the same end result as

    this->MemberName

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I'm still a little confused,
    How would i accomplish something like this

    Code:
    class test
    {
    public:
    void outputsomething(int a);
    private:
    int a;
    };
    
    void outputsomething(int a)
    {
    cout << a; // output the passed var
    cout << a; // output the class's var
    }

  10. #10
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Code:
    class test
    {
    public:
    void outputsomething(int a);
    private:
    int a;
    };
    
    void test::outputsomething(int a)
    {
    cout << a; // output the passed var
    cout << this->a; // output the class's var
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    k,got i now, thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Read-only class members
    By kidburla in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2006, 01:52 PM
  4. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  5. variables in instances of a class
    By alloc(MEM_NEWBIE) in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2002, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21