Gary Wessle wrote:
Kai-Uwe Bux <jk********@gmx.netwrites:
>Gary Wessle wrote:
>>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
#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_holderfund_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_holderin those places where you have acc_holder. But that
is an entirely different design issue alltogether.
Best
Kai-Uwe Bux