1
Good night to you all!
I’m studying C++
, and gave error in the code I tried to compile. I believe it is because the example is a very old book. I’ve tried some kicks, but I haven’t been able to solve the problem yet.
Thanks in advance!
//
// main.cpp
// membros_static_1
//
//
#include <iostream>
using namespace std;
class Rec{
private:
static int n;
public:
Rec(){n++;} //Construtor
int getRec() const{return n;}
};
int main(int argc, const char * argv[]) {
// insert code here...
Rec r1, r2, r3;
cout << "\nNumero de objetos: " << r1.getRec();
cout << "\nNumero de objetos: " << r2.getRec();
cout << "\nNumero de objetos: " << r3.getRec();
return 0;
}
Thanks in advance for your attention.
alternative is not to declare
n
as a statistical variable, then initialize it asint n=0
.– Anna Maule
@Annamaule yes, I could, but with the
static
da to count how many objects were created :)– Codigo de Senior