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. Other Discussions
  3. The Weird and The Wonderful
  4. Customise my static without a ctor

Customise my static without a ctor

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++performancequestion
5 Posts 4 Posters 2 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.
  • S Offline
    S Offline
    SortaCore
    wrote on last edited by
    #1

    I'll get a static initialised without a explicit ctor if it kills me! header:

    struct client {
    static client dummy;
    std::string username;
    int id; // 0+ OK
    }

    cpp:

    // Static definition
    client client::dummy = std::move(*[=] { client * c = new client();
    c->username = "";
    c->id = -1;
    return c;
    }()
    );

    I'm not going to use this code but I wonder about it... memory leak?

    L D 2 Replies Last reply
    0
    • S SortaCore

      I'll get a static initialised without a explicit ctor if it kills me! header:

      struct client {
      static client dummy;
      std::string username;
      int id; // 0+ OK
      }

      cpp:

      // Static definition
      client client::dummy = std::move(*[=] { client * c = new client();
      c->username = "";
      c->id = -1;
      return c;
      }()
      );

      I'm not going to use this code but I wonder about it... memory leak?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      And you are sure you didn't miss a way to write it more unreadable? ;P :laugh:

      It does not solve my Problem, but it answers my question

      S 1 Reply Last reply
      0
      • L Lost User

        And you are sure you didn't miss a way to write it more unreadable? ;P :laugh:

        It does not solve my Problem, but it answers my question

        S Offline
        S Offline
        SortaCore
        wrote on last edited by
        #3

        I'm used to C# so tried

        return new client(){username = ""; /* etc */ }

        but didn't get told off about it until compile-time ;P

        1 Reply Last reply
        0
        • S SortaCore

          I'll get a static initialised without a explicit ctor if it kills me! header:

          struct client {
          static client dummy;
          std::string username;
          int id; // 0+ OK
          }

          cpp:

          // Static definition
          client client::dummy = std::move(*[=] { client * c = new client();
          c->username = "";
          c->id = -1;
          return c;
          }()
          );

          I'm not going to use this code but I wonder about it... memory leak?

          D Offline
          D Offline
          David ONeil
          wrote on last edited by
          #4

          Why does the struct have to have a static copy of itself in it? Why can't you just do:

          struct client {
          std::string username;
          int id; // 0+ OK
          }

          static client theInstance;
          theInstance.username = "";
          ...

          The forgotten roots of science | C++ Programming | DWinLib

          J 1 Reply Last reply
          0
          • D David ONeil

            Why does the struct have to have a static copy of itself in it? Why can't you just do:

            struct client {
            std::string username;
            int id; // 0+ OK
            }

            static client theInstance;
            theInstance.username = "";
            ...

            The forgotten roots of science | C++ Programming | DWinLib

            J Offline
            J Offline
            Jon McKee
            wrote on last edited by
            #5

            David O'Neil wrote:

            Why does the struct have to have a static copy of itself in it?

            Doubt it but might be the pattern used by some of .NET for defaults. Think string.Empty or Comparer.Default[^].

            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