Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having a hard time understanding the concept of sharing anything between members of a class, especially when the member is another class.
The C++ tool I am using does not support inheritance and my task is to pass data between members.

My specific question is :
it looks as I need to replace default null pointer and pass a "this" pointer starting with an "new" instance of the primary class.

In my case

C++
MainWindow_Bluetooth *MWBT = new MainWindow_Bluetooth(this);




C++
MainWindow_Bluetooth::MainWindow_Bluetooth(QWidget *parent) :
      QMainWindow(parent),
      m_ui(new Ui::MainWindow_Bluetooth),
      m_status(new QLabel),
      m_console(new Console),
      // insert MDI area here ??9
      m_mdiarea(new QMdiArea(parent)),
      m_settings(new SettingsDialog(parent)),
      //! [1]
      //! add rfcomm ??
      m_serial(new QSerialPort(this))
    //! [1]
  { // function\


I am asking for verification / confirmation / explanation of this.
Is is conceptually acceptable in C++ which lacks inheritance?


At present I like to detect an event in settingsDialog ( have that working ) and execute a method in QMdiArea.

I can execute QMmdiArea from within the class, but my code to do so from SettignsDialog does not work.

I AM NOT ASKING FOR CODE!

I need better understanding of the "passing data " conceptually,

if there is RTFM I am game.



C++
m_mdiarea(new QMdiArea(parent)),
m_settings(new SettingsDialog(parent)),


What I have tried:

I get compilation errors when default - null pointer - is passed to derived class.
Posted
Comments
k5054 10-May-24 11:44am    
You'll need to explain what you're trying to do better.
"my C++ tool does not support inheritance" and " ... is passed to a derived class" are almost oxymoronic. Base/Derived classes are inheritance in C++ speak, something that goes back to pre ANSI C++, I'm sure. So you're doing something else. Maybe nested classes?
[no name] 10-May-24 18:31pm    
Now explain why SettingsDialog is NOT part of the "main window" ?
Should that be "not part of the base class" ?
I am not being picky, but I do prefer using C++ terminology when appropriate.
[no name] 10-May-24 18:38pm    
OK, these comments are not is posted sequence.
Can I stay with the OP ?
I really do not want to get into "why not inheritance".
Kindly allow me to fix this first, using resources I have.
If that is not OK....

1 solution

this is always the current instance of the current class - you can't 'pass a "this" pointer starting with an "new" instance of the primary class' and assume it will all work out.

When you inherit class B from class A, you add information - the instance of class B you create is also an instance of class A becuas eit contains everything A does plus the B additions, but the reverse is not true - an instance of class A is NOT an instance of class B because it doesn't contain any B info.

For example, if you have a Fruit class, and you derive the Strawberry class from that, you can execute the "Hull" operation on a Strawberry, but generic Fruits can't be "hulled". However, you can "Pick" any Fruit, including Strawberries because that is a generic Fruit operation (though the exact way you "Pick" it may differ from Fruit to Fruit. Similarly, you can "Peel" an Orange, or an Apple, or a Banana, but you can't "Peel" every Fruit.

In your case the dialog isn't part of the main window, and it doesn't necessarily have access to it's contents (for pretty good reasons!) What you need to do is tell the main window that something has happened and that it should respond by executing some of it's code on your data. How you do that, I have no idea - I don't touch QuickTime with a ten foot cattle prod - so you will need to go back to wherever you are learning this stuff from (hopefully not YouTube or you are completely knackered) and read up on how to pass / return data from a dialog to the calling window.
 
Share this answer
 
Comments
[no name] 10-May-24 18:24pm    
OK, allow me to start from scratch.
When I create an instance of the primary/ base class and DO NOT
specify "thIs" or ANY pointer , hence use default NULL pointer - that is OK?
OriginalGriff 11-May-24 1:10am    
No, read what I said again: "this is always the current instance of the current class" If you are executing class code, the system sets this for you. null is very different and indicates "no instance here at all".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900