V
Verbal Kint
hi.
could you please have a look to the following code:
class cClassA
{
public:
cClassA(void):itsX(0)
{}
void SetX (int val)
{itsX = val;}
int GetX ()
{return itsX;}
private:
int itsX;
};
class cClassB
{
public:
void AddDat (cClassA dat)
{itsDat.push_back(dat);}
//*/
// alternative 1:
int GetDat (int nr)
{
if (nr < (int)itsDat.size())
return itsDat[nr].GetX();
cout << "cClassB::GetDat\tPROBLEM" << endl;
return -1;
}
void SetDat (int nr, int val)
{
if (nr < (int)itsDat.size()) itsDat[nr].SetX(val);
cout << "cClassB::SetDat\tPROBLEM" << endl;
}
// alternative 2:
cClassA* ModDat (int nr)
{
if (nr < (int)itsDat.size())
return &itsDat[nr];
cout << "cClassB::ModDat\tPROBLEM" << endl;
}
//*/
private:
vector<cClassA> itsDat;
};
int main()
{
int i;
cClassA x;
cClassB data;
for (i=0;i<5;i++)
{
data.AddDat(x);
}
return 0;
}
my question is the following: which is the best way to get access to
the methods of cClassA from cClassB? i have two alternatives here, but
the problems are:
1. alternative: if there are a lot more variables, hence a lot more
methods, i have to write almost the same code in cClassB.
2. alternative: after returning the pointer, e.g. to the main function,
i don't know how to continue my code so that the data in itsDat will be
modified permamently.
do you have any idea, how to improve this code?
THANKS for your help!
V.K.
could you please have a look to the following code:
class cClassA
{
public:
cClassA(void):itsX(0)
{}
void SetX (int val)
{itsX = val;}
int GetX ()
{return itsX;}
private:
int itsX;
};
class cClassB
{
public:
void AddDat (cClassA dat)
{itsDat.push_back(dat);}
//*/
// alternative 1:
int GetDat (int nr)
{
if (nr < (int)itsDat.size())
return itsDat[nr].GetX();
cout << "cClassB::GetDat\tPROBLEM" << endl;
return -1;
}
void SetDat (int nr, int val)
{
if (nr < (int)itsDat.size()) itsDat[nr].SetX(val);
cout << "cClassB::SetDat\tPROBLEM" << endl;
}
// alternative 2:
cClassA* ModDat (int nr)
{
if (nr < (int)itsDat.size())
return &itsDat[nr];
cout << "cClassB::ModDat\tPROBLEM" << endl;
}
//*/
private:
vector<cClassA> itsDat;
};
int main()
{
int i;
cClassA x;
cClassB data;
for (i=0;i<5;i++)
{
data.AddDat(x);
}
return 0;
}
my question is the following: which is the best way to get access to
the methods of cClassA from cClassB? i have two alternatives here, but
the problems are:
1. alternative: if there are a lot more variables, hence a lot more
methods, i have to write almost the same code in cClassB.
2. alternative: after returning the pointer, e.g. to the main function,
i don't know how to continue my code so that the data in itsDat will be
modified permamently.
do you have any idea, how to improve this code?
THANKS for your help!
V.K.