0

i want pass pointer to bitset to function argument,i'm declaring bitset on heap memory:

std::bitset<size> *ptr;
ptr = new std::bitset<size>();
void function('here i want pass the pointer '){

}
beaugoose
  • 1
  • 2
  • 4
    Give the function a parameter like `std::bitset *ptr`? – NathanOliver Jul 05 '21 at 02:26
  • Does this answer your question? [Pointers vs. values in parameters and return values](https://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values) – prehistoricpenguin Jul 05 '21 at 03:02
  • 1
    Others have already answered how to do this to pass by pointer, but... Do you really need a `bitset` on the heap? Usually `new`ing up containers like this is a bit of a design smell in C++; the stack is often better because it has automatic storage duration -- at which point you can pass the `bitset` by reference. At the least, if dynamic lifetime is needed, you should be using `unique_ptr` – Human-Compiler Jul 05 '21 at 04:48

1 Answers1

3

Pretty directly, just receive the right type:

#include <bitset>

const int size = 8;

void function(std::bitset<size>* ptr)
{

}


int main()
{
    std::bitset<size> *ptr;
    ptr = new std::bitset<size>();

    function(ptr);
}

https://godbolt.org/z/e74MaMMe7

Jeffrey
  • 11,063
  • 1
  • 21
  • 42