Reputation: 648
I need to count the times my public member function creates new data in a loop. Each time data is read it is saved into a private member varaible. Nothing is overwritten.
// my classType.h
const int ImpliedIndex = 1000;
class classType
{
private:
char privateMember[ImpliedIndex];
char privateMember2[ImpliedIndex];
public:
static void myMemberCounter;
void printMyInformation():
void mySupplier(const char[] );
...
};
//classType.cpp
int globalCounter = 0;
void classType::printMyInformation()
{
...
cout << privateMember << endl;
cout << globalCounter++ << endl;
}
void classType::mySupplier( const char buff[] )
{
strcpy( privateMember, buff );
}
// The main should clear things up a bit.
// main.cpp
int main()
{
while ( !inFile.eof() )
{
for ( int x = 0; x < 20; x++ )
{
// do file I/O here
// save each line of file into an temp array
// supply temp array with a routine defined in myClass
// re-use temp array until we run out of file
}
}
//close file
I need to change my 20 to a variable. Notice that in classType I used globalCounter to retrieve the number of types.
What I wanted to do is, globalCounter = memberCounter;
But I have to re-declare both of these varaibles, and I can't use myClass[ImpliedIndex].memberCounter
with the assignment operator (=) or the binary insertion operator(<<).
Upvotes: 0
Views: 741
Reputation: 399763
This line:
static void myMemberCounter;
Should generate a compile-time error. It looks like a variable, but has type void
, which doesn't make sense.
If you want a static ("class variable") counter, you should use a type like int
. Then it would be easy for the class to use that counter to count the number of instances, by just incrementing the counter in the constructor. The count can be read by the outside world by adding a public
class function to return it.
Upvotes: 1