Why is this code using buf() when it passes a variable name to MyBuffer constructor? What does buf() do exactly?
#include <iostream>
using namespace std;
class MyBuffer {
private:
int* myNums;
public:
explicit MyBuffer(unsigned int length) {
cout << "Constructor allocates " << length << " integers" << endl;
myNums = new int[length];
}
~MyBuffer() {
cout << "Destructor releasing allocated memory"<< endl;
delete[] myNums;
}
};
int main() {
cout << "How many integers would you like to store?" << endl;
unsigned int numsToStore = 0;
cin >> numsToStore;
MyBuffer buf(numsToStore);
return 0;
}
I did some research on this and I found out that MyBuffer(numsToStore); actually makes compiler to not recognize numsToStore as a parameter to MyBuffer but as a separate variable that is similar to this: MyBuffer numsToStore;. And one way to solve this is to add buf(), but I do not know what exactly does buf() mean.