Why does the following code skip the initialization of values of the temporary created?

Box::Box() : X1(0), Y1(0), X2(0), Y2(0), W((X2 < 0 ? -X2 : X2) - (X1 < 0 ? -X1 : X1)), H((Y2 < 0 ? -Y2 : Y2) - (Y1 < 0 ? -Y1 : Y1)) {std::cout<<"Con1";}

Box::Box(RECT B) : X1(B.left), Y1(B.top), X2(B.right), Y2(B.bottom), W((X2 < 0 ? -X2 : X2) - (X1 < 0 ? -X1 : X1)), H((Y2 < 0 ? -Y2 : Y2) - (Y1 < 0 ? -Y1 : Y1)) {std::cout<<"Con3";}

Box::Box(int x1, int y1, int x2, int y2) : X1(x1), Y1(y1), X2(x2), Y2(y2), W((X2 < 0 ? -X2 : X2) - (X1 < 0 ? -X1 : X1)), H((Y2 < 0 ? -Y2 : Y2) - (Y1 < 0 ? -Y1 : Y1)) {std::cout<<"Con2";}

Box::Box(Point UpperLeft, Point LowerRight) : X1(UpperLeft.X), Y1(UpperLeft.Y), X2(LowerRight.X), Y2(LowerRight.Y), W((X2 < 0 ? -X2 : X2) - (X1 < 0 ? -X1 : X1)), H((Y2 < 0 ? -Y2 : Y2) - (Y1 < 0 ? -Y1 : Y1)) {}

Box::~Box(){}




//Testing the default constructor.
Box B;
std::cout<<"B was constructed";
GetWindowRect(SomeWindowHandle, (LPRECT)&B);
cout<<B.W<<endl;

The above prints "Con2, Con1, B Was Constructed" but yet the values of Width(W) and Height(H) are 0! I'm not sure why both constructors were called before it even hits GetWindowRect :S This leads me to believe that when the constructor was called, the initialization of the members were skipped! Why? The stuff inbetween the braces print AND the values of X1, Y1, X2, Y2 are correct, but width and height aren't.

Is there a way to get it initialized when casted like that? I don't think I want to put initialization in the brackets.

Recommended Answers

All 2 Replies

Anyone? I fixed my constructor problem but the casting still does not work correctly.

Is there any way to make this work:

    Box B;
    HWND Desktop = GetDesktopWindow();
    GetWindowRect(Desktop, (LPRECT) &B);

    cout<<B;    //Prints 0, 0, 1366, 768 For X1, Y1, X2, Y2 which is correct!
    cout<<B.W;  //Prints 0 Width. Wrong!
    cout<<B.H   //Prints 0 Height. Wrong!

All I want is for the width and height to work. Is there an operator that performs "(T1*) &T2" Conversion?

Box B
In your constructor X1, Y1, X2 & Y2 are initized to 0.
H and W are also initalized to 0.
After your call to GetWindowRect() X1, Y1, X2 & Y2 are assign the desktop coordinates but H & W are still 0.
You should provide some member functions in the Box class to calculate the height and width and call these to get their values.
Milton

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.