-1

I have a class declared using this form

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

I want to refactor it with this form looking like this :

class MainWindow : QMainWindow, ui 
{
    MainWindow(QWidget *parent)
    {
        ...
    }
}

But I removed the parameters that were in the first form. What do this parameters mean ?

How to keep them in the second form ? Please explain me the first syntax (or point to tutorial). I don't understand inheritance with parameters.

EDIT:

I understand the problem now, the class was initialised in a separate .h file which I did not saw at first look. I thought : after the method definition was inheritance operator, whereas it is member initialization operator.

bokan
  • 3,601
  • 2
  • 23
  • 38
  • Unless I've misunderstood, the answer is "you can't". You are already using the correct constructor syntax; you can't just move some of your constructor syntax into the class definition and expect it to work. The parameter `parent` isn't even defined at the point of class definition! – Rook Sep 05 '12 at 16:21
  • The first is a declaration of a constructor. The second is some crazy attempt at inheritance. The parts after the colon are different things. In the first snippet the part after the `:` is member initializers, in the second snippet the part after the `:` is performing inheritence – Bob Fincheimer Sep 05 '12 at 16:22
  • @BobFincheimer Thanks "member initializer" was the term I needed. Thanks – bokan Sep 05 '12 at 16:29
  • 1
    I am afraid you won't have much success making Qt work if you lack basic C++ knowledge. I suggest you at least learn about classes first(since from your questions you seem to be grossly misunderstanding the concept). Any beginner book will do and it won't take that much time. Sure, you can't "learn to program" in just a few days, but you will at least know where to look. And what for. – Zeks Sep 05 '12 at 21:25

2 Answers2

1

This is the implementation of a constructor, calling a base class constructor and initializind a data member in the initialization list:

MainWindow(QWidget *parent) :
        QMainWindow(parent),   // call base class constructor
        ui(new Ui::MainWindow) // initialize data member ui
{
    ...
}

It is very likely that the class inherits from QMainWindow and has a Ui::MainWindow* data member called ui. Your second code snippet seems like an incorrect attempt to declare a class. There is no "inheritance with parameters" as such, so you cannot do what you ask about.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

First is constructor with initializer-list initialization and this example is well. Second example will never compile, since it's wrong syntax. Look at this for example In this specific case, is there a difference between using a member initializer list and assigning values in a constructor? for explanations about first syntax.

Community
  • 1
  • 1
ForEveR
  • 55,233
  • 2
  • 119
  • 133