Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making numerous pointers NULL at the same time

Tags:

c++

pointers

This is a C++ class that I have made with n number of pointers.

class SomeClass
{
private:
        int* ptr1;
        int* ptr2;
        ...
        int* ptrn;

private:
        // constructors, destructors, and methods
};

During the initialization stage, I want to make all those pointers point to NULL (or make pointers point to NULL by default when they are declared) rather than doing so:

void SomeClass::MakePtrNull()
{
        ptr1 = NULL;
        ptr2 = NULL;
        ...
        ptrn = NULL;
}

Is there any easy method of accomplishing this goal? I'm just wondering if there are any ways of avoiding typing n lines of ptr = NULL; in my function. Thanks in advance.

ADDED based on the answers that I have received so far: Unfortunately, those pointers have to be separate, as they are used for different purposes. I made the names of the pointers as such just to make a point of what I'm trying to do, but each pointer has a completely different purpose. I guess I would have to make them point to NULL as I have done already. Thanks for your answers.

like image 827
stanigator Avatar asked Jun 19 '09 23:06

stanigator


4 Answers

Instead of int *, create a smart-pointer-like class which works exactly like a int *, but default-constructs with NULL:

template <typename T>
class MyPointer {
    T *p;

public:
    MyPointer() : p(NULL) { }
    MyPointer(T *o) : p(o) { }
    operator T*() const { return p; }
    // ...and the rest of the traditional smart-pointer operators
};

Then, use it in your class:

class SomeClass
{
private:
        MyPointer<int> ptr1;
        MyPointer<int> ptr2;
        ...
        MyPointer<int> ptrn;

private:
        // constructors, destructors, and methods
};

Every variable of the MyPointer<int> type will be automatically initialized correctly in SomeClass's constructors, without the need for any extra typing. If you did not forget or incorrectly implement any of MyPointer's methods, it will act exactly like a normal pointer, and have the exact same size and performance.

like image 165
CesarB Avatar answered Sep 30 '22 01:09

CesarB


Why don't you use an array or a vector rather than creating n individually named pointers? Then you can do the nulling in a short for loop.

like image 31
brian-brazil Avatar answered Sep 30 '22 03:09

brian-brazil


You can do this:

void SomeClass::MakePtrNull()
{
        ptr1 = ptr2 = ptr3 = ... = ptrn = NULL;
}
like image 33
Eclipse Avatar answered Sep 30 '22 01:09

Eclipse


First, the technique that does not work:

Calling memset to set the entire object to zero won't do. First, it'll cause a lot of trouble if your function has one or more virtual functions, and second, a null pointer is not guaranteed to be represented by a bit pattern of all zeros.

What I would probably do in your case is store the pointers in an array or a vector. Then you can use the std::fill function to set them all to NULL. (Or you could use a loop if you prefer)

Of course, if you need to do this often enough, it may be worth writing a wrapper class which behaves as the pointer, but which sets it to NULL in its default constructor.

Or you could use boost::optional which works essentially like this. (although it is not specific for pointers)

like image 43
jalf Avatar answered Sep 30 '22 02:09

jalf