Basic C++ - can you explain the difference / function of each definition ?
-
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...
-
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...
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 performedBut these days you really should be using smart pointers instead of new/delete. See the documentation for
std::shared_ptr
andstd::unique_ptr
here: [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory)Keep Calm and Carry On
-
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...
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. *Unlessnew
has been overridden to do something unusual. -
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 performedBut these days you really should be using smart pointers instead of new/delete. See the documentation for
std::shared_ptr
andstd::unique_ptr
here: [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory)Keep Calm and Carry On
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?
-
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?
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.nclass 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
-
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...
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 variablelocalDevices
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 thedelete
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. -
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?
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[^].
-
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...
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.
-
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.
-
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...
-
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.