A Class question

This is a discussion on A Class question within the C++ Programming forums, part of the General Programming Boards category; Hi, When you create a class instance its variables are cleared, but what if you have made a class instance ...

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

    A Class question

    Hi,

    When you create a class instance its variables are cleared,
    but what if you have made a class instance in another function
    and gave a variable a value in that function, and you wish
    to retrieve its value from another fucntion, how can this be done?
    As you'd just get empty variables if you created an instance of
    that class
    Last edited by Travis Dane; 01-02-2003 at 07:24 PM.

  2. #2
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    But those variables would be local and not global. So only the function that they were created in should be able to use them. I am not sure if this is correct but it seems logical.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You can create a global class?
    A class that every function can acces?

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I can't answer that question, I don't know anything about classes. But I know you create a global variable by creating it out of a function. But global variables can create run-time errors.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Travis Dane
    You can create a global class?
    A class that every function can acces?
    No, you create a global object.

    The answer to this isn't very hard...
    Ask this; what if you want to access the value of a single integer in two different functions, what do you do? One of many things.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Munkey01
    I don't know anything about classes
    You're not worthy of your avatar!

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I know some about them in Ruby but not enough to call me a user of classes. I stay away from them when coding in Ruby. But I have not come accross them in C++ yet.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Eibro
    No, you create a global object.

    The answer to this isn't very hard...
    Ask this; what if you want to access the value of a single integer in two different functions, what do you do? One of many things.
    Sorry,meant to say object.

    As to the ask, you create a global int or pass it from the point
    where they are called from, but thats not possible with classes

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Travis Dane
    Sorry,meant to say object.

    As to the ask, you create a global int or pass it from the point
    where they are called from, but thats not possible with classes
    Why not? Have you tried it?

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Well yes, like so

    Code:
    class abc
    {
    abc();
    int a;
    }
    
    abc::abc()
    {
    }
    
    abc def; // like NOT allowed, it doesnt recognize abc
    
    void main()
    {
    cout << def.a;
    }

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    class abc
    {
    public:
    abc();
    private:
    int a;
    };
    
    abc::abc()
    {
    }
    
    abc def;
    I spot three immediate errors
    No semi-colon after class definition
    Constructor is private
    And that's not a valid main() function

    Also, I doubt a global variable is the best answer to your problem.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    All 3 errors you spotted was because i typed it out of my hand,
    I need to acces a variable in a object thats given a value from
    another function, got a solution?

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Pass the object by reference to both functions.
    Also, it's perfectly valid to do what you did in your previous post; you must have made an error somewhere else.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Really? my compiler doesn't think so!
    Could you post your code for me to try then?

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    class Buffer
    {
    public:
       Buffer(size_t size) : m_data(new char[size]), m_size(size) 
       {
             memset(reinterpret_cast<void*>(m_data), 0, size);
       };
    
       size_t length() const { return m_size-1; }
       char * data() { return m_data; }
    
       ~Buffer() { delete[] m_data; }
    
    protected:
    
       Buffer(const Buffer &bf) {};
       Buffer &operator=(const Buffer &bf) {};
    
       char *m_data;
       size_t m_size;
    };
    
    // ...
    
    Buffer myBuf(512);
    
    void ReadData();
    void PrintData();
    
    int main()
    {
       ReadData();
       PrintData();
    
       return 0;
    }
    
    void ReadData()
    {
       cin >> myBuf.data();
    }
    
    void PrintData()
    {
       cout << myBuf.data();
    }
    OR, as I suggest... pass the object by reference.

    Code:
    void PrintData(const Buffer &bf);
    void ReadData(Buffer &bf);
    
    int main()
    {
       Buffer myBuf(512);
    
       ReadData(myBuf);
       PrintData(myBuf);
    
       return 0;
    }
    
    void ReadData(Buffer &bf)
    {
       cin >> bf.data();
    }
    
    void PrintData(const Buffer &bf)
    {
       cout << bf.data();
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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