1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ Classes

Discussion in 'Software Development' started by Regicide, Oct 6, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. Regicide

    Regicide Thread Starter

    Joined:
    Aug 8, 2003
    Messages:
    307
    I'm working with one class in a program. In that class I have one function. For one of the arguments I want it to store the value to a protected member of the class:
    Code:
    class Test
    {
    public:
          int Function(char* X)
          {
                cout <<  X << endl;
                return 1;
           }
    protected:
          char X[50];
    };
    
    When I do this, the protected X doesn't equal the X argumented in the function. Does anyone know how to do this without making the X in the function have a different name? Thanks.
     
  2. moebius

    moebius

    Joined:
    Oct 7, 2003
    Messages:
    1,741
    i think u would find everything about java classes in a java class. try aptech, or informatics. no, really! ;)
     
  3. Guy

    Guy

    Joined:
    Feb 19, 1999
    Messages:
    260
    What are you talking about? He wants c++ help not java propeganda

    On with the problem: to access a member variable withing a class, use the this operator:
    Code:
    class Test
    {
    public:
          int Function(char* X)
          {
                cout <<  this->X << endl; // or this.X I cant remember if this is a pointer or a value
                return 1;
           }
    protected:
          char X[50];
    };
     
  4. moebius

    moebius

    Joined:
    Oct 7, 2003
    Messages:
    1,741
    i mean dimp c++ go2 java
     
  5. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    Mobius, if you like Java, that's fine. Quit trying to get everybody to use Java, they use, what they use and are asking legitimate questions.

    When you get to the real world, you'll realize, often you don't have a choice what language you use. It's allready been decided, you have to make it work.

    So instead of getting people to change to JAVA, try helping them in the language they are asking for help in.

    ps I think Guy got it, in OOP you have to be very careful about variable scope and what you are really accessing.
     
  6. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    The X in the function declaration and the X in the class definition are different variables occupying different areas of memory. If you want to assign one to the other then do so; otherwise the protected member of your class is not going to be updated.

    Also, I strongly encourage you to use different variable names in your class definition and function argument list. I hop that the code that you posted was an example and you don't in reality intend to use the same variable name twice in the same scope. This process only serves to confuse others trying to read your code.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/169979

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice