Thread: objects

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    objects

    i created a simple program to store two different names. how do i acesss the objects thats in int main from a class function?
    Code:
     #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    class nameishere {
        protected:
            char *theName;    
    };
    
    class player_name: public nameishere{
        public:
            player_name (char *);       
    };
    
    player_name::player_name (char* name)
    {
        theName = name;
    }  
    
    class display_theName {
        public:
            void display ();
    };
    
    void display_theName::display()
    {
        cout << sakic.theName; // <-- doesnt work
        cout << joe.theName; // <-- doesnt work
    }
    
    
    int main()
    {
        player_name sakic ("sakic");
        player_name joe ("joe");
        getch();
        return 0;
    }

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    That wont work cos the function does not know where them are objects are declared or anything. It has no local variables to look at, nor has it got any globals to look at it. You need to pass the address of the classes that you have declared in main to the display_theName function. Or use the local variable which you have used within your class.

    Code:
    
    
    void display_theName::display()
    {
        cout << theName;
    }
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM