M
M
mr-ZA2019-02-24 04:07:43
Qt
mr-ZA, 2019-02-24 04:07:43

Qt syntax question?

Hello, please explain, there are lines of the form:
QLabel *label = new QLabel("Hello");
label->show();
I understand that [* label] is a pointer to the address of objects which will be many in the future, probably? But why is the name of the class and the string ["Hello"] there at once?
Let's say this is an example:
SomeClass a; //an object is created here based on the class
SomeClass *p = &a; //this is a pointer to the address of the object to // call class methods through it
p-> print("Hi")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey〒., 2019-02-24
@axifive

QLabel *label = new QLabel("Привет");
The pointer is assigned the address of a new type object QLabelwith the text - "Hello"
SomeClass *ppointer to a type variableSomeClass

A
Andrey, 2019-02-28
@poslannikD

This is a C++ syntax question)

QLabel *label = new QLabel("Hi");
label->show();

let's break it down line by line
1) QLabel("Hello"); - created an instance of the QLabel class
2) new QLabel("Hi") - the new operator allocates memory on the heap, places an object there
QLabel("Hi")
and returns a pointer to an address on the heap. Our object is located at this address
3) = new QLabel("Hi"); - operator = performs an assignment (or initialization depending on the context). Assignment (initialization) of what? Assignment (initialization) of the right operand. Assignment (initialization) to what? Assignment (initialization) to the left operand.
4) QLabel *label - declared a pointer that can point to objects of the QLabel class.
5) QLabel *label = new QLabel("Hi"); -read from Right to Left created an object, placed it on the heap, passed the object's address to a QLabel type pointer which is located to the left of the = sign
This is a pointer to an OBJECT address , this pointer points to only ONE object at a time. since the const keyword is missing, yes it can point to different objects. But at a particular moment in time it only points to one object or nowhere
label->show();
- we refer to the object through the POINTER to this object
SomeClass a; //an object is created here based on the class
SomeClass *p = &a; //this is a pointer to the address of the object to // call class methods through it

1) SomeClass a; - the object is created on the STACK, it is not recommended to create heavy objects on the stack, but lightweight ones can be created.
2)SomeClass *p = &a; - the pointer points to an object on the stack, everything is fine :)
The difference between
SomeClass a;
and
new QLabel("Hi");
that the object a exists on the stack, and QLabel("Hello"); in a heap

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question