Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > polymorphism in static member functions? --can it be virtual?

Reply
Thread Tools

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

 
 
newbie
Guest
Posts: n/a
 
      04-06-2007
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2007
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2007
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


 
Reply With Quote
 
SasQ
Guest
Posts: n/a
 
      04-06-2007
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
 
Reply With Quote
 
newbie
Guest
Posts: n/a
 
      04-06-2007
On Apr 6, 11:42 am, SasQ <(E-Mail Removed)> 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


 
Reply With Quote
 
SasQ
Guest
Posts: n/a
 
      04-06-2007
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
 
Reply With Quote
 
bb
Guest
Posts: n/a
 
      04-06-2007
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"
};
}
};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments