473,385 Members | 1,766 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.

Is static of base shared by derived classes ?

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

------------------------------------------------------
Thanks
Diwakar

Feb 9 '07 #1
8 1454
Diwa wrote:
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.

--
Ian Collins.
Feb 9 '07 #2

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.

Feb 9 '07 #3
On 9 Feb., 23:29, "Diwa" <shettydiwa...@gmail.comwrote:
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

Feb 9 '07 #4
Diwa wrote:
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

Feb 9 '07 #5
Ondra Holub wrote:
>
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.
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
Feb 9 '07 #6
On Feb 9, 5:42 pm, "Ondra Holub" <ondra.ho...@post.czwrote:
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.
Yes, this seems to be the best solution.

Feb 10 '07 #7
On Feb 9, 5:54 pm, "peter koch" <peter.koch.lar...@gmail.comwrote:
On 9 Feb., 23:29, "Diwa" <shettydiwa...@gmail.comwrote:


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- Hide quoted text -

- Show quoted text -
Indeed CRTP seems to have been created to solve
exactly what my problem is. Thanks a lot !!

Feb 10 '07 #8
On Feb 9, 6:01 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Ondra Holub wrote:
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.

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

Feb 10 '07 #9

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

Similar topics

4
by: Grey Plastic | last post by:
I have a program where several classes each have a single static std::map to keep track of all the instances of that class. For each of these classes, I want a static lookupByID(int id) method...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
10
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of...
8
by: Jon Slaughter | last post by:
Is there any way to declare a member of a base class to make the itself in the derived class to be static? i.e., I have two classes class Base { protected: char *name;
2
by: Mark | last post by:
I know that you can't have a static virtual property, but is there a way to simulate the same results? I have a base class that I want to extend so that you can change a value and it inherits...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
2
by: cmonthenet | 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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.