Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. What is the actuall syntax to pass a variable to C++ class / constructor ?
Forum Updated to NodeBB v4.3 + New Features

What is the actuall syntax to pass a variable to C++ class / constructor ?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 589 Views 2 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    I plead ignorance...so sue me...

    I want to pass a variable , either int or better a whole QString ...
    My objective is to simply show a variable (int) related or direct text message when the class in used.
    I have never grasped the correct way to pass variables to C++ constructor - my fault...
    I would prefer a code (example) instead of "RTFM" suggestions...

    namespace Ui {
    class Form_MDI;
    }

    class Form_MDI : public QWidget
    {
    Q_OBJECT

    public:
    explicit Form_MDI(QWidget *parent = nullptr);
    ~Form_MDI();

    private:
    Ui::Form_MDI *ui;
    };

    #endif // FORM_MDI_H

    Form_MDI::Form_MDI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_MDI)
    {
    ui->setupUi(this);
    }

    Form_MDI::~Form_MDI()
    {
    delete ui;
    }

    JonBJ 1 Reply Last reply
    0
    • A Anonymous_Banned275

      I plead ignorance...so sue me...

      I want to pass a variable , either int or better a whole QString ...
      My objective is to simply show a variable (int) related or direct text message when the class in used.
      I have never grasped the correct way to pass variables to C++ constructor - my fault...
      I would prefer a code (example) instead of "RTFM" suggestions...

      namespace Ui {
      class Form_MDI;
      }

      class Form_MDI : public QWidget
      {
      Q_OBJECT

      public:
      explicit Form_MDI(QWidget *parent = nullptr);
      ~Form_MDI();

      private:
      Ui::Form_MDI *ui;
      };

      #endif // FORM_MDI_H

      Form_MDI::Form_MDI(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Form_MDI)
      {
      ui->setupUi(this);
      }

      Form_MDI::~Form_MDI()
      {
      delete ui;
      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @AnneRanch
      It's no different from any other method/function. I've added one int and one QString:

      explicit Form_MDI(int someInt, QString someString, QWidget *parent = nullptr);
      ...
      Form_MDI::Form_MDI(int someInt, QString someString, QWidget *parent) :
      ...
      // Caller example:
      Form_MDI *formMDI = new Form_MDI(42, "Hello World", this);
      
      1 Reply Last reply
      2
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        https://en.cppreference.com

        I spend a lot of time here to understand the internals of C++.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved