CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    1,343

    how pass a class pointer to a object class?

    i have a class image, with Pixel structure:
    Code:
    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."

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,301

    Re: how pass a class pointer to a object class?

    Quote Originally Posted by Cambalinho View Post
    i have a class image, with Pixel structure:
    Code:
    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;
    }
    };
    This code snippet looks like trash!.
    and it won't compile.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2017
    Posts
    674

    Re: how pass a class pointer to a object class?

    Quote Originally Posted by Cambalinho View Post
    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,

    Code:
          Pixel *pix;
    And even if you did, the pointer will get lost when the constructor finishes.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,692

    Re: how pass a class pointer to a object class?

    What are you trying to achieve?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.4.1)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,343

    Re: how pass a class pointer to a object class?

    2kaud getting the

    image instance pointer correctly for the multithread

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,692

    Re: how pass a class pointer to a object class?

    What multi-thread - this isn't mentioned in the original post??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.4.1)

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,692

    Re: how pass a class pointer to a object class?

    [I think the OP is referring to this thread https://cplusplus.com/forum/general/285034/ ]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.4.1)

  8. #8
    Join Date
    Apr 2009
    Posts
    1,343

    Re: how pass a class pointer to a object class?

    2kaud: yes i'm... my problem was more than we think
    - the pointer seems not corrected. so no results
    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
    so i added my image class on my simple multithread test code and works.
    Code:
    #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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured