polymorphism in static member functions? --can it be virtual?

Discussion in 'C++' started by newbie, Apr 6, 2007.

  1. newbie

    newbie Guest

    class AbstractBook {
    public:
    virtual static AbstractBook* Allocate () =0;
    virtual PrintTitle() = 0;
    }

    class ScifiBook {
    public:
    static AbstractBook* Allocate () { return new ScifiBook; }
    virtual PrintTitle() { cout << "scifi"; }
    private:
    int num_stories;
    }

    class NovelBook {
    static AbstractBook* Allocate () { return new NovelBook; }
    virtual PrintTitle() { cout << "Novel"; }
    private:
    int num_pages;
    }
     
    newbie, Apr 6, 2007
    #1
    1. Advertising

  2. newbie wrote:
    > class AbstractBook {
    > public:
    > virtual static AbstractBook* Allocate () =0;
    > virtual PrintTitle() = 0;
    > }

    ;
    >
    > class ScifiBook {


    class ScifiBook : public AbstractBook

    > public:
    > static AbstractBook* Allocate () { return new ScifiBook; }
    > virtual PrintTitle() { cout << "scifi"; }
    > private:
    > int num_stories;
    > }

    ;
    >
    > class NovelBook {



    class NovelBook : public AbstractBook

    > static AbstractBook* Allocate () { return new NovelBook; }
    > virtual PrintTitle() { cout << "Novel"; }
    > private:
    > int num_pages;
    > }

    ;

    No. But I feel your pain, this has been discussed and suggested
    many times. You could probably use templates to accomplish something
    similar.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 6, 2007
    #2
    1. Advertising

  3. newbie wrote:
    > class AbstractBook {
    > public:
    > virtual static AbstractBook* Allocate () =0;
    > virtual PrintTitle() = 0;
    > }
    >
    > class ScifiBook {
    > public:
    > static AbstractBook* Allocate () { return new ScifiBook; }
    > virtual PrintTitle() { cout << "scifi"; }
    > private:
    > int num_stories;
    > }
    >
    > class NovelBook {
    > static AbstractBook* Allocate () { return new NovelBook; }
    > virtual PrintTitle() { cout << "Novel"; }
    > private:
    > int num_pages;
    > }


    Read up about 'clone' virtual function, you can probably get
    away without needing a static one...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 6, 2007
    #3
  4. newbie

    SasQ Guest

    Dnia Fri, 06 Apr 2007 11:21:37 -0700, newbie napisa³(a):

    > class AbstractBook {
    > public:
    > virtual static AbstractBook* Allocate () =0;


    static method cannot be virtual, because it's not
    bounded with any particular object, but with the
    class of objects as a whole.

    --
    SasQ
     
    SasQ, Apr 6, 2007
    #4
  5. newbie

    newbie Guest

    On Apr 6, 11:42 am, SasQ <> wrote:
    > Dnia Fri, 06 Apr 2007 11:21:37 -0700, newbie napisa³(a):
    >
    > > class AbstractBook {
    > > public:
    > > virtual static AbstractBook* Allocate () =0;

    >
    > static method cannot be virtual, because it's not
    > bounded with any particular object, but with the
    > class of objects as a whole.


    Then, can I do something like

    class AbstractBook {
    public:
    static AbstractBook* Allocate () { return NULL;};
    virtual PrintTitle() = 0;

    }

    class ScifiBook : public AbstractBook {
    public:
    static AbstractBook* Allocate () { return new ScifiBook; }
    virtual PrintTitle() { cout << "scifi"; }
    private:
    int num_stories;

    }

    class NovelBook : public AbstractBook{
    static AbstractBook* Allocate () { return new NovelBook; }
    virtual PrintTitle() { cout << "Novel"; }
    private:
    int num_pages;
    }

    I mainly want to have different version of Allocate (), which will be
    the interface to another function.
    Thanks,

    >
    > --
    > SasQ
     
    newbie, Apr 6, 2007
    #5
  6. newbie

    SasQ Guest

    Dnia Fri, 06 Apr 2007 11:49:07 -0700, newbie napisa³(a):

    > Then, can I do something like


    Then U've got the method in derived class hiding the
    method from the base class. If you call it using
    a pointer to base, you'll still be calling the base-class
    version, not the derived.

    > I mainly want to have different version of Allocate (),
    > which will be the interface to another function.


    Consider 'Abstract Factory' design pattern.

    >> --
    >> SasQ


    Don't quote sigs. Thank you.

    --
    SasQ
     
    SasQ, Apr 6, 2007
    #6
  7. newbie

    bb Guest

    See if the following simple 'Factory Method' pattern helps?

    struct AbstractBook {
    virtual PrintTitle() = 0;
    };

    class ScifiBook : public AbstractBook {
    public:
    virtual PrintTitle() { cout << "scifi"; }
    private:
    int num_stories;
    };

    class NovelBook : public AbstractBook {
    public:
    virtual PrintTitle() { cout << "Novel"; }
    private:
    int num_pages;
    };

    struct bookFactory {
    static AbstractBook* createScifi() { return new ScifiBook; }
    static AbstractBook* createNovel() { return new NovelBook; }
    // -- or --
    static AbstractBook* create(int what) {
    switch(what) {
    case 1:
    return new ScifiBook;
    case 2:
    return new NovelBook:
    default:
    throw "do not know"
    };
    }
    };
     
    bb, Apr 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith

    virtual static member functions?

    john smith, Aug 8, 2003, in forum: C++
    Replies:
    2
    Views:
    5,860
    Shane Beasley
    Aug 8, 2003
  2. Krivenok Dmitry
    Replies:
    13
    Views:
    1,452
    Axter
    Jun 1, 2006
  3. John Goche
    Replies:
    10
    Views:
    772
    Marcus Kwok
    Dec 8, 2006
  4. Jonas Huckestein
    Replies:
    3
    Views:
    357
    Rolf Magnus
    Feb 12, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,363
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page