Jump to content
  • Advertisement
Sign in to follow this  
MTclip

returns new object or just pointer ??

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Not quite sure about this.. does this return a new Object or Just a pointer to the new object? //code from codesampler.com
class MyCreator : public Creator
{
public:

    virtual Product* MyCreator::Create( ProductId id )
    {
        if( id == YOURS  ) return new MyProduct;    // Switch YOURS for MINE
        if( id == MINE   ) return new YourProduct;  // Switch MINE for YOURS
        if( id == THEIRS ) return new TheirProduct; // New product Added

        return Creator::Create(id); 
    }
};

//-----------------------------------------------------------------------------
// Main entry point for the test application...
//-----------------------------------------------------------------------------
void main( void )
{
    MyCreator mCreator;

    Product *mProd = mCreator.Create( MINE );
    Product *yProd = mCreator.Create( YOURS );
    Product *tProd = mCreator.Create( THEIRS );

    mProd->printBarCode();
    yProd->printBarCode();
    tProd->printBarCode();	
}

Share this post


Link to post
Share on other sites
Advertisement
if it returns a pointer to the object... how do you delete the new object..?

delete *mProd


??
thanks

Share this post


Link to post
Share on other sites
just "delete mProd;".

new returns a pointer to an object, and delete takes that pointer and frees it.

Also, note that if you used the array version of new, like this:
MyProduct *mProd = new MyProduct[5];

you have to delete it with a special array delete:
delete[] mProd;

Share this post


Link to post
Share on other sites
oh...

what about if you store a vector<Product*> and you get rid of one of them from the vector i.e.
..
pop_back()
clear()

will that delete the new object it holds a pointer to?

Share this post


Link to post
Share on other sites
Quote:
Original post by MTclip
oh...

what about if you store a vector<Product*> and you get rid of one of them from the vector i.e.
..
pop_back()
clear()

will that delete the new object it holds a pointer to?


Nope.

Check out the code in this thread for a good way to delete vectors of pointers (their "pointees", actually)

Share this post


Link to post
Share on other sites
Quote:
Original post by MTclip
oh...

what about if you store a vector<Product*> and you get rid of one of them from the vector i.e.
..
pop_back()
clear()

will that delete the new object it holds a pointer to?

No, it will simply remove the pointer from the vector. If you don't call delete on these pointers when you remove them, you'll get a memory leak. You can use smart pointers to handle the release of this memory for you when no more references to it exist. I personally would only use a smart pointer when an object should be responsible for controlling its own life span, but some people use them for just about everything. They're easier and safer, but they also have an additional overhead associated with them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!