Thread: boost shared_ptr problem

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

    boost shared_ptr problem

    Heres my code:

    Code:
    class sockclass {
    };
    class someclass : public sockclass {
    };
    
    typedef boost::shared_ptr<sockclass> sockclass_ptr;
    
    void somefunc() {
    	list <sockclass_ptr> sockets;
    
    	for (int i = 0; i < 10; i++)
    		sockets.push_back( sockclass_ptr(new someclass()) );
    
    	list <sockclass_ptr>::iterator p;
    	for (p = sockets.begin(); p != sockets.end(); p++) {
    		cout << "data: " << endl;
    		
    		/*
    		now if i want access to element in sockclass with p->
    		i get error: element is not a member of 'boost::shared_ptr<T>
    		*/
    	}
    
    }
    Please help me, what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    I think (*p)->? would work, iterators are pointers and the list values are pointers aswell.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Hey, that works.. Thanks a lot

    One more thing.. What would be the right way to get member from someclass with p (using this boost shared ptr)?

    Thanks again

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    someclass cannot be accessed from sockclass but it can be done the other way around.
    change your code to use someclass instead:

    Code:
    list <someclass_ptr> sockets;
    
    for (int i = 0; i < 10; i++)
    	sockets.push_back( someclass_ptr(new someclass()) );
    
    list <someclass_ptr>::iterator p;

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by X PaYnE X
    someclass cannot be accessed from sockclass but it can be done the other way around.
    change your code to use someclass instead:

    Code:
    list <someclass_ptr> sockets;
    
    for (int i = 0; i < 10; i++)
    	sockets.push_back( someclass_ptr(new someclass()) );
    
    list <someclass_ptr>::iterator p;
    Can I access element in sockclass too with that code?

    I want to be able to access elements in both classes with one pointer (p)..

    Also is this the right approach?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    yes and yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Problem With Use of Unicode + Boost
    By Tonto in forum C++ Programming
    Replies: 0
    Last Post: 04-05-2007, 08:55 PM
  4. problem with boost random func (again)
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 03:22 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM