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

Static variable

Hello,
I have a doubt about static variable.

class B
{
private:
static int x;
public:
static set(int y)
{
x=y;
}
};

int B::x=10;

class D1: class B
{
}
class D2: class B
{
}

Here if I modify the static variable by an object of class D1, then for the
object of D2 the same modification is visible.

Here suppose I want that D2 object's x should not be modified by modifying
D1 object's x.
How to acheive this?
Can it anyway be done by using templete class.
Aug 23 '05 #1
4 1956
"santosh" <sa*************@in.bosch.com> schrieb im Newsbeitrag
news:de**********@ns2.fe.internet.bosch.com...
Hello,
I have a doubt about static variable.

class B
{
private:
static int x;
public:
static set(int y)
{
x=y;
}
};

int B::x=10;

class D1: class B
{
}
class D2: class B
{
}

Here if I modify the static variable by an object of class D1, then for the object of D2 the same modification is visible.

Here suppose I want that D2 object's x should not be modified by modifying D1 object's x.
static variables in classes exist per class not per object. Don't use static
if you want that each object has it's x.
How to acheive this?
Can it anyway be done by using templete class.

Aug 23 '05 #2
> Here suppose I want that D2 object's x should not be modified by modifying
D1 object's x.
How to acheive this?

Have a static member each in D1 and D2?

Srini

Aug 23 '05 #3
santosh wrote:

Hello,
I have a doubt about static variable.

class B
{
private:
static int x;
public:
static set(int y)
{
x=y;
}
};

int B::x=10;

class D1: class B
{
}
class D2: class B
{
}

Here if I modify the static variable by an object of class D1, then for the
object of D2 the same modification is visible.

Here suppose I want that D2 object's x should not be modified by modifying
D1 object's x.
How to acheive this?


Move x into D1 and D2.
Then each of them has its own variable and hence they don't interfere.

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 23 '05 #4

"santosh" <sa*************@in.bosch.com> wrote in message
news:de**********@ns2.fe.internet.bosch.com...
Hello,
I have a doubt about static variable.

class B
{
private:
static int x;
public:
static set(int y)
{
x=y;
}
};

int B::x=10;

class D1: class B
{
}
class D2: class B
{
}

Here if I modify the static variable by an object of class D1, then for
the
object of D2 the same modification is visible.

Here suppose I want that D2 object's x should not be modified by
modifying
D1 object's x.
How to acheive this?
Can it anyway be done by using templete class.


The one and only static variable you have here is B::x. It is the compiler
that translates D1::x, D2::x, (and o.x when o is an object of type B, D1 or
D2) into B::x. Therefore, they are all just one thing.

To achieve you goal, just declare and define a static x in both D1 and D2.
However, by this way, changing D1::x or D2::x will not have effect on B,
since now D1::x, D2::x and B::x are all different things.

If, however, you want to change the static x for the base class but have D1
and D2 each maintaining individual static objects, you need two bases, say
B1 and B2:

class B1{public: static int x;};
class B2{public: static int x;};

int B1::x = 0;
int B2::x = 0;

class D1: public B1{};
class D2: public B2{};

This of course can simply apply a template as you have been mentioning:

template <typename T>
class B{public: static int x;};

template <typename T>
int B<T>::x = 0;

class D1: public B<D1>{};
class D2: public B<D2>{};

Note that B<D1> and B<D2> are totally different types, so D1 and D2 does NOT
share a common base. To remedy, you can make a common base for all B<T>:

class CommonBase{};

template <typename T>
class B: public CommonBase
{public: static int x;};

template <typename T>
int B<T>::x = 0;

class D1: public B<D1>{};
class D2: public B<D2>{};

Ben
Aug 23 '05 #5

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

Similar topics

3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
5
by: Tom Pearson | last post by:
What is the scope of static variable when programming in ASP.NET? For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.