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

N

newbie

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;
}
 
V

Victor Bazarov

newbie said:
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
 
V

Victor Bazarov

newbie said:
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
 
S

SasQ

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.
 
N

newbie

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


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,
 
S

SasQ

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.

Don't quote sigs. Thank you.
 
B

bb

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"
};
}
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top