passing pointers to object in C++

Write your question here.
Please help C++ greenhorn navigate the task and learn how to pass pointers to an object.

My objective is to put an information in main window - GUI dialog.
I have build a class to access the required information and added the class / object to main window.

CCC_LocalAdapters *LocalAdapters = new CCC_LocalAdapters();

The info is retrieved by method which returns a pointer to the info and accessed by this line of code – which is wrong for my task :

LocalAdapters.LocalAdapter();

I cannot figure out how to make the info – which is for now a local variable in
LocalAdapter() method accessible in main window via a pointer.

My guess is I need to rebuild the constructor of the CCC_LocalAdapters() to include a pointer.


Here are the main and CCC_LocalAdapters() constructors :

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

CCC_LocalAdapters *LocalAdapters = new LocalAdapter(????);
}


CCC_LocalAdapters::CCC_LocalAdapters(QObject *parent) : QObject(parent)
{

// bluetooth test for local device - works as expected

QList<QBluetoothHostInfo> infos = QBluetoothLocalDevice::allDevices();

}

In not so technical terms – I need to pass “infos” upstream so I can let main
window GUI print it.


I’ll be happy if pointed to some decent tutorial about the “passing pointers to objects” in C++ .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
…
CCC_LocalAdapters *LocalAdapters = new LocalAdapter(????);
}

 
 CCC_LocalAdapters::CCC_LocalAdapters(QObject *parent) : QObject(parent)
{

    // bluetooth test for local device - works as expected 
    QList<QBluetoothHostInfo> infos = QBluetoothLocalDevice::allDevices();
…
}
it's hard to understand you and it seems that you have some conceptual errors
¿what are you trying to do? (not how, just what)


to dereference the pointer you may use * or ->
Trying to have access to object in hierarchy as posted. I am not asking for syntax how to use pointers.

I have declared
QList<QBluetoothHostInfo> infos;

in MainWindow

and attempted to build instance of CCC_LocalAdapters (in Main constructor ) and pass the "infos" to it.

CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo>*infos);

Now I am getting

/home/f/QT_PROJECT/TEST_PROJECT_3/mainwindow.cpp:84: error: expected primary-expression before '*' token
CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo> *infos);


Questions:

Am I on the right track - declaring "infos" and instantiation of the CCC_LocalAdapters object?

If so -
I am obviously passing the object incorrectly.
What is the correct syntax (way ) and

where can I read-up on passing the objects pointers in C++ - as stated ihttps://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-listn post title.




PS
Found this - busy looking for ANSWER

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list


Last edited on
CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(&info);
but if `info' is local to a function, when that function ends the pointer becomes invalid

I would also question the use of dynamic allocation for `LocalAdapters_info'
Qt, as marvelous as it is, is probably the hardest way to learn the fundamentals of C++. Combining that with Bluetooth and connections I can imagine this as a very steep learning curve.

If you are using Qt Creator or Qt Design Studio then trying out the QT Bluetooth scanner examples would be a good move if not already tried.

But there you go, as @ne555 writes you need to declare the pointer in MainWindow, and create the object (new etc) wherever using that pointer along with the appropriate file linkages. Otherwise you won't be moving upstream, downstream, sidestream or anywhere beyond.

BTW MainWindow is often not the best place to work in if you ever decide to have interfaces other than Qt MainWindow etc.
CCC_LocalAdapters *LocalAdapters_info = new CCC_LocalAdapters(QList<QBluetoothHostInfo>*infos);

Your problem doesn't seem to be anything to do with pointers, but with the very fundamentals of the syntax for passing arguments to a constructor. I suggest you take a look at:

https://www.cplusplus.com/doc/tutorial/classes/

and specifically the section about constructors.

There's nothing special about passing a pointer into a constructor, or into any other method; you do it in exactly the same way you would pass any other value.
Topic archived. No new replies allowed.