class and pointer question

This is a discussion on class and pointer question within the C++ Programming forums, part of the General Programming Boards category; Here is my code (I use boost::shared_ptr): Code: class socket { public: SOCKET s; //all socket functions// }; typedef boost::shared_ptr<socket> ...

  1. #1
    l2u
    l2u is offline
    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
    Portugal
    Posts
    7,578
    You don't need templates.

    read about dynamic binding and polymorphism
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    l2u
    l2u is offline
    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
    University of Waterloo
    Posts
    1,904
    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, 08:05 AM
  2. Replies: 25
    Last Post: 10-29-2007, 05: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, 11:19 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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