473,378 Members | 1,489 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,378 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 1953
"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 -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.