Sign in to follow this  
Maverick Programmer

[C++] Contain an object and never have to reference it again

Recommended Posts

My C++ terminology is wayyy off but my problem is I want to pass an object ( A ) to another object ( B ) and never have to do it again yet B has up-to-date info on A. I'm using Boost though Ima newbeh at it, so any help is radical. Here's my current layout. It's wrong. I know. :D
class A; //assume it has the method "setNumbah" and "printzNumbah"

class B
{
private:
boost::shared_ptr< A > a;

public:
B () { ; }
~B () { ; }

void referenceObjectOnce ( boost::shared_ptr< A > A )
{
     a = A;
}

void print ( void )
{
     a->printzNumbah ( );
{

};

//later in Main [ insert comic book announcer voice here ]
boost::scoped_ptr< B > b;
boost::shared_ptr< A > a;

a.setNumbah ( 3 );
b.referenceObjectOnce ( a );
a.printzNumbah ( ); // output 3
b.print ( );        // output 3

a.setNumbah ( 42 ); 
a.printzNumbah ( ); // output 42
b.print ( );        // output 42




I can do this the normal way but I'm trying to learn all of Boost's glorious advantages and working with Boost in general. Tanks. ( Not "thanks" but "tanks"! ) :p [Edited by - ApochPiQ on June 21, 2009 6:41:58 PM]

Share this post


Link to post
Share on other sites
The main aim of shared_ptr is to take care of dynamically allocated objects (which your example doesn't show). Another implication is that all objects having a shared_ptr to something have a say in the lifetime of the object: it is deleted when the last shared_ptr to it is destroyed.

With correct syntax:


class A; //assume it has the method "setNumbah" and "printzNumbah"

class B
{
private:
boost::shared_ptr< A > a;

public:
B () { ; }
~B () { ; }

//the copy constructor of shared_ptr does things
//so avoid making unnecessary copies and pass by const reference
void referenceObjectOnce (const boost::shared_ptr< A >& pa )
{
a = pa;
}

void print ( void )
{
a->printzNumbah ( );
}

};

int main()
{
//no need to use scoped_ptr if b doesn't need to be dynamically allocated
boost::scoped_ptr< B > b(new B);
boost::shared_ptr< A > a(new A);

//smart pointers overload operators to look like regular pointers
a->setNumbah ( 3 );
b->referenceObjectOnce ( a );
a->printzNumbah ( ); // output 3
b->print ( ); // output 3

a->setNumbah ( 42 );
a->printzNumbah ( ); // output 42
b->print ( ); // output 42
}





If that is not an answer to your question (whatever it is?) you might need to clarify.

Share this post


Link to post
Share on other sites
Well, what I'm really trying to do is have a list of objects ( A ) stored into an object ( B ) that A only has to be referenced once and the B can update these objects. Like an entity manager. This is what it looks like right now ( roughly ):

 
class Entity; //for this example, assume it contains setName() and printName()

class Manager
{
private:
std::list< boost::shared_ptr< Entity > > lstEntities;

public:
Entity () { }
~Entity ( ) { if ( !lstEntities. empty () ) lstEntities.clear ( ); }

void addEntity ( const boost::shared_ptr< Entity > &toAdd )
{
lstEntities.push_back ( toAdd );
}

void printEntities ( void )
{
std::list< boost::shared_ptr< Entity > >::iterator i = lstEntities.begin ();

for( ; i != lstEntities.end(); ++i )
{
(*i)->printName();
}

}

};







Manager M;
boost::shared_ptr< Entity > Entity;
Entity.reset( new Entity );

Entity->setName ( "Bill" );
M.addEntity( Entity );

M.printEntities(); //Prints "Bill"
Entity->setName ( "Randy" );

M.printEntities(); //Now prints "Randy" without needed to addEntity again






I hope this makes more sense. Currently this is not working. It doesn't have any compiler issues, but it isn't right run-time and some times it locks up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this