returns new object or just pointer ??

Started by
6 comments, last by Nemesis2k2 18 years, 9 months ago
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();	
}

Advertisement
It returns a Product* which is a pointer to a Product.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
if it returns a pointer to the object... how do you delete the new object..?

delete *mProd


??
thanks
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;
ah, thanks for that....
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?
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)

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.

This topic is closed to new replies.

Advertisement