473,406 Members | 2,345 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,406 software developers and data experts.

virtual+static

Hi,
I am dealing with problem where I need virtual+static function:

enum {A=0,B=1};

Factory.cpp:
------------------
Base * createInstance(int classType,char *name)
{
if(classType == A)
{
return dynamic_cast<Base *> ( A::getInstance(name));
}
else if(classType == B)
{
return dynamic_cast<Base *> (B::getInstance(name));
}
}

base.cpp:
---------------
class Base {
public:
virtual getInstance(string name)=0; //Pure virtual
};
class A:public Base {
public:
static getInstance(string name)
{
if(name already in objList)
return objinlist;
else
return new A();
}
private:
A(){}
List<A> objList;
}

class B:public Base {
public:
static getInstance(string name)
{
if(name already in objList)
return objinlist;
else
return new B();
}
private:
B(){}
List<B> objList;
}

getInstance() is made static in concrete class as we want it to be able
to create instances of that class (hence it has to be static, since we
all this method on class itself)
getInstance() is made pure virtual in base class since I want to
"enforce" the rule that ever yclass deriving from base must implement
this method. If someone doesnt and tries to create a instance, he will
get compile error.
However above code fails to compile:
The function A::getInstance cannot be both virtual and static. This is
a valid case where I need this functionality from design point of view,
but C++ doesnt allow me to do that. Is there any way I can get around
this?
Thanks,
Sunil

Jun 21 '06 #1
7 2577
sunil wrote:
I am dealing with problem where I need virtual+static function:

enum {A=0,B=1};

Factory.cpp:
------------------
Base * createInstance(int classType,char *name)
{
if(classType == A)
{
return dynamic_cast<Base *> ( A::getInstance(name));
There seems to be no need for the dynamic_cast, conversion from
a pointer to the derived to a pointer to the base class is implicit
and provided by the language.
}
else if(classType == B)
{
return dynamic_cast<Base *> (B::getInstance(name));
}
}

base.cpp:
---------------
class Base {
public:
virtual getInstance(string name)=0; //Pure virtual
};
class A:public Base {
public:
static getInstance(string name)
{
if(name already in objList)
return objinlist;
else
return new A();
}
private:
A(){}
List<A> objList;
}

class B:public Base {
public:
static getInstance(string name)
{
if(name already in objList)
return objinlist;
else
return new B();
}
private:
B(){}
List<B> objList;
}

getInstance() is made static in concrete class as we want it to be
able to create instances of that class (hence it has to be static,
since we all this method on class itself)
getInstance() is made pure virtual in base class since I want to
"enforce" the rule that ever yclass deriving from base must implement
this method. If someone doesnt and tries to create a instance, he will
get compile error.
However above code fails to compile:
The function A::getInstance cannot be both virtual and static. This is
a valid case where I need this functionality from design point of
view, but C++ doesnt allow me to do that. Is there any way I can get
around this?


What happens if you drop the 'getInstance' from 'Base' altogether?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #2

sunil wrote:
The function A::getInstance cannot be both virtual and static. This is
a valid case where I need this functionality from design point of view,
but C++ doesnt allow me to do that. Is there any way I can get around
this?


No, and it would make no sense if there was.

You might find a way to cause a compile error with boost:

template<class sub>
class X
{
public:
X() : { BOOST_STATIC_CHECK((sizeof(sub::create()))); }
};

class Y : public X<Y>
{
};

but then you loose polymorphism.

You might consider that you are using the wrong pattern and try
Prototype instead.

http://en.wikipedia.org/wiki/Prototype_pattern

Check GOF.

Jun 21 '06 #3
Noah Roberts wrote:
sunil wrote:
The function A::getInstance cannot be both virtual and static. This
is a valid case where I need this functionality from design point of
view, but C++ doesnt allow me to do that. Is there any way I can
get around this?


No, and it would make no sense if there was.

You might find a way to cause a compile error with boost:

template<class sub>
class X
{
public:
X() : { BOOST_STATIC_CHECK((sizeof(sub::create()))); }
};

class Y : public X<Y>
{
};

but then you loose polymorphism.

You might consider that you are using the wrong pattern and try
Prototype instead.

http://en.wikipedia.org/wiki/Prototype_pattern

Check GOF.


I don't understand the problem. The OP wants a compile-time error. Where?
While compiling the "createInstance" function (or what's it called?), right?
The function is a [big] switch/case statement for A::blah, B::blah, C::blah
and so on, for every class deriving from 'Base', right? Now, if 'Base' has
'blah', it would be bad. If 'Base' doesn't have 'blah' member (which it
doesn't define anyway, since the proposed one is *pure*), an attempt to call
any of A::blah or B::blah or C::blah (or whatever::blah) would fail and
force
the developer of A, B, C, whatever, to define that function. Am I missing
something here?

"You folks are young, you think the life is simple... But the life is so
much simpler!" -- from an old Russian joke.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #4

Victor Bazarov wrote:
I don't understand the problem.


He wants to enforce the policy that all subclasses of a type have a
certain static member function. There is no way to do this.

Jun 21 '06 #5
Noah Roberts wrote:
Victor Bazarov wrote:
I don't understand the problem.


He wants to enforce the policy that all subclasses of a type have a
certain static member function. There is no way to do this.


It's true that there is no way to do that specifically, but there are
many things in C++ that are not enforceable. For example, there is
no way to enforce that there is a function called 'foo' with a certain
set of arguments and returning void, declared in scope. I can easily
circumvent it with a declaration

void (*foo)(...) = 0;

, right? But if there is no declaration of 'foo' at all, then the
compiler will at least complain if I try calling it. That's why I've
suggested to drop the function from the base class and just make sure
the program tries to call those functions from other classes with the
class name and the :: in front. It won't force the implementor of the
derive classes to add a *static* member *function*, but it will make
him think. And if he resolves it by having a static *pointer* to
function or a static *functor* there, why the hell not?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #6
Noah Roberts wrote:
Victor Bazarov wrote:

I don't understand the problem.

He wants to enforce the policy that all subclasses of a type have a
certain static member function. There is no way to do this.


That's overdesign. The actual requirement is that all derived types that
are used in that factory function must provide that static member
function. And since the factory function calls that function, if it's
not present, it's an error.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 21 '06 #7

Pete Becker wrote:
Noah Roberts wrote:
Victor Bazarov wrote:

I don't understand the problem.

He wants to enforce the policy that all subclasses of a type have a
certain static member function. There is no way to do this.


That's overdesign. The actual requirement is that all derived types that
are used in that factory function must provide that static member
function. And since the factory function calls that function, if it's
not present, it's an error.


Whatever. I never said what the OP was trying to do was good. I said
it was impossible. The OP has the answer to his question and two
alternatives. If you guys want to bitch about the fact that I answered
the question fine...be my guest.

Jun 22 '06 #8

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

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
2
by: Tony Johansson | last post by:
Hello Experts! I know that you can't have virtual static methods and I know what a static method is. A static method exist only one time no matter how many object you have. You have this...
3
by: Donald Gillies | last post by:
So I have a virtual base class. I want each child class to have its own unique log. There may be hundreds of child classes but all classes of a particular type should share one log. After...
5
by: babylon | last post by:
I would like to have a parent class that make one of the static function as abstract (i can't do it in csharp now) so that all the subclasses have to implement a static function with same...
2
by: Pavils Jurjans | last post by:
Hello, I wanted to get some feedback on why there are not allowed virtual static members in C#. Say, I have the Parent class, that hosts number of protected methods, that occasionaly make...
1
by: archana15bang | last post by:
hi, i have some queries: 1. can friend functions be virtuals??? and why 2. can friend functions be Static??? and why 3. can virtuals functions be static??? and why
5
by: Ralfeus | last post by:
Hi all. I have a base class and several classes inherited from this base class. I need to create a instances counter for each class. I thought about something like creation of virtual static...
10
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
4
emibt08
by: emibt08 | last post by:
Hi. I know the title looks a little bit silly and that we can not have pure virtual static functions. But i've been wondering what approach to take to make the abstraction. This is the case: I have...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.