By making multiple NULL pointers at the same time

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

class SomeClass { private: int* ptr1; int* ptr2; ... int* ptrn; private: // constructors, destructors, and methods }; 

At the initialization stage, I want all of these pointers to point to NULL (or pointers to point to NULL by default when they are declared), and not do this:

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

Is there an easy way to achieve this? I'm just wondering if there are ways to avoid entering n lines ptr = NULL; in my function. Thanks in advance.

ADDED based on the answers I have received so far: Unfortunately, these pointers must be separate, as they are used for different purposes. I made pointer names as such to indicate exactly what I'm trying to do, but each pointer has a completely different purpose. I think I would have to point out that they point to NULL, as I already did. Thank you for your responses.

+6
c ++ pointers
source share
8 answers

Why not use an array or vector, rather than create n individual pointers? Then you can do a zeroing in a short cycle.

+6
source share

Instead of int * create a class with a smart pointer that works exactly the same as int * , but by default 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 }; 

Each variable of type MyPointer<int> will be automatically initialized correctly in SomeClass constructors without the need for additional SomeClass . If you have not forgotten or incorrectly implemented any of the MyPointer methods, it will act just like a regular pointer, and has the same size and performance.

+11
source share

You can do it:

 void SomeClass::MakePtrNull() { ptr1 = ptr2 = ptr3 = ... = ptrn = NULL; } 
+5
source share

Firstly, the method does not work :

Calling memset to set the entire object to zero will not. Firstly, it will cause a lot of problems if your function has one or more virtual functions, and secondly, a null pointer cannot be represented by a bit diagram of all zeros.

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

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

Or you can use boost :: optional, which works essentially like this. (although this does not apply to pointers)

+3
source share

I recommend that you do the following if you need to specify pointers separately (perhaps the most urgent need would be that pointers can have different types)

 class SomeClass { struct Pointers { int* ptr1; int* ptr2; float* ptrn; } ptrs; public: void MakePtrNull(); }; void SomeClass::MakePtrNull() { // clears all pointers at once ptrs = Pointers(); } 

This works because initializing values ​​for classes that do not have a constructor declared by the user will cost to initialize all its members. Initializing a pointer value will create a null pointer.

+3
source share

Why not use the default constructor:

 SomeClass::SomeClass() : ptr1(NULL), ptr2(NULL), ... { } 

You can also do:

 ptr1 = ptr2 = ptr3 = NULL; 
+2
source share

You can put pointers in a structure, and then a memset () structure when necessary. Pointers are still separate, but you have the means to target them as a whole, without affecting the rest of your class. For example:

 struct MyPointers { int* ptr1; int* ptr2; ... int* ptrn; }; class SomeClass { private: MyPointers ptrs; ... }; void SomeClass::MakePtrNull() { memset(&ptrs, 0, sizeof(ptrs)); } 
+1
source share

Use a vector for this (because of the sounds you won’t need to ever edit the list in the future - and you need random access). You can do it as follows:

 class PointerList { private: typedef std::vector<int*> IntPtrVector; IntPtrVector m_ptrVec; public: PointerList() { m_ptrVec.reserve(5); for (int i = 0; i < 5; i++) m_ptrVec.push_back(NULL); } int* getPointer(int index) { return m_ptrVec[index]; } void setPointer(int index, int* ptr) { assert(index <= 4); m_ptrVec[index] = ptr; } }; 

EDIT

Although honestly, it smells of ghetto. Are you sure your problem requires this solution? If you dwell on your specific problem in more detail, perhaps in another question, I am sure that you can get a better answer on how to accomplish what you want more elegantly, rather than creating a second problem.

0
source share

All Articles