Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Possible for C++ class to force derived class to contain static datamember?

Reply
Thread Tools

Possible for C++ class to force derived class to contain static datamember?

 
 
Hubert Fritz
Guest
Posts: n/a
 
      01-28-2007
Hello,

I fear I want to have something which is not possible in C++.

How is it possible to define a base class so that the derived
class is forced to contain a static member variable, which can
be used by static member functions of the base class?
Something like
virtual static XClass* pXClass;
/* pXClass shall be pure virtual, so not static in base class, but
static in derived class */
The static class related part of the derivation could so provide
manager functionality for the object instances of the class.

The only solution I currently see is to have a base class
containing
XClass* pXClass; /* non static */
and related member functions.
Derive a singleton class from this, so I would have some "static"
effect. And have a separate class XClass chained by the derived class.


Best regards,
Hubert
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-28-2007
Hubert Fritz wrote:
> Hello,
>
> I fear I want to have something which is not possible in C++.
>
> How is it possible to define a base class so that the derived
> class is forced to contain a static member variable, which can
> be used by static member functions of the base class?


You might be able to do something with templates, but what problem are
you attempting to solve?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Hubert Fritz
Guest
Posts: n/a
 
      01-28-2007
Ian Collins wrote:
> Hubert Fritz wrote:
>> Hello,
>>
>> I fear I want to have something which is not possible in C++.
>>
>> How is it possible to define a base class so that the derived
>> class is forced to contain a static member variable, which can
>> be used by static member functions of the base class?

>
> You might be able to do something with templates, but what problem are
> you attempting to solve?
>


My problem is more a theoretical one - to understand a) the limitations
of the language or b) my limitated understanding of
the language.

Given that you want to manage objects of a class, e.g. create
them, find them again by several keys, delete them. So you
might have an anchor, and chained the objects in a list.
Using a single class to implement this concept, you can have
the anchor static - it will belong to the class. The link
field in the class will be non-static and belongs to the
objects. Member functions manipulating the linked list
would be static for being used independently of particular
objects. Let us name this class ClassA.

Now imagine to generalise this and try to find a base class
ClassB gathering the algorithms and derive one, or several
classes with functionality such as ClassA, we may call them
ClassD1, ... ClassDn.
The static member (anchor) of ClassA is obviously needed in
the derived classes. Algorithms shall be inherited from ClassB.

For me it is not clear if there is a way to provide
or force the definition of the static member in ClassD1
by ClassB and use it from static member functions in
ClassB.

Hubert
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-28-2007
Hubert Fritz wrote:
>
> My problem is more a theoretical one - to understand a) the limitations
> of the language or b) my limitated understanding of
> the language.
>
> Given that you want to manage objects of a class, e.g. create
> them, find them again by several keys, delete them. So you
> might have an anchor, and chained the objects in a list.
> Using a single class to implement this concept, you can have
> the anchor static - it will belong to the class. The link
> field in the class will be non-static and belongs to the
> objects. Member functions manipulating the linked list
> would be static for being used independently of particular
> objects. Let us name this class ClassA.
>
> Now imagine to generalise this and try to find a base class
> ClassB gathering the algorithms and derive one, or several
> classes with functionality such as ClassA, we may call them
> ClassD1, ... ClassDn.
> The static member (anchor) of ClassA is obviously needed in
> the derived classes. Algorithms shall be inherited from ClassB.
>
> For me it is not clear if there is a way to provide
> or force the definition of the static member in ClassD1
> by ClassB and use it from static member functions in
> ClassB.
>

I'm still not 100% clear what you are trying to achieve, but would
something like this do what you want?

#include <iostream>

template <typename Derived>
struct B
{
static int doSomething()
{
return Derived::staticMember;
}
};

struct D : B<D>
{
static const int staticMember = 42;
};

int main()
{
std::cout << D::doSomething() << std::endl;
}

--
Ian Collins.
 
Reply With Quote
 
Hubert Fritz
Guest
Posts: n/a
 
      01-28-2007

Hello Ian,

I have to think about and try out the
reachability of staticMember by B.
The static member in D seems not to be
enforced by struct B. But this is no
severe problem.
Somehow strange that the base class is
dependent from the derived one.

Hubert Fritz


Ian Collins wrote:

> I'm still not 100% clear what you are trying to achieve, but would
> something like this do what you want?
>
> #include <iostream>
>
> template <typename Derived>
> struct B
> {
> static int doSomething()
> {
> return Derived::staticMember;
> }
> };
>
> struct D : B<D>
> {
> static const int staticMember = 42;
> };
>
> int main()
> {
> std::cout << D::doSomething() << std::endl;
> }
>

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-28-2007
Hubert Fritz wrote:

Please try not to top post.

>
> Ian Collins wrote:
>
>>I'm still not 100% clear what you are trying to achieve, but would
>>something like this do what you want?
>>
>>#include <iostream>
>>
>>template <typename Derived>
>>struct B
>>{
>> static int doSomething()
>> {
>> return Derived::staticMember;
>> }
>>};
>>
>>struct D : B<D>
>>{
>> static const int staticMember = 42;
>>};
>>
>>int main()
>>{
>> std::cout << D::doSomething() << std::endl;
>>}
>>

> Hello Ian,
>
> I have to think about and try out the
> reachability of staticMember by B.


Either make it public, or

class D : public B<D>
{
friend class B<D>;
static const int staticMember = 42;
};

> The static member in D seems not to be
> enforced by struct B. But this is no
> severe problem.


It is if anything tries to call doSomething().

> Somehow strange that the base class is
> dependent from the derived one.
>

Welcome to the world of C++!

--
Ian Collins.
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      01-28-2007


On Jan 28, 12:35 pm, Hubert Fritz <(E-Mail Removed)> wrote:
> Hello,
>
> I fear I want to have something which is not possible in C++.
>
> How is it possible to define a base class so that the derived
> class is forced to contain a static member variable, which can
> be used by static member functions of the base class?


The answer is simple: have the base class declare the static data
member itself - thereby ensuring that the static data member exists
and is accessible from the derived class.

After all, what difference does it make whether the base class or its
derived class is the one to declare the static data member? The
overhead in either case is exactly the same.

Greg

 
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
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments