0

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.

  • 1
    Where is any `buf()` in your code? – Daniel Langr Feb 28 '23 at 13:52
  • remove `using namespace std;` and you'll [find a lot less problems](https://stackoverflow.com/a/1452738/332733) – Mgetz Feb 28 '23 at 13:53
  • buf() is in main() where MyBuffer buf(numsToStore); – Chanhyuk Park Feb 28 '23 at 13:53
  • 4
    You are declaring an object of type `MyBuffer` named `buf` and initialized with `numsToStore`. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Feb 28 '23 at 13:56
  • Thank you so much, Mr.Oliver. I was struggling with this for a very long time. – Chanhyuk Park Feb 28 '23 at 13:59
  • 3
    BTW, the source from which you got this code is not good. `new int[length]` really does not belong in 2023, but if you'd write that, at least get the rest of the class correct. This class has several memory leaks. `std::vector` can do everything this class does and more, and it's free of memory leaks. – MSalters Feb 28 '23 at 14:08
  • https://en.cppreference.com/w/cpp/language/constructor – 463035818_is_not_an_ai Feb 28 '23 at 14:27
  • very important that you read this https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172724#4172724 – 463035818_is_not_an_ai Feb 28 '23 at 14:28
  • `int main() { MyBuffer buf(100); MyBuffer buf2 = buf; }` -- This shows that the code is broken. That two-line program has a double-delete error at the end of `main`. As mentioned, use `std::vector`, and not `MyBuffer`. – PaulMcKenzie Feb 28 '23 at 14:49

1 Answers1

1

A class is not an object, it is a type of an object.

class MyBuffer {...}; //define a *type*
MyBuffer buf; //make an object of type `MyBuffer`, and name it `buf`.

class Person {...}; //define a *type*
Person Chanhyuk_Park; //make an object of type `Person` and name it `Chanhyuk_Park`.

However, to make an object, you may need to pass parameters to its constructor:

MyBuffer buf(numsToStore); //make an object of type `MyBuffer`, and name it `buf`.
//and pass the value `numsToStore` to its constructor
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158