I am not sure what i am even asking exists, but just thought i ask. Anyway, i have to create 3 objects of different types and i am suppose to count the objects of each type i have created.
E.g. int abc[10], string s, double dbl[]. So memory will be allocated to these objects right. So without knowing or looking at the above statement that i have executed, how do i count how many elements were created? Does our memory work by via queue or stack or something along these lines, that i count the number of objects created for each type?
Not sure if i make sense, but i am kind of also just shooting arrows in the dark. So anyone who can enlighten will be fantastic!
If you would like to count classes you could modify the constructor to increment a static variable. So you can see how many variables of a specific type have been created.
To do this with integral types you could call a specific function (one for each integral type) that also counts the number of variables created. But you have to call the function yourself.
Otherwise this macro might help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int int_count = 0 ;
typedefint SaveInt ;
#define int int_count++; SaveInt
std::cout << int_count << "#"; // writes 0
int a = 5 ;
std::cout << int_count << "#"; // writes 1
int b ;
std::cout << int_count << "#"; // writes 2
int c[] = {1,2,3} ;
std::cout << int_count << std::endl; // writes 3
//reset the macro
#undef int
return 0 ;
But beware that this will break some code!
1 2 3
std::vector<int> d ;
//error since the macro expands it to:
//std::vector<int_count++ ; SaveInt> d ;
So for example, i could do this in my class if i take that each creation of an object is standard by 5. Is it done like this? I didn't quite understand your macro coding and how it can help to differentiate between the type of classes object created.
You should initialize your static variable somewhere!
Why don't you simple try the code you've written ;-)?
Implement something to fetch the value of abcCnt or move it - for testing purposes - to the public section. So you can check if the code works.
So i am now told by my lecturer that i can't modify abc.cpp and test.cpp. I can only modify abc.h. Actually, what i am trying to do is to use a method called getCount() in abc.h to find out how many Class abc have i created in the for loop as shown in test.cpp assuming that i can't see the for-loop.
So i guess i should create a global variable say countValue. But i wont be able to tell if i have created a class abc or efg if i just increment countValue. So that's why i am wondering if there is anyway i can find out the type of the objects i have created.
The annoying thing is i can't touch abc.cpp and test.cpp. So i am sort of lost in a sense, i also don't know where i could increment the countValue considering the constraints.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//abc.h
int countValue=0;
int getCount(){
return countValue;
}
Class abc{
public: abc(){};
};
Class efg{
public: efg(){};
};
Is this allowed?
You have a seperate count for each class.
If the extra function is not allowed, move the count to the public section and simply read it using abc::count
//abc.h
int countValue=0;
int getCount(){
return countValue;
}
Class abc{
public: abc(){++count ;};
int getCount(){return count ;}; //inline so no change to abc.cpp!
private:
staticint count = 0 ; // static initialization inside class allowed for integral types
};
Class efg{
public: efg(){++count ;};
int getCount(){return count ;}; //inline so no change to abc.cpp!
private:
staticint count = 0 ; // static initialization inside class allowed for integral
};
I really wish that was allowed because that will save me a whole lot of trouble!
I tried adding public: abc(){++count ;}; in my abc.h file but it will give me an error because of "Redefinition of abc::abc". So i can't compile.
Is it possible to have my original getCount() while having a getCount() for all the other classes? So i will use getCountABC() and getCountEFG() in getCount(). Example:
//abc.h
int countValue=0;
int getCount(){
return countValue;
}
Class abc{
public: abc(){++count ;};int getCountABC(){return count ;}; //inline so no change to abc.cpp!
private:
staticint count = 0 ; // static initialization inside class allowed for integral types
};
Class efg{
public: efg(){++count ;};
int getCountEFG(){return count ;}; //inline so no change to abc.cpp!
private:
staticint count = 0 ; // static initialization inside class allowed for integral
};
1) You don't need getCountABC, since you have to call it this way: abc::getCountABC() so you can't mix it up with getCount() anyway!
2) If you can't influence the constructor then - without using macros or messing with operator new - it is impossible to count the number of created instances.
I'd like to see the solution of your instructor though if he has any ;-).
I tried adding public: abc(){++count ;}; in my abc.h file but it will give me an error because of "Redefinition of abc::abc". So i can't compile.
Then you already have the constructor defined in the cpp-file.
One last trick I can think of: Use the destructor!
So instead of putting ++count ; in the constructor, place it in the destructor!
1 2 3 4 5 6 7 8 9 10 11 12
//abc.h
Class abc{
private:
staticint count;
public: abc();
~abc(){
++count ;
// maybe print them right here, so that you see them even if your
// program exits...
// std::cout << "there were " << count << " occurences of class abc\n" ;
};
};
The drawback is that you only get the count after the variables are released...
I am waiting to see his solution too. If i use the destructor, i won't get it till it the variable is released and i wont be able to determine when that happens right? Or does it happen right after int main() ends?
I looked through your macro, but i didn't understand how i am supposed to use it or understand for example typedefint SaveInt ;, etc. Could you briefly explain how it works please?
It looks confusing as to who i should apply it to my case. Is it possible to use the macro in getCount(), because in the test.cpp class, it calls getCount() method and as you know i can't amend test.cpp.
You can't modify test.cpp and line 6 should not have ()'s. The compiler should interpret that line has a function declaration that takes no arguments and returns an abc.
Don't use the macro. It will break your code in subtle ways.
Macros replaces source code (Like "search and replace" in your editor). The macro #define abc ++abcCount; abc
modifies the code abc v ; // no () since this will declare a function (see seymores post)
to
++abcCount; abc v ;
But since the macro will be used recursively this will never end.
I used the destructor as mentioned previously and it seems to work. However can i ask, why it seems to miss the creation if i did abc abc at line 7 in my test.cpp file with int main(). So by line 8: f(2), i should have countA=4, but it gets countA=3. Why is that so?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//abc.h
int countA=0;
int count() {
return countA;
}
class abc {
public:
abc ();
~abc (){
++countA ;
}
};
//test.cpp
void f(int d){
abc abc;
if(d) f(d-1);
}
int main() {
{
abc x;
} // a block will cause the x to be destroyed.
//alternatively:
abc* y = new abc();
delete y ; // delete also invokes the destructor!
f(2);
cout << count() << endl;
f(1);
cout << count() << endl;
f(0);
cout << count() << endl;
system("pause");
return 0;
}
Did you also mean that #define abc ++abcCount ; abcSafe will translate to ++abcCount ; abcSafe v ;?
It will translate abc v; to ++abcCount ; abcSafe v ;?
I am only able to change abc.h and no other files. So i can't make the changes like those you made in test.cpp. I am just wondering i mean the other variables can be counted when i call f(), so why not abc x? After all, it will invoke the destructor too right?
The difference why some variables can be counted and some cannot be counted is when they are freed. If you declare them in main, they are only freed when main exits, i.e. the end of the program. If you declare them in a function they are local variables and freed when the function returns.
If you could enclose the variables in a block they would be freed when the block ends.
The macro replaces abc v;
to ++abcCount ; abcSafe v;
.
I see ... So that means if i call abc x in a for-loop for example, so when the for-loop ends the variable will be freed right? Whereas for the abc x called in the main(), it will not be free till main() ends. Am i right?
Thanks for the insight! I guess i will have to cheat a bit by starting countA=1 instead countA=0. Then my count will be fine, with the constraints, i guess this is the best alternative. Thanks again!