Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sharing a QLatin1String across derived classes: static const vs. inline static const
Forum Updated to NodeBB v4.3 + New Features

Sharing a QLatin1String across derived classes: static const vs. inline static const

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 192 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.
  • L Offline
    L Offline
    l3u_
    wrote on 21 Jan 2025, 17:13 last edited by l3u_
    #1

    Hi all!

    I want to share a QLatin1String across multiple classes derived from the same base class. I found two ways to do this:

    Variant 1: I declare it as static const inside BaseClass.h :

    class BaseClass
    {
        ...
        protected:
            static const QLatin1String someString;
    }
    

    and then I declare the value in BaseClass.cpp:

    #include "BaseClass.h"
    const QLatin1String BaseClass::someString("some string");
    

    Variant 2: I declare the variable as inline static const and assign the value right in BaseClass.h:

    class BaseClass
    {
        ...
        protected:
            inline static const auto someString = QLatin1String("some string");
    }
    

    For both variants, I can access the value via BaseClass::someString in all classes derived from BaseClass.

    I'm not sure about this. What is the right way to do it, and why?

    Thanks in advance to all the C++ pros for all clarification :-)

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 21 Jan 2025, 17:33 last edited by
      #2

      The only difference is that in variant 2 the optimizer can already optimize away e.g. QLatin1String::size() or other misc functions which need the length of the string.
      I doubt it makes any difference in speed here.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • L Offline
        L Offline
        l3u_
        wrote on 21 Jan 2025, 17:36 last edited by l3u_
        #3

        So this is more a question of programming style? Does it have any other implication? Besides the fact that I cheap out the line of code needed in variant 1 in the cpp file? Or is variant 1 still around because variant 2 was not possible before C++17?

        C 1 Reply Last reply 21 Jan 2025, 17:46
        0
        • L l3u_
          21 Jan 2025, 17:36

          So this is more a question of programming style? Does it have any other implication? Besides the fact that I cheap out the line of code needed in variant 1 in the cpp file? Or is variant 1 still around because variant 2 was not possible before C++17?

          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 21 Jan 2025, 17:46 last edited by
          #4

          @l3u_ said in Sharing a QLatin1String across derived classes: static const vs. inline static const:

          Or is variant 1 still around because variant 2 was not possible before C++17?

          correct

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • L Offline
            L Offline
            l3u_
            wrote on 21 Jan 2025, 18:07 last edited by
            #5

            Well okay, then I'll go for variant 2 – seems more logical to me to declare such a shared const variable right away instead of having to re-reference it inside the implementation.

            Thanks for the clarification :-)

            1 Reply Last reply
            0

            1/5

            21 Jan 2025, 17:13

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved