Is static of base shared by derived classes ?

D

Diwa

In the below example, will d1 and d2 have
separate copies of "a" (which is static
in base class). Seems like not.

If not, then how do I have one static for
class d1 and one static for class d2.
some_class is populated by "class b"

------------------------------------------------------

class b
{
public:
static some_class a;
};

class d1 : public b { };
class d2: public b { };
 
I

Ian Collins

Diwa said:
In the below example, will d1 and d2 have
separate copies of "a" (which is static
in base class). Seems like not.
No.

If not, then how do I have one static for
class d1 and one static for class d2.
some_class is populated by "class b"
Declare them in each class.
 
O

Ondra Holub

Diwa napsal:
In the below example, will d1 and d2 have
separate copies of "a" (which is static
in base class). Seems like not.

No, there will be only one copy.
If not, then how do I have one static for
class d1 and one static for class d2.
some_class is populated by "class b"

You can define virtual method returning reference to required class
instance.

class Base
{
public:
virtual SomeClass& Get() = 0;
};

class D1: public Base
{
public:
virtual SomeClass& Get()
{
return sc_instance_;
}

static SomeClass sc_instance_;
};

And then access value of class only with that method.
 
P

peter koch

In the below example, will d1 and d2 have
separate copies of "a" (which is static
in base class). Seems like not. They do not.

If not, then how do I have one static for
class d1 and one static for class d2.
some_class is populated by "class b"

------------------------------------------------------

class b
{
public:
static some_class a;

};

class d1 : public b { };
class d2: public b { };

Lookup CRTP. Sketch:
template< typename t>
struct counter
{
static int count;
};

class d1: public counter<d1> ....

/Peter
 
K

Kai-Uwe Bux

Ondra said:
Diwa napsal:

No, there will be only one copy.


You can define virtual method returning reference to required class
instance.

class Base
{
public:
virtual SomeClass& Get() = 0;
};

class D1: public Base
{
public:
virtual SomeClass& Get()
{
return sc_instance_;
}

static SomeClass sc_instance_;
};

And then access value of class only with that method.

Depending on the underlying problem, CRTP might be an alternative:

template < typename T >
struct b {

static SomeClass a;

};

class d1 : public b<d1> {};
class d2 : public b<d2> {};


Best

Kai-Uwe Bux
 
E

Ernst Murnleitner

Diwa said:
In the below example, will d1 and d2 have
separate copies of "a" (which is static
in base class). Seems like not.


There is only one copy. You access it with b::a.
If not, then how do I have one static for
class d1 and one static for class d2.
some_class is populated by "class b"

class d1 : public b {static some_class a; };

So you have another object, but this is called d1::a.

But now you have the problem that you want to populate it by class b.
I would suggest to make a virtual member function "virtual void populate()"
in b, which you have to redefine in d1 and d2.

Greetings

Ernst
 
D

Diwa

Diwa napsal:


No, there will be only one copy.


You can define virtual method returning reference to required class
instance.

class Base
{
public:
virtual SomeClass& Get() = 0;

};

class D1: public Base
{
public:
virtual SomeClass& Get()
{
return sc_instance_;
}

static SomeClass sc_instance_;

};

And then access value of class only with that method.

Yes, this seems to be the best solution.
 
D

Diwa

They do not.


Lookup CRTP. Sketch:
template< typename t>
struct counter
{
static int count;

};

class d1: public counter<d1> ....

/Peter- Hide quoted text -

- Show quoted text -

Indeed CRTP seems to have been created to solve
exactly what my problem is. Thanks a lot !!
 
D

Diwa

Depending on the underlying problem, CRTP might be an alternative:

template < typename T >
struct b {

static SomeClass a;

};

class d1 : public b<d1> {};
class d2 : public b<d2> {};

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

Thanks Kai, yes as mentioned in my
earlier mail, CRTP is the right solution
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top