CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2002
    Posts
    137

    Protoype Design Pattern Link Issue

    Hi All,

    I am using the prototype design pattern to design a static library in linux.

    the class architecture looks like below:

    Code:
    //Product.h
    class Product
    {
    public:
       virtual ~Product() {}
       virtual Product* clone() const = 0;
       static Product* makeProduct(string type);
       static Product* addPrototype(string type, Product* p);
       static map<string, Product*>& getMap()
       {
            static map<string, Product*> protoTable;
            return protoTable;
       }
    };
    
    #define IMPLEMENT_CLONE(TYPE) \
       Product* clone() const { return new TYPE(*this); }
    
    #define MAKE_PROTOTYPE(TYPE) \
       Product* TYPE ## _myProtoype = \
          Product::addPrototype(#TYPE, new TYPE());

    Code:
    // product1.h
    #include "product.h"
    
    class Product1: public Product
    {
    public:
       IMPLEMENT_CLONE(Product1)
       // etc.
    };
    Code:
    // product1.cpp
    #include "product1.h"
    MAKE_PROTOTYPE(Product1)
    // etc.

    Code:
    //in test.cpp
    int main()
    {
        Product* pObj = Product::makeProduct("Product1");
       // etc.
    }
    all subclass of Product will exist in their own .o file, like Product1.o, Product2.o ......
    and they will be grouped into a libProduct.a static lib.

    Code:
    gcc test.o libProduct.a
    problem is that my test main program it does not know the existence of Product1 and Product2 for example, then Product1.o and Product2.o will not be linked. And when I was trying to create the Prodcut1 object as in the test main function, it will return NULL pointer.

    Your ideas and expertise advice is very appreciated.

    Thanks a lot in advance!
    sandodo

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Protoype Design Pattern Link Issue

    you havent added it (the product) to the protoTable before you try and make it.

    you can do it like this:
    Code:
    Product::getMap()["Product1"] = new Product1;
    the getMap code and the table should really be moved to a factory class though - it's not strictly part of the product.
    Last edited by Amleto; March 9th, 2011 at 04:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured