Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Question about "auto" keyword in combination with "new"
Forum Updated to NodeBB v4.3 + New Features

Question about "auto" keyword in combination with "new"

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 5 Posters 2.0k Views 2 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.
  • KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by
    #1

    Hi all C++ gurus, I have a question about auto usage with pointers.
    Is there a difference for this two declaration?

    auto var1 = new MyClass();
    auto* var2 = new MyClass();
    

    Is one preferable to ensure C++ compiler compatibility (msvc, gcc, clang)?
    Or those it not mather?

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    aha_1980A 1 Reply Last reply
    0
    • KroMignonK KroMignon

      Hi all C++ gurus, I have a question about auto usage with pointers.
      Is there a difference for this two declaration?

      auto var1 = new MyClass();
      auto* var2 = new MyClass();
      

      Is one preferable to ensure C++ compiler compatibility (msvc, gcc, clang)?
      Or those it not mather?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @KroMignon,

      Is there a difference for this two declaration?
      auto var1 = new MyClass();
      auto* var2 = new MyClass();

      No, they are equal.

      Is one preferable to ensure C++ compiler compatibility (msvc, gcc, clang)?

      I'm not sure if there are compiler differences, they should all handle both without problems; so it's more a matter of style.

      E.g. in QtCreator we use auto ptr = new Ptr(); (without asterisk).

      Regards

      Qt has to stay free or it will die.

      J.HilkJ 1 Reply Last reply
      2
      • aha_1980A aha_1980

        Hi @KroMignon,

        Is there a difference for this two declaration?
        auto var1 = new MyClass();
        auto* var2 = new MyClass();

        No, they are equal.

        Is one preferable to ensure C++ compiler compatibility (msvc, gcc, clang)?

        I'm not sure if there are compiler differences, they should all handle both without problems; so it's more a matter of style.

        E.g. in QtCreator we use auto ptr = new Ptr(); (without asterisk).

        Regards

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @aha_1980 said in Question about "auto" keyword in combination with "new":

        No, they are equal.

        In almost all cases!
        A quick google search resulted in this stack overflow thread:
        https://stackoverflow.com/questions/12773257/does-auto-type-assignments-of-a-pointer-in-c11-require

        🤷‍♂️


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        KroMignonK 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @aha_1980 said in Question about "auto" keyword in combination with "new":

          No, they are equal.

          In almost all cases!
          A quick google search resulted in this stack overflow thread:
          https://stackoverflow.com/questions/12773257/does-auto-type-assignments-of-a-pointer-in-c11-require

          🤷‍♂️

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @J-Hilk Thank you for the link, I tried myself a google search before posting my question, but did not find this entry!

          @aha_1980 thank you also to take time to reply, I've added a start on all my auto in combination with new. It is more "clean" for me, even if it made no real difference.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          2
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @KroMignon said in Question about "auto" keyword in combination with "new":

            Is there a difference for this two declaration?

            Not sure if you noticed, but if you hover over the "auto" keyword with the mouse in Qt Creator it tells you the type it has deduced most of the time.

            C++ is a perfectly valid school of magic.

            fcarneyF 1 Reply Last reply
            1
            • fcarneyF fcarney

              @KroMignon said in Question about "auto" keyword in combination with "new":

              Is there a difference for this two declaration?

              Not sure if you noticed, but if you hover over the "auto" keyword with the mouse in Qt Creator it tells you the type it has deduced most of the time.

              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @KroMignon
              Apparently it does have a use:
              https://en.cppreference.com/w/cpp/language/auto

              Is particular:

              auto i = 0, *p = &i;
              

              In this case auto is deduced as int. So *p is a pointer to int.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @fcarney said in Question about "auto" keyword in combination with "new":

                auto i = 0, *p = &i;

                But this is nothing special to the auto keyword. It's C basic.

                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

                • Login

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