Initializing const struct member

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    Initializing const struct member

    How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right...
    [code=cpp]

    class Example
    {
    public:
    Example() : st.a(1), st.b(5) // error
    {
    }

    const struct TheStruct
    {
    int a;
    int b;
    } st;
    };
    [/code]
    Obviously it looks like I'm trying to reference nonexistant methods of the struct. What is the correct syntax?
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by arnaudk
    How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right...
    [code=cpp]

    class Example
    {
    public:
    Example() : st.a(1), st.b(5) // error
    {
    }

    const struct TheStruct
    {
    int a;
    int b;
    } st;
    };
    [/code]
    Obviously it looks like I'm trying to reference nonexistant methods of the struct. What is the correct syntax?
    Are you trying to declare a const instance of the struct in the class? If so, use "static const." That way, it will be initialized, then it cannot be changed.

    something like
    Code:
    class MyClass
    {
    	public:
    		MyClass(){};
    	struct MyStruct
    	{
    		static const int a = 1;
    		static const int b = 2;
    	};
    
    	MyStruct MyInstanceOfMyStruct;
    };

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      Hi Sick0Fant, and thanks for your reply. If I initialized it as you proposed, then it would be the same accross all instances of Example objects. However, by const I merely mean to enforce that, once initialized, the struct should not change. It will, however, hold different values accross different instances of Example. Do you know how I may initialize such a struct in the initialization list of the Example constructor?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Here you go:
        [code=cpp]
        class Example
        {
        public:
        Example() : st(1,5)
        {
        }

        struct TheStruct
        {
        int a;
        int b;
        TheStruct(int first, int second);
        };
        const TheStruct st;
        };
        Example::TheStr uct::TheStruct( int first, int second) : a(first), b(second)
        {

        }
        int main()
        {
        Example obj;
        }
        [/code]

        What you have to do is have a const data member. But that means the const values need to be set before the TheStruct constructor is called so st.a is too late.

        What I did was create a TheStruct declaration followed by a const instance of TheStruct. That requires a constructor for the struct.

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Great! Thanks agian.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by weaknessforcats
            Here you go:
            [code=cpp]
            class Example
            {
            public:
            Example() : st(1,5)
            {
            }

            struct TheStruct
            {
            int a;
            int b;
            TheStruct(int first, int second);
            };
            const TheStruct st;
            };
            Example::TheStr uct::TheStruct( int first, int second) : a(first), b(second)
            {

            }
            int main()
            {
            Example obj;
            }
            [/code]

            What you have to do is have a const data member. But that means the const values need to be set before the TheStruct constructor is called so st.a is too late.

            What I did was create a TheStruct declaration followed by a const instance of TheStruct. That requires a constructor for the struct.
            That is so incredibly filthy! No, seriously, very clever WFC; my compliments.

            kind regards,

            Jos

            Comment

            Working...