Share via

Constants in constructor arguments

Bryan Kelly 636 Reputation points
2022-09-10T03:19:52.813+00:00

Windows 11, VS 2019, C++, wxWidgets 3.2.0
The opening lines to construct a wxFrame are:

dialog_base::dialog_base(const wxString& title)  
    : wxFrame(  
        NULL,                 // parent  
        wxID_ANY,             // identifier  
        title,                // title                
        wxPoint( 10,  10 ),   // position  
        wxSize( 600, 400 )    // this works as expected  
//        wxSize(c_initial_width, c_initial_heigth)  // this causes panel size to be about 60 x 30  
        )  
{ …}  

As shown, it pops a dialog with width 600 and height 400.
From the h file:

const int c_initial_width  = 600;  
const int c_initial_height = 400;  

When the wxSize( 600, 400 ) line is commented out and the next one is activated, it builds and runs, but the dialog is a minimum size.
In a conversation in the wxWidgets forums I am told that the constants must be constructed and allocated a place in memory before they can be used. Therefore the values of those constants are not available when the code to construct the object starts execution.
I don’t buy that answer.
Another suggestion is to use a #define. I prefer not.
Does this make sense? Why might this happen?
I have tried several variations of declaring the constants in different places but the results always differ from using “600, 400”
What is the code needed to put these values in constants rather than hard coded so the object will be created properly.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

RLWA32 52,571 Reputation points
2022-09-10T16:42:24.017+00:00

Also, you should try this -

constexpr int test_width = 640;  
constexpr int test_height = 400;  
  
  
dialog_base::dialog_base(const wxString& title)  
    : wxFrame(  
        NULL,                 // parent  
        wxID_ANY,             // identifier  
        title,                // title                
        wxPoint( 10,  10 ),      // position  
        //wxSize( 600, 400 )                    // this works as expected  
        wxSize( test_width, test_height )  
 //        wxSize( setx, sety )                 // causes panel size to be about 60 x 30  
        )  
  

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bryan Kelly 636 Reputation points
    2022-09-11T00:49:13.46+00:00

    That constexpr is new to me. Thank you.

    The concept that a declared constant is not viable until some point in the constructor raises a question.
    Is it really a constant, meaning that 600, or 400 is actually put in the argument list for the constructor.
    But since the value appears not accessible until at least part of the constructor has run, it lends the appearance of not being a true constant. By that I mean that while the compiler does not allow it to be changed, the argument list really has the address of the value. And everywhere else in the object that references that constant uses its address to fetch the value. If this is the case, then it really is possible to change the value of the constant after compile time.
    I lack any such experience to estimate the possibility of that happening, but just being possible is a bit of a concern.

    Thanks again for your time.

    Was this answer helpful?

    0 comments No comments

  2. Bryan Kelly 636 Reputation points
    2022-09-10T16:22:49.38+00:00

    I saw the comments about static, looked up and read about static, but evidently did not understand it fully. On the other hand, it is still a constant and there was nothing about how static is initialized before the constructor.
    Using the static constants does work.
    Tried the enum version and it does work.
    It still strikes me as strange that a declared constant is not valid until after the constructor has been run.
    Ran a few more tests. The non-static declarations were activated along with this modification:

    dialog_base::dialog_base(const wxString& title)  
        : wxFrame(  
            NULL,                 // parent  
            wxID_ANY,             // identifier  
            title,                // title                
            wxPoint( 10,  10 ),      // position  
    //        wxSize( 635, 400 )                    // this works as expected  
            wxSize(c_initial_width, c_initial_height)  // causes panel size to be about 60 x 30  
            )  
    {  
        int x = c_initial_width;         // check these with the debugger  
    int y = c_initial_height;  …}  
    

    The dialog was open in the minimized state. A break point was added just after those lines, run it again, and variable x and y had the correct value. This is within the constructor for my object. I presume they are initialized within the wxFrame constructor. But, evidently again, the wxFrame constructor had already passed a critical point and the constants were not available.
    This continues to strike be as bad behavior.
    Regardless, that is the way it works and I will adapt.
    Thank you for your time and patience.

    Edit: I do not see anything that looks like a button to accept the above answer. The only rating is below my response and not below the reply. Having added this caveat, I will rate this as good.

    Edit again: After doing the above, the first reply is no longer visible to me. That is strange.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.