Thread: Classes being able to use other classes functions

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    29

    Classes being able to use other classes functions

    I am trying to write an irc bot that has text games such as ghost, wheel of fortune, hangman, etc on it. Since the number of lines of code is getting big I like to divide everything up into classes and put them into seperate files. One huge one for handling the irc connection and others for handling the various games. My question is: If SendData is a member of the ircbot class, is it possible to use it in the ircgame1 and 2 classes?
    Code:
    #include <iostream>
    
    class ircgame1
    {
    public:
    	void actoncommands(std::string command);
    };
    
    void ircgame1::actoncommands(std::string command)
    {
    	//if (command == "!join")
    		//sendmessage("I see youve joined");	// from irc bot class
    }
    
    class ircgame2
    {
    	void actoncommands(std::string command);
    };
    
    
    class ircbot
    {
    	public:
    		void connect(); //connects to irc and a channel
    		void messageloop();
    	private:	
    		void recvdata(std::string &data);
    		void sendmessage(std::string message);
    		std::string buffer;
    };
    
    void ircbot::connect()
    {
    	//connect to server channel etc	
    }
    
    void ircbot::recvdata(std::string &data)
    {
    	//recv data from irc	
    }
    
    void ircbot::messageloop()
    {
    	ircgame1 textgame;
    	bool textgamemode = true;
    	
    	while (textgamemode)
    	{
    		recvdata(buffer);
    		textgame.actoncommands(buffer);
    	}
    }
    
    
    int main()
    {
    	ircbot bot;
    	bot.connect();
    	bot.messageloop();
    	std::cin.get();
    	return 0;
    }
    I want game1 class to be able to use ircbots sendata function to send text to irc. Is this possible, how would I go about doing this? Is there a better way?

    (I can make an irc bot that connects to IRC already and have it act on commands but the ircbot class and all of its functions are getting harder and harder to look through and harder to add new feature too as the number of lines increases, I would Like an easier way to organize it)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    WinSock has standard functions for sending and receiving "data". Those functions may not be in your final program. Look more into WinSock programming, you're running into issues that you don't need to be.

    As for your question. As long as it's a public member of your class, then it can be used in anything you wish. It may be preferable to you to use some inheritance, though.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Make each ircgame inherit the ircbot class.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If SendData is a member of the ircbot class, is it possible to use it in the ircgame1 and 2 classes?
    If you are asking whether SendData() in the ircbot class can be called by objects of type ircgame1 and ircgame2, the answer is no. Generally, functions in a class can only be called by objects of the class.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    what about making it a friend function?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The problem with using inheritence for this is that it would mean you would have an instance of rcbot for each ircgame* object when you only need one per server.

    I sugest having the Ircgame* objects contain a pointer to an Ircbot object, set it in the constructor perhaps.
    Last edited by Quantum1024; 01-29-2006 at 10:45 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even implement ircbot as a singleton object, since you only ever need the one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM