static members and variables

G

Gary Wessle

Hi

I am calling a class method on many objects of that class
alternately. the class needs to make available "remember" values of
variables of said method for each object separetly when the object
calls the method again.

I can't make those variables static inside the method because it will
hold its value for all objects without discrimination.

I can't make those variables object attributes because will be erased
when the object goes out of scope at the closing bracket of the
alternating mechanize "for loop".

class mytype
{
static int unique;
public:
mytype():unique = 0 {}
void dothis(){
unique++; <<-- error line

....
};

undefined reference to `mytype::unique'

how is this fixed?

thanks
 
R

Rolf Magnus

Gary said:
Hi

I am calling a class method on many objects of that class
alternately. the class needs to make available "remember" values of
variables of said method for each object separetly when the object
calls the method again.

I can't make those variables static inside the method because it will
hold its value for all objects without discrimination.

I can't make those variables object attributes because will be erased
when the object goes out of scope at the closing bracket of the
alternating mechanize "for loop".

class mytype
{
static int unique;
public:
mytype():unique = 0 {}
void dothis(){
unique++; <<-- error line

...
};

undefined reference to `mytype::unique'

how is this fixed?

By defining 'unique'. As it stands above, the variable is only declared, but
there is no actual storage reserved for it. Add:

int mytype::unique;

somewhere in the implementation file.
 
A

amirkam1

Hi,
mytype():unique = 0 {}
This is a static variable u need to define this outside the class
(preferably in cpp file) as
int mytype::unique = 0;

Regards,
Amir Kamerkar
 
K

Kai-Uwe Bux

Gary said:
Hi

I am calling a class method on many objects of that class
alternately. the class needs to make available "remember" values of
variables of said method for each object separetly when the object
calls the method again.

I do not fully understand what you mean by "'remember' values".

I can't make those variables static inside the method because it will
hold its value for all objects without discrimination.

Ok, apparently, "remember" values could be different for each object.

I can't make those variables object attributes because will be erased
when the object goes out of scope at the closing bracket of the
alternating mechanize "for loop".

There appears to be a confusion: when the object goes out of scope, it is
destroyed. Thus, the object cannot "call the method again" (your words from
the description of your design requirement). Only a different object can
call the method now. Therefore, "remember" values (whatever they might be)
should not be required.

class mytype
{
static int unique;
public:
mytype():unique = 0 {}
void dothis(){
unique++; <<-- error line

...
};

undefined reference to `mytype::unique'

how is this fixed?

To help you, one would need to know what problem you are trying to solve.
Post a minimal complete program that illustrates the difficulty.


Best

Kai-Uwe Bux
 
G

Gary Wessle

Rolf Magnus said:
By defining 'unique'. As it stands above, the variable is only declared, but
there is no actual storage reserved for it. Add:

int mytype::unique;

somewhere in the implementation file.

sure that will fix the error above but still will not satisfy the
requirement of what I am trying to do, which is keep values in
variables between method calls for the same object and not for all
objects.
 
R

Rolf Magnus

Gary said:
sure that will fix the error above but still will not satisfy the
requirement of what I am trying to do, which is keep values in
variables between method calls for the same object and not for all
objects.

As Kai-Uwe wrote, your requirements don't really make sense. If you want to
remember a value for each object, just make it a non-static member. If the
object goes out of scope, you don't need to remember the value anymore,
because the object is gone, too.
 
S

Siam

Gary said:
sure that will fix the error above but still will not satisfy the
requirement of what I am trying to do, which is keep values in
variables between method calls for the same object and not for all
objects.

I can't see why you don't simply use non-static member variables?
Surely thats what they're used for...to hold data for a specific
object. Yes, the variable will go out of scope when the object is
destroyed, but in that case you won't be able to call any methods on
the object anyway..

Siam
 
G

Gary Wessle

Kai-Uwe Bux said:
I do not fully understand what you mean by "'remember' values".



Ok, apparently, "remember" values could be different for each object.



There appears to be a confusion: when the object goes out of scope, it is
destroyed. Thus, the object cannot "call the method again" (your words from
the description of your design requirement). Only a different object can
call the method now. Therefore, "remember" values (whatever they might be)
should not be required.



To help you, one would need to know what problem you are trying to solve.
Post a minimal complete program that illustrates the difficulty.


Best

Kai-Uwe Bux


#include <string>
#include <vector>

using namespace std;



class acc_holder
{
string name;
double max_weekly_withdraw;
double daily_withdraw;

public:
acc_holder(string nam, double d_withdraw)
: name(nam),
daily_withdraw(d_withdraw)
{
update_weekly_figurs(double d_withdraw);
}


void update_weekly_figurs(double i)
{
max_weekly_withdraw += i;
}
};



int main(){


acc_holder jack("jack", 20);
acc_holder mary("mary", 24);
vector<acc_holder> fund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);

// no acc_holder is permited to do 2 or more consecutive transactions
// there for they must alternate, each is permited 10 transactions.

for( unsigned i = 0; i<=9; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j].update_weekly_figurs(fund_participanets[j].daily_withdraw);
}
}
}
 
K

Kai-Uwe Bux

Gary said:
Kai-Uwe Bux said:
I do not fully understand what you mean by "'remember' values".



Ok, apparently, "remember" values could be different for each object.



There appears to be a confusion: when the object goes out of scope, it is
destroyed. Thus, the object cannot "call the method again" (your words
from the description of your design requirement). Only a different object
can call the method now. Therefore, "remember" values (whatever they
might be) should not be required.



To help you, one would need to know what problem you are trying to solve.
Post a minimal complete program that illustrates the difficulty.


Best

Kai-Uwe Bux


#include <string>
#include <vector>

using namespace std;



class acc_holder
{
string name;
double max_weekly_withdraw;
double daily_withdraw;

public:
acc_holder(string nam, double d_withdraw)
: name(nam),
daily_withdraw(d_withdraw)
{
update_weekly_figurs(double d_withdraw);
}


void update_weekly_figurs(double i)
{
max_weekly_withdraw += i;
}
};



int main(){


acc_holder jack("jack", 20);
acc_holder mary("mary", 24);
vector<acc_holder> fund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);

// no acc_holder is permited to do 2 or more consecutive transactions
// there for they must alternate, each is permited 10 transactions.

for( unsigned i = 0; i<=9; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j].update_weekly_figurs(fund_participanets[j].daily_withdraw);
}
}
}

a) This contains a few errors that prevent it from compiling. Not a big deal
in this case, but a nuisance.

b) The code does not illustrate any problem with variables going out of
scope: your acc_holders jack and mary have been copied into a vector
(fund_participants) and live there happily through all iterations of the
nested for loops. You can just use a non-static member variable to keep
track of anything that the acc_holders need to remember about their last
transaction.

c) Should you be pondering how the class acc_holder can ensure the
requirement that a member function cannot be called twice by the same
object without another object calling it before, you may consider:

#include <iostream>
#include <cassert>

struct dummy {

static
dummy* & last_object ( void ) {
static dummy* ptr = 0;
return ( ptr );
}

void member_func ( void ) {
assert( this != last_object() );
last_object() = this;
std::cout << "member_func called\n";
}

};

int main ( void ) {
dummy a;
dummy b;
a.member_func();
b.member_func();
a.member_func();
a.member_func(); // this one fails.
}

Probably you want to throw an exception instead of using assert().

d) It is not entirely clear why you are using value semantics for
acc_holders. I would expect that there is one and only one jack and that
persons cannot be copied. Thus, I would expect to see a
shared_ptr<acc_holder> in those places where you have acc_holder. But that
is an entirely different design issue alltogether.


Best

Kai-Uwe Bux
 
J

Jim Langston

Gary Wessle said:
Kai-Uwe Bux said:
I do not fully understand what you mean by "'remember' values".



Ok, apparently, "remember" values could be different for each object.



There appears to be a confusion: when the object goes out of scope, it is
destroyed. Thus, the object cannot "call the method again" (your words
from
the description of your design requirement). Only a different object can
call the method now. Therefore, "remember" values (whatever they might
be)
should not be required.



To help you, one would need to know what problem you are trying to solve.
Post a minimal complete program that illustrates the difficulty.


Best

Kai-Uwe Bux


#include <string>
#include <vector>

using namespace std;



class acc_holder
{
string name;
double max_weekly_withdraw;
double daily_withdraw;

public:
acc_holder(string nam, double d_withdraw)
: name(nam),
daily_withdraw(d_withdraw)
{
update_weekly_figurs(double d_withdraw);
}


void update_weekly_figurs(double i)
{
max_weekly_withdraw += i;
}
};



int main(){


acc_holder jack("jack", 20);
acc_holder mary("mary", 24);
vector<acc_holder> fund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);

// no acc_holder is permited to do 2 or more consecutive transactions
// there for they must alternate, each is permited 10 transactions.

for( unsigned i = 0; i<=9; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j].update_weekly_figurs(fund_participanets[j].daily_withdraw);
}
}
}

Just store in your object the last transaction they made.
Store in a static the last object that made a transaction. I.E. std::string
name.
If the current object making the transaction isn't the same as the last one
(compare the static stored name with the object's name) then you can look at
the object's last transaction.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top