Question about "new" keyword

I'm learning about design patterns, specifically about the decorator pattern. There's a bit of code that I don't know what's going on exactly. Here's a snippet of the classes I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 class Component
{
    public:
        virtual ~Component(){};
        virtual std::string Describe() const = 0;
};

class Object : public Component
{
    public:
        Object( const std::string& name ): mName( name ){}
        virtual std::string Describe() const{ return mName; }

    private:
        std::string mName;
};


And within the main function, it gets called like this:

1
2
3
4
5
6
int main()
{
     Component* ship = new Object( "Ship" );
     std::cout << ship->Describe() << "\n";
     delete ship;
}


I don't understand what's going on with this line specifically:

 
Component* ship = new Object( "Ship" );


What's happening there. Why is Object being used with the new keyword if "ship" is of component type.

Thanks a lot for taking the time
Last edited on
Pointer to a base class ( Component ) may points to any derived classes ( Object ). This is a part of polymorphism mechanism.

See http://www.dev-hq.net/c++/21--polymorphism

Thanks a lot for the info :)
Topic archived. No new replies allowed.