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 7688621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:48+00:00 2026-05-31T19:57:48+00:00

I have a member pointer with this type: const TopState<TestHSM>* state_; state_ is of

  • 0

I have a member pointer with this type:

const TopState<TestHSM>* state_;

state_ is of polymorphic type.

TopState is the base:

template<typename H>
struct TopState {
//... functions etc
};

There is a bit of a hierarchy and finally LeafState which is not abstract:

template<typename H, unsigned id,
typename B=CompState<H,0,TopState<H> > >
struct LeafState : B {
   //...functions
   static const LeafState obj;
};

The following objects represent state:

//indentation to indicate state nesting
typedef CompState<TestHSM,0>      Top;
typedef CompState<TestHSM,1,Top>    S0;
typedef CompState<TestHSM,2,S0>       S1;
typedef LeafState<TestHSM,3,S1>         S11;
typedef CompState<TestHSM,4,S0>       S2;
typedef CompState<TestHSM,5,S2>         S21;
typedef LeafState<TestHSM,6,S21>          S211;

Note only S11 and S211 (LeafState‘s) can be instantiated.

I have a TestHSM class which looks like this:

class TestHSM {
public:
TestHSM()  {
    state_ = new S11;
}

//fix destruction - problem
~TestHSM() {
    //reset to s11
//  state_ = &S11;
//  delete state_;
//  state_ = 0;
}

void next(const TopState<TestHSM>& state)
{ 
    state_ = &state; 
}

private:
const TopState<TestHSM>* state_;
};

My problem right now is creation of the state_ object. See constructor above. This works but I am not entirely sure if it is correct way to do things?

S11 is the first state where the object can be instantiated – and in my program S11 is the first state at startup. The code ‘works’ as expected. Or seems to anyway. But I am not sure that this is the optimal or even correct way to instatiate the fist state?

In addition, if I do this then I get heap memory runtime errors when I attempt to delete state_ – see commented out destructor code.

Any suggestions?

Angus

  • 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-05-31T19:57:49+00:00Added an answer on May 31, 2026 at 7:57 pm

    Your object construction and usage is correct and standard C++. The only problem is with your destruction code as you note. You are trying to take a pointer of a type – this is not going to work and the compiler won’t let you do that. The destructor should be as simple as freeing the memory:

    ~TestHSM() {
      if (state_)  {
        delete state_;
        state_ = 0;
      }
    }
    

    Also, be sure when you change states to delete the previous state. I.e. the next() function should look like this:

    void next(TopState<TestHSM> *state)
    { 
        if (state_)
          delete state_;
        state_ = state; 
    }
    

    You should always pass a state constructed using new to the next function and let TestHSM free it when it’s unnecessary:

    int main()  {
      TestHSM test;
      test.next(new S211());
      // No freeing, TestHSM destructor frees everything
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template member function with this signature: template<typename T> void sync(void (*work)(T*),
I have a map of pointer to member declared as : std::map<char, T (Operand::*)(const
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
I wonder if it is a good practice to have a member template function
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I have a interface member like this. AppInterface.cs ObjLocation[] ArrayLocations { get; } App.cs
I have the following member of class foo. foo &foo::bar() { return this; }
This is strictly in C. Say you're given one base struct, and 2 other
Let us say I have: // This is all valid in C++11. struct Foo

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.