Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Proper way to put a C++ object inside a struct initialization?

Proper way to put a C++ object inside a struct initialization?

Scheduled Pinned Locked Moved C / C++ / MFC
hardwarec++questionworkspace
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I working in an embedded environment (STM32 microcontroller) and I compile using GCC (STM32CubeIDE). I want to put a C++ object inside a struct and this is the only way I could get it to compile:

    namespace MyNamespace {
    class MyObject
    {
    int m_i;
    public:
    MyObject(int i):m_i(i){}
    MyObject(const MyObject&) = delete;
    };
    }

    typedef struct {
    MyNamespace::MyObject* MyObject;
    } MyStruct_s;

    static MyStruct_s MyStruct = {new MyNamespace::MyObject(0) };

    However, I'm having strange hanging issues in my code and I suspect the code above might be the cause. Is the code above legal or will it corrupt RAM? If so, what's the correct way to do it? Ideally I'd like to have the whole object inside the struct, not just a pointer to it.

    C M 2 Replies Last reply
    0
    • A arnold_w

      I working in an embedded environment (STM32 microcontroller) and I compile using GCC (STM32CubeIDE). I want to put a C++ object inside a struct and this is the only way I could get it to compile:

      namespace MyNamespace {
      class MyObject
      {
      int m_i;
      public:
      MyObject(int i):m_i(i){}
      MyObject(const MyObject&) = delete;
      };
      }

      typedef struct {
      MyNamespace::MyObject* MyObject;
      } MyStruct_s;

      static MyStruct_s MyStruct = {new MyNamespace::MyObject(0) };

      However, I'm having strange hanging issues in my code and I suspect the code above might be the cause. Is the code above legal or will it corrupt RAM? If so, what's the correct way to do it? Ideally I'd like to have the whole object inside the struct, not just a pointer to it.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      As matter of fact, you don't need a pointer. Try

      #include using namespace std;

      namespace MyNamespace
      {
      class MyObject
      {
      int m_i;
      public:
      MyObject(int i):m_i(i){}
      int get_i(){return m_i;}
      };
      }

      struct MyStruct
      {
      MyNamespace::MyObject myObject;
      };

      static MyStruct s_myStruct = { MyNamespace::MyObject(5) };

      // this probably won't run on a microcontroller...
      int main()
      {
      cout << s_myStruct.myObject.get_i() << endl;
      }

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      A 1 Reply Last reply
      0
      • A arnold_w

        I working in an embedded environment (STM32 microcontroller) and I compile using GCC (STM32CubeIDE). I want to put a C++ object inside a struct and this is the only way I could get it to compile:

        namespace MyNamespace {
        class MyObject
        {
        int m_i;
        public:
        MyObject(int i):m_i(i){}
        MyObject(const MyObject&) = delete;
        };
        }

        typedef struct {
        MyNamespace::MyObject* MyObject;
        } MyStruct_s;

        static MyStruct_s MyStruct = {new MyNamespace::MyObject(0) };

        However, I'm having strange hanging issues in my code and I suspect the code above might be the cause. Is the code above legal or will it corrupt RAM? If so, what's the correct way to do it? Ideally I'd like to have the whole object inside the struct, not just a pointer to it.

        M Offline
        M Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        Your code is correct but dangerous. You don’t control the time when myStruct is initialized. There are various methods to control it. My preferred method is the nifty counter[^]. In general, you can include objects in structures. As a matter of fact C++ makes little difference between the two. Without seeing the code, I suspect that your problem is that you don’t have a default constructor for your object. Let me explain: as you don’t have a default constructor for myStruct, the compiler will synthesize one that invokes the default constructor for any included non-trivial members. If your object doesn’t have a default constructor, the compiler complains.

        Mircea

        A 1 Reply Last reply
        0
        • C CPallini

          As matter of fact, you don't need a pointer. Try

          #include using namespace std;

          namespace MyNamespace
          {
          class MyObject
          {
          int m_i;
          public:
          MyObject(int i):m_i(i){}
          int get_i(){return m_i;}
          };
          }

          struct MyStruct
          {
          MyNamespace::MyObject myObject;
          };

          static MyStruct s_myStruct = { MyNamespace::MyObject(5) };

          // this probably won't run on a microcontroller...
          int main()
          {
          cout << s_myStruct.myObject.get_i() << endl;
          }

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          I'm able to compile your code example. However, I just realized that I had forgotten the following line of code (I didn't think it was important) in my first post (I have added it now):

          MyObject(const MyObject&) = delete;

          I'm not good at programming C++ so I'm not sure what that line does. After I have added this line, I get the following error message with your code example: "use of deleted function 'MyNamespace::MyObject::MyObject(const MyNamespace::MyObject&)"

          M C 2 Replies Last reply
          0
          • M Mircea Neacsu

            Your code is correct but dangerous. You don’t control the time when myStruct is initialized. There are various methods to control it. My preferred method is the nifty counter[^]. In general, you can include objects in structures. As a matter of fact C++ makes little difference between the two. Without seeing the code, I suspect that your problem is that you don’t have a default constructor for your object. Let me explain: as you don’t have a default constructor for myStruct, the compiler will synthesize one that invokes the default constructor for any included non-trivial members. If your object doesn’t have a default constructor, the compiler complains.

            Mircea

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            I edited my initial post to also show the actual class.

            1 Reply Last reply
            0
            • A arnold_w

              I'm able to compile your code example. However, I just realized that I had forgotten the following line of code (I didn't think it was important) in my first post (I have added it now):

              MyObject(const MyObject&) = delete;

              I'm not good at programming C++ so I'm not sure what that line does. After I have added this line, I get the following error message with your code example: "use of deleted function 'MyNamespace::MyObject::MyObject(const MyNamespace::MyObject&)"

              M Offline
              M Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              The line prohibits compiler from copying MyObject objects. Look at the following code to better understand what's going on:

              namespace MyNamespace
              {
              class MyObject
              {
              int m_i;
              public:
              MyObject (int i) :m_i (i + 1) { cout << "MyObject constructor i=" << i << endl; }
              MyObject (const MyObject&) = delete; //these objects cannot be copied
              int get_i () const { return m_i; }
              };
              }

              struct MyStruct
              {
              MyStruct (const MyNamespace::MyObject& r) //tell compiler how to build a MyStruct
              : myObject (r.get_i ()) //cannot copy myObject so I just make a new one
              { cout << "MyStruct constructor" << endl; }

              MyNamespace::MyObject myObject;
              };

              static MyStruct s_myStruct { MyNamespace::MyObject (5) };
              //created a MyObject, then created a second one and finally finished construction of myStruct

              // this probably won't run on a microcontroller...
              int main ()
              {
              cout << s_myStruct.myObject.get_i () << endl;
              }

              The output is:

              MyObject constructor i=5
              MyObject constructor i=6
              MyStruct constructor
              7

              Mircea

              C 1 Reply Last reply
              0
              • A arnold_w

                I'm able to compile your code example. However, I just realized that I had forgotten the following line of code (I didn't think it was important) in my first post (I have added it now):

                MyObject(const MyObject&) = delete;

                I'm not good at programming C++ so I'm not sure what that line does. After I have added this line, I get the following error message with your code example: "use of deleted function 'MyNamespace::MyObject::MyObject(const MyNamespace::MyObject&)"

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                That disables the (otherwise automatically generated) copy constructor. Not sure why you need that. Have a look at https://stackoverflow.com/questions/34291938/why-and-when-delete-copy-constructor-and-operator[^].

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                A 1 Reply Last reply
                0
                • C CPallini

                  That disables the (otherwise automatically generated) copy constructor. Not sure why you need that. Have a look at https://stackoverflow.com/questions/34291938/why-and-when-delete-copy-constructor-and-operator[^].

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  A Offline
                  A Offline
                  arnold_w
                  wrote on last edited by
                  #8

                  I'm not allowed to make changes inside the class, I'm only supposed to write code that uses that class.

                  1 Reply Last reply
                  0
                  • M Mircea Neacsu

                    The line prohibits compiler from copying MyObject objects. Look at the following code to better understand what's going on:

                    namespace MyNamespace
                    {
                    class MyObject
                    {
                    int m_i;
                    public:
                    MyObject (int i) :m_i (i + 1) { cout << "MyObject constructor i=" << i << endl; }
                    MyObject (const MyObject&) = delete; //these objects cannot be copied
                    int get_i () const { return m_i; }
                    };
                    }

                    struct MyStruct
                    {
                    MyStruct (const MyNamespace::MyObject& r) //tell compiler how to build a MyStruct
                    : myObject (r.get_i ()) //cannot copy myObject so I just make a new one
                    { cout << "MyStruct constructor" << endl; }

                    MyNamespace::MyObject myObject;
                    };

                    static MyStruct s_myStruct { MyNamespace::MyObject (5) };
                    //created a MyObject, then created a second one and finally finished construction of myStruct

                    // this probably won't run on a microcontroller...
                    int main ()
                    {
                    cout << s_myStruct.myObject.get_i () << endl;
                    }

                    The output is:

                    MyObject constructor i=5
                    MyObject constructor i=6
                    MyStruct constructor
                    7

                    Mircea

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Fine.

                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups