473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Apr 6 '07 #1
6 2136
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
Apr 6 '07 #2
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
Apr 6 '07 #3
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
Apr 6 '07 #4
On Apr 6, 11:42 am, SasQ <s...@go2.plwrote:
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
Apr 6 '07 #5
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
Apr 6 '07 #6
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"
};
}
};

Apr 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rahul Joshi | last post by:
Hi, Is it possible to define static member functions that are 'const', i.e. they just read but do not modify the static data members of a class? Declaring functions like: class SomeClass {...
5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
1
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.