# Visual C++ & C++ Programming > C++ and WinAPI >  how pass a class pointer to a object class?

## Cambalinho

i have a class image, with Pixel structure:


```
class image
{
public:
      struct Pixel    {
        image *img;
        float X = 0;
        float Y = 0;
        float Z = 0;
        COLORREF color = 0;


    };

   image()
{
      Pixel *pix;
      pix->img=this;
     pix->color = LineColor;
            pix->X =X;
            pix->Y=Y;
            pix->Z=Z;
}
};
```

i can get the 'X' and 'Y' and 'Z' correctly... aren't pointers.
but 'pix->img=this;' seems not correct. because i don't get results.
the 'img' must recive the  class instance for i use some image members.
so what i'm doing wrong? is these line correct or i miss something on pointers?
when exit the program: "Process returned -1073741819 (0xC0000005)   execution time : 0.812 sPress any key to continue."

----------


## VictorN

> i have a class image, with Pixel structure:
> 
> 
> ```
> class image
> {
> public:
>       struct Pixel    {
>         image *img;
> ...


This code snippet looks like *trash*!. 
and it won't compile.  :Frown:

----------


## wolle

> is these line correct or i miss something on pointers?


The immediate problem is that you declare a pointer variable in the constructor but never assign a pointer to it,



```
      Pixel *pix;
```

And even if you did, the pointer will get lost when the constructor finishes.

----------


## 2kaud

What are you trying to achieve?

----------


## Cambalinho

_2kaud_  getting the 

image instance pointer correctly  for  the multithread  :Frown:

----------


## 2kaud

What multi-thread - this isn't mentioned in the original post??

----------


## 2kaud

[I think the OP is referring to this thread https://cplusplus.com/forum/general/285034/ ]

----------


## Cambalinho

2kaud: yes  i'm... my problem was more than we think  :Frown: 
- the pointer seems not corrected. so no results  :Frown: 
so i did a simple complete test and worked. what i was trying to do was adding multithreading on my image class... but i wasn't getting results but errors  :Frown: 
so i added my image class on my simple multithread test code and works.


```
#include <iostream>#include <thread>
#include <windows.h>
#include <math.h>


using namespace std;
class image
{
public:
    int ImageWidth = 0;
    int ImageHeight = 0;
    HDC ImageHDC = NULL;
    HBITMAP ImageBitmap;
    HBITMAP oldBit;
    BITMAP bmp;
    BITMAPINFO info;
    size_t pixelSize;
    size_t scanlineSize;
    size_t bitmapSize;
    void* p;
    LPBYTE Pixels;




    void Clear(COLORREF BackColor = RGB(0,0,0))
    {
        RECT rec{0,0,ImageWidth,ImageHeight};
        HBRUSH HB = CreateSolidBrush(BackColor);
        FillRect(ImageHDC,&rec,HB);
        DeleteObject(HB);
    }


    image(int Width, int Height, COLORREF BackColor=RGB(0,0,0))
    {
        ImageHDC = CreateCompatibleDC(NULL);
        ImageWidth = Width;
        ImageHeight =Height;


        ZeroMemory (&info, sizeof (BITMAPINFO));
        info.bmiHeader.biSize = sizeof(info.bmiHeader);
        info.bmiHeader.biWidth = ImageWidth;
        // pay attention to the sign, you most likely want a
        // top-down pixel array as it's easier to use
        info.bmiHeader.biHeight = -ImageHeight;
        info.bmiHeader.biPlanes = 1;
        info.bmiHeader.biBitCount = 32;
        info.bmiHeader.biCompression = BI_RGB;
        info.bmiHeader.biSizeImage = 0;
        info.bmiHeader.biXPelsPerMeter = 0;
        info.bmiHeader.biYPelsPerMeter = 0;
        info.bmiHeader.biClrUsed = 0;
        info.bmiHeader.biClrImportant = 0;


        // the following calculations work for 16/24/32 bits bitmaps
        // but assume a byte pixel array




        ImageBitmap = CreateDIBSection(ImageHDC, &info, DIB_RGB_COLORS, (LPVOID*)&Pixels, 0, 0);
        if(ImageBitmap ==NULL) cout << "no HBITMAP";
        if(SelectObject(ImageHDC, ImageBitmap)==NULL) cout << "error";
        pixelSize = info.bmiHeader.biBitCount / 8;
        // the + 3 ) & ~3 part is there to ensure that each
        // scan line is 4 byte aligned
        scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
        bitmapSize = bmp.bmHeight * scanlineSize;
        Clear(BackColor);
    }






    void NewSetPixel(HDC DestinationHDC, int X, int Y, BYTE RedColor, BYTE GreenColor, BYTE BlueColor)
    {
        size_t pixelOffset = Y *scanlineSize + X *pixelSize;
        Pixels[pixelOffset+2]=RedColor;
        Pixels[pixelOffset+1]=GreenColor;
        Pixels[pixelOffset+0]=BlueColor;
    }


    void DrawLine( float X0, float Y0, float Z0, float X1, float Y1, float Z1, COLORREF LineColor)
    {
        //Getting Line Distance(float results):
        float DX = abs(X1 - X0);
        float DY = abs(Y1 - Y0);
        float DZ = abs(Z1 - Z0);
        float LineDistance =sqrt((DX * DX) + (DY * DY) + (DZ * DZ));




        //Getting the Steps incrementation(float results):
        float XSteps = DX/LineDistance;
        float YSteps = DY/LineDistance;
        float ZSteps = DZ/LineDistance;


        //Draw Line using the Steps\ Incrementation:
        float X = X0;
        float Y = Y0;
        float Z = Z0;
        BYTE R = GetRValue(LineColor);
        BYTE G = GetGValue(LineColor);
        BYTE B = GetBValue(LineColor);
        std::thread th[(int)LineDistance+1];
        for(int i =0; i <LineDistance; i++)
        {
            //For every steps we calculate the perspective:
            float EyeDistance = 500;
            //Avoiding division by zero:
            if(Z==0) Z=1;
            float Perspective = EyeDistance/(EyeDistance+Z);


            //The 3D to 2D convertion(i use 300 of eye distance, but we can change it):


            int PosX = trunc(X*Perspective);
            int PosY = trunc(Y*Perspective);
            if(Z>=0 && PosX<ImageWidth && PosX>=0 && PosY<ImageHeight && PosY>=0)
            {
                th[i] = std::thread(&image::NewSetPixel,this,ImageHDC, PosX,PosY,R,G,B);//erros
                th[i].join();
                //NewSetPixel(ImageHDC, PosX,PosY,R,G,B);
            }


            //Increment steps(integer results):
            X+=XSteps;
            Y+=YSteps;
            Z+=ZSteps;
        }
    }


    void DrawRectangle(float PosX, float PosY, float PosZ, float Width, float Height, float Depth, COLORREF Color = RGB(255,0,0), bool Filled = false)
    {
        DrawLine( PosX, PosY, PosZ,PosX + Width, PosY, PosZ + Depth, Color);
        DrawLine( PosX, PosY, PosZ, PosX, PosY + Height, PosZ, Color);
        DrawLine( PosX + Width, PosY, PosZ + Depth, PosX + Width, PosY+Height, PosZ + Depth, Color);
        DrawLine( PosX, PosY + Height, PosZ, PosX + Width, PosY + Height, PosZ + Depth, Color);
        if(Filled==true)
        {
            for(int i = 0; i<Height; i++)
                DrawLine( PosX, PosY + i, PosZ,PosX + Width, PosY +i, PosZ + Depth, Color);


        }
    }


    ~image()
    {
        SelectObject(ImageHDC, oldBit);
        DeleteObject(ImageBitmap);
        DeleteDC(ImageHDC);
    }


};


image img(200,200);


int main()
{
    img.DrawRectangle(0,100,0, 100,100,500, RGB(255,0,0),true);
    BitBlt(GetWindowDC(GetConsoleWindow()),10,100,img.ImageWidth,img.ImageHeight,img.ImageHDC,0,0,SRCCOPY);


    return 0;
}
```

my big question is: why the multithread seems much more slow than don't use it? did i miss something?

----------

