Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:04:10+00:00 2026-06-04T07:04:10+00:00

I’m an old C-dude that tries to learn about C++11 by porting my old

  • 0

I’m an old C-dude that tries to learn about C++11 by porting my old state-machine framework from C to C++11. My idea is to have a class for the state-machine itself and then nested classes for the states within. The states may be hierarchical, i.e. super- and substates. The framework needs to know about a state’s superstate and for that I have a pointer (state *superstate) in the nested state class.

My problem is that I intended to set the superstate-pointer by using the constructor directly within the machine’s class, which should be possible in C++11 with non-static data member initialization, by using uniform initialization. But some reason it fails to compile (substateB3{superstateA}) when set to another type of state/class. But it works fine if I later set it by using a specific function (set_superstate) for this purpose, which has the same argument as the constructor! And funny enough the constructor is accepted if I set the superstate to a state/class of same type (substateB2{substateB1}).

I’m using gcc 4.7.0 (to get support for non-static data member initializers) and here’s my code:

// My state-machine framework (simplified)
struct machine {
  struct state {
    state()                  : superstate(nullptr) { }     // No superstate => toplevel state!
    state(state &superstate) : superstate(&superstate) { }
    state *superstate;
    void set_superstate(state &superstate) { this->superstate = &superstate; } // Non-ctor way to set superstate
  };
};

// An example of a specific state-machine using my framework
struct Machine : machine {
  struct SuperstateA : state {
  } superstateA;

  struct SubstateB : state {
  } substateB1,              // Compiles OK; gets its superstate set in Machine's ctor below
    substateB2{substateB1},  // Compiles OK; but not correct superstate
    substateB3{superstateA}; // gcc 4.7.0 error: could not convert ‘{((Machine*)this)->Machine::superstateA}’ from ‘<brace-enclosed initializer list>’ to ‘Machine::SubstateB’

  Machine()  { substateB1.set_superstate(superstateA); } // Compiles OK;
} myMachine;

Any tips or guidance are much appreciated thanks! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:04:10+00:00Added an answer on June 4, 2026 at 7:04 am

    Stripping one layer of inheritance:

    struct m {
        struct state { state *s;
            state() : s(0) {};
            state(state &s) : s(&s) {}
            set(state &s) { this->s = &s; }
        };
    
        struct s1 : state {} A;   // calls s1(), the default constructor
        struct s2 : state {}    B     // calls s2(), ditto
        ,                       C{B}  // calls s2(const s2&), the default copy constructor
        ,                       D{A}; // calls s2(const s1&)
    
        m() { B.set(A); } // The m() body runs after all the members are constructed.
    } M;
    

    You’re getting the construction error because your substates declare no constructors so they’re getting the compiler-provided defaults, and there is none from a sibling- or base- class reference (the compiler doesn’t provide s2(s1&) or s2(state&)).

    You’re getting the wrong superstate for C because C{B} invokes the default copy constructor s2(s2&), which runs before m()‘s body.

    Here’s what you want instead:

    struct m {
        struct state { state *s; state() : s(0) {} state(state &s) : s(&s) {} };
        struct s1 : state {} A; // default-constructs, fine
        struct s2 : state {
            s2(state &s) : state(s) {}
            s2(s2&s)     : state(s) {}
        }            B     // default-constructs
        ,            C{B}  // calls s2(s2&), invokes state(state&)
        ,            D{A}; // calls s2(state&)
        ;
        m() : B(A) {};
    } M;
    

    When M’s constructor runs, first its base classes (there are none) then its members are constructed in declaration order using the specified initializations. There’s only one: B(A), so all the rest are defaulted. After all the bases and members are constructed, then the object constructor’s body runs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.