# Why does \_\_null, 0, 0.0 work but (void \*)0,nullptr dont work when assignment overload is used

**URL:** <https://forum.opencv.org/t/why-does-null-0-0-0-work-but-void-0-nullptr-dont-work-when-assignment-overload-is-used/12368>\
**Category:** C++\
**Tags:** core, programming\
**Created:** [March 14, 2023, 12:35pm UTC](https://forum.opencv.org/t/why-does-null-0-0-0-work-but-void-0-nullptr-dont-work-when-assignment-overload-is-used/12368 "2023-03-14T12:35:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vijay\_shankar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/vijay_shankar/32/7391_2.png) [@vijay\_shankar](https://forum.opencv.org/u/vijay_shankar)\
**Post date:** [March 14, 2023, 12:35pm UTC](https://forum.opencv.org/t/why-does-null-0-0-0-work-but-void-0-nullptr-dont-work-when-assignment-overload-is-used/12368/1 "2023-03-14T12:35:08Z")

</div>

why does \_\_null, 0, 0.0 work but (void \*)0,nullptr dont work when assignment overload is used  
Take for example

```auto

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/types_c.h>
#include<iostream>
using namespace cv;

int main(int argc, char** argv) {
   Mat frame;
   frame = (void *)0;  
   Mat frame2;
   frame2 = nullptr;
   return 0;
   }

```

the above throws overload errors

```auto
/home/shft/build/test/hello.cpp:9:10: error: no viable overloaded '='
   frame = (void *)0;
   ~~~~~^~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1073:10: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const cv::Mat' for 1st argument
    Mat& operator = (const Mat& m);
         ^
/usr/local/include/opencv4/opencv2/core/mat.inl.hpp:3008:11: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const cv::MatExpr' for 1st argument
Mat& Mat::operator = (const MatExpr& e)
          ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1260:10: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const cv::Scalar' (aka 'const Scalar_<double>') for 1st argument
    Mat& operator = (const Scalar& s);
         ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:2101:10: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'cv::Mat' for 1st argument
    Mat& operator = (Mat&& m);
         ^
/home/shft/build/test/hello.cpp:11:11: error: no viable overloaded '='
   frame2 = nullptr;
   ~~~~~~^~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1073:10: note: candidate function not viable: no known conversion from 'std::nullptr_t' to 'const cv::Mat' for 1st argument
    Mat& operator = (const Mat& m);
         ^
/usr/local/include/opencv4/opencv2/core/mat.inl.hpp:3008:11: note: candidate function not viable: no known conversion from 'std::nullptr_t' to 'const cv::MatExpr' for 1st argument
Mat& Mat::operator = (const MatExpr& e)
          ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1260:10: note: candidate function not viable: no known conversion from 'std::nullptr_t' to 'const cv::Scalar' (aka 'const Scalar_<double>') for 1st argument
    Mat& operator = (const Scalar& s);
         ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:2101:10: note: candidate function not viable: no known conversion from 'std::nullptr_t' to 'cv::Mat' for 1st argument
    Mat& operator = (Mat&& m);

```

But \_\_null or any integer literal work (on my system NULL is defined as nullptr as a macro definition

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 14, 2023, 12:39pm UTC](https://forum.opencv.org/t/why-does-null-0-0-0-work-but-void-0-nullptr-dont-work-when-assignment-overload-is-used/12368/2 "2023-03-14T12:39:55Z")

</div>

if you just want an empty `Mat`, say `cv::Mat the_mat;` and that’s it. it’s an empty Mat with no size.

if you want a Mat of a certain size, initialized to all zero values, use `cv::Mat the_mat = cv::Mat::zeros(...)` with appropriate arguments.

* * *

you shouldn’t do any “assigning pointers”. Mats aren’t pointers. don’t assign pointers, or anything zero/null-like.

Mats are actual objects.

C++ has the concept of “RAII”, which basically means you don’t use pointers for objects (heap allocation) but you instantiate them on the stack, so they’re cleaned when the function ends.

Mat objects additionally have logic for reference counting, like `std::shared_ptr`.

They also have some “broadcasting” rules. [if you assign a number, it’s broadcast to all the matrix elements.](https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#aa5c947f7e449a4d856a4f3a87fcebd50) that works for anything that’s a number, but not for pointers, because pointers are pointers, not numbers.

please review a few C++ basics and perhaps some of the example code found in the OpenCV docs.
