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. Basic C++ - can you explain the difference / function of each definition ?

Basic C++ - can you explain the difference / function of each definition ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
12 Posts 5 Posters 3 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 Lost User

    This is a test in few words of plain English , can you comment on the title preferably even answer the question without RTFM Google it everybody knows that snidely remarks etc etc etc

    QBluetoothLocalDevice localDevices;
    QBluetoothLocalDevice \*localDevices\_new =  new QBluetoothLocalDevice();
    

    Constructive comments , as always are appreciated. "just the facts ... ma'am...." PS I know one is using a pointer...

    G Offline
    G Offline
    Graham Breach
    wrote on last edited by
    #3

    The first one creates an instance on the stack. It will be destroyed when it goes out of scope. The second one creates an instance in heap memory*. It will not be automatically destroyed and you should call delete to destroy it. *Unless new has been overridden to do something unusual.

    1 Reply Last reply
    0
    • K k5054

      If you're having problems understanding that, then you really do need to take a step back, find a good resource for learning C++ and work your way through it. This is simple, basic C++ stuff that you should be able to grok, almost without thinking about it. Using more basic types

      class C {
      public:
      int n;
      C() { n = 0; } // class constructor;
      };

      C c; // declares an object of type C
      C *pc; // declares an pointer to an object of type C.
      // The pointer does not point to any object.
      // Until it is instantiated, accessing *pc is undefined
      C *pc2 = new C(); // declares a pointer to an object of type C
      // A new object of type C is created on the stack
      // and the object constructor is called
      // the object will remain valid until a
      // corresponding delete is performed

      But these days you really should be using smart pointers instead of new/delete. See the documentation for std::shared_ptr and std::unique_ptr here: [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory)

      Keep Calm and Carry On

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

      Yes, I am having a problem understanding this: I can "declare " int a; a should be undefined - at it is customary to define / assign value... That is NOT the issue. if I declare class a; should a be also undefined ?? if I have

      QBluetoothLocalDevice test;

      "declaration of object type

      QBluetoothLocalDevice

      " how does "test" contains all the real Bluetooth devices data? Show me a resource which describes what I just asked ? Do I have to look thru the

      QBluetoothLocalDevice

      class documentation to find which method makes the "test" to contain all the hardware info?

      K L 2 Replies Last reply
      0
      • L Lost User

        Yes, I am having a problem understanding this: I can "declare " int a; a should be undefined - at it is customary to define / assign value... That is NOT the issue. if I declare class a; should a be also undefined ?? if I have

        QBluetoothLocalDevice test;

        "declaration of object type

        QBluetoothLocalDevice

        " how does "test" contains all the real Bluetooth devices data? Show me a resource which describes what I just asked ? Do I have to look thru the

        QBluetoothLocalDevice

        class documentation to find which method makes the "test" to contain all the hardware info?

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #5

        Every C++ class has a constructor that gets called when the object is created. So if we have

        class C {
        public:
        int n;
        C() { n = -1; }
        }

        int main()
        {
        C c; // C:C() gets called here, assigning -1 to n;
        std::cout << c.n << '\n'; // will print -1
        }

        In the class C given above, if you do not give a default constructor, then the compiler will provide one, but it will not initialize the value of C.n

        class C {
        public:
        int n;
        };

        int main()
        {
        C c; // compiler provided constructor called
        std::cout << c.n << '\n'; // This generates a warning with -Wall (gcc) that n is unititalized
        }

        Presumably class QBluetoothLocalDevice provides a default constructor that fills in reasonable default values for its members. If some of those members use system resources (e.g. open file handles, memory, etc), then there is also a destructor that gets called when the object goes out of scope to release the resources (e.g close open files, release memory, etc).

        Keep Calm and Carry On

        1 Reply Last reply
        0
        • L Lost User

          This is a test in few words of plain English , can you comment on the title preferably even answer the question without RTFM Google it everybody knows that snidely remarks etc etc etc

          QBluetoothLocalDevice localDevices;
          QBluetoothLocalDevice \*localDevices\_new =  new QBluetoothLocalDevice();
          

          Constructive comments , as always are appreciated. "just the facts ... ma'am...." PS I know one is using a pointer...

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

          1. Create an object on the stack

          QBluetoothLocalDevice localDevices;

          This reserves all the memory space required for a QBluetoothLocalDevice object on the local stack. It then calls the constructor of the class to initialise any parts of that memory as specified in the class (see answers by @k5054). The variable localDevices holds the address of the object (even though it does not appear to be a pointer). 2. Create an object on the dynamic heap, and return a pointer to it.

          QBluetoothLocalDevice *localDevices_new = new QBluetoothLocalDevice();

          In this case the memory is requested from the heap, the constructor called to initialise it, and its address returned and saved in the pointer localDevices_new. The end result is much the same in both cases, apart from the location and lifetime of the two objects. In case 1 the object only exists within the function where it is created; it is automatically destroyed when the function ends. In case 2 the object exists until it is destroyed by the delete statement, or the program terminates. But as suggested elswhere, this is basic C++, which you should have learned and understood long before you charged down this rabbit hole that you currently find yourself in.

          1 Reply Last reply
          0
          • L Lost User

            Yes, I am having a problem understanding this: I can "declare " int a; a should be undefined - at it is customary to define / assign value... That is NOT the issue. if I declare class a; should a be also undefined ?? if I have

            QBluetoothLocalDevice test;

            "declaration of object type

            QBluetoothLocalDevice

            " how does "test" contains all the real Bluetooth devices data? Show me a resource which describes what I just asked ? Do I have to look thru the

            QBluetoothLocalDevice

            class documentation to find which method makes the "test" to contain all the hardware info?

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

            Member 14968771 wrote:

            Do I have to look thru the QBluetoothLocalDevice class documentation to find which method makes the "test" to contain all the hardware info?

            Yes, as you would need to do with any class that you are using. And here it all is: QBluetoothLocalDevice Class | Qt Bluetooth 6.4.2[^].

            1 Reply Last reply
            0
            • L Lost User

              This is a test in few words of plain English , can you comment on the title preferably even answer the question without RTFM Google it everybody knows that snidely remarks etc etc etc

              QBluetoothLocalDevice localDevices;
              QBluetoothLocalDevice \*localDevices\_new =  new QBluetoothLocalDevice();
              

              Constructive comments , as always are appreciated. "just the facts ... ma'am...." PS I know one is using a pointer...

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #8

              Member 14968771 wrote:

              This is a test in few words of plain English ,

              Which suggests homework. But the answer is no. The question cannot be answered in "plain english" because it require concepts that only exist in programming. So as the other answers suggest one would need to understand what a 'local variable' is and what a 'heap' is for any answer to make sense.

              L 1 Reply Last reply
              0
              • J jschell

                Member 14968771 wrote:

                This is a test in few words of plain English ,

                Which suggests homework. But the answer is no. The question cannot be answered in "plain english" because it require concepts that only exist in programming. So as the other answers suggest one would need to understand what a 'local variable' is and what a 'heap' is for any answer to make sense.

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

                It's not homework. This member is struggling with basic concepts in C++.

                1 Reply Last reply
                0
                • L Lost User

                  This is a test in few words of plain English , can you comment on the title preferably even answer the question without RTFM Google it everybody knows that snidely remarks etc etc etc

                  QBluetoothLocalDevice localDevices;
                  QBluetoothLocalDevice \*localDevices\_new =  new QBluetoothLocalDevice();
                  

                  Constructive comments , as always are appreciated. "just the facts ... ma'am...." PS I know one is using a pointer...

                  C Offline
                  C Offline
                  charlieg
                  wrote on last edited by
                  #10

                  Please follow up if the explanations helped.

                  Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                  L 1 Reply Last reply
                  0
                  • C charlieg

                    Please follow up if the explanations helped.

                    Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

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

                    I will not engage in a discussion full of criticism.

                    C 1 Reply Last reply
                    0
                    • L Lost User

                      I will not engage in a discussion full of criticism.

                      C Offline
                      C Offline
                      charlieg
                      wrote on last edited by
                      #12

                      oh well, suit yourself.

                      Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

                      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