Thread: class and pointer question

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    class and pointer question

    Here is my code (I use boost::shared_ptr):

    Code:
    class socket {
    public:
    	SOCKET s;
    	//all socket functions//
    };
    
    typedef boost::shared_ptr<socket> socket_ptr;
    
    
    class client1 : public socket {
    public:
    	short c;
    	//client1 functions//
    };
    
    typedef boost::shared_ptr<client1> client1_ptr;
    
    
    class client2 : public socket {
    public:
    	short c;
    	//client2 functions//
    };
    
    typedef boost::shared_ptr<client2> client2_ptr;
    
    
    /// THE PROBLEM ///
    template <class T>	//not sure if I should use template
    class multisock {
    public:
    	list <T> sockets;
    };

    The problem is I want to have let say 10 instances of client1/client2/or socket class and want to be able to access their members from multisock class:
    this would be like that:
    list <client1_ptr> sockets;
    list <client2_ptr> sockets;
    list <socket_ptr> sockets;

    Im not sure what would be the right way to do this, probably with templates? Anyway I get errors with my code..

    This is how I would do things (and get errors) for class client1:

    Code:
    multisock mc <client1_ptr> ();
    for (int i = 0; i < 10; i++) {
    	mc.sockets.push_back <client1_ptr> ( client1_ptr(new client1()) );
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You don't need templates.

    read about dynamic binding and polymorphism
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    So I could just let multisock class:

    Code:
    class multisock {
    public:
    	list <socket_ptr> sockets;
    };
    and then be able to add client1 and 2 class pointers to the socket list..? How should I access their members through socket_ptr with boost::shared_ptr?

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Using polymorphism and the example you gave in your second post, you will only be able to access public members of the socket class. By using virtual functions you can override the parent class's functionality and therefor utilize the functionality of the child functions. As Mario F stated, read up on polymorphism and dynamic binding as it will give you a firmer understanding of this matter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM