constructor causes crash

Hi guys so I'm at the point where I think I'm comfortable enough to start trying some frameworks/apis so I am thinking of learning the qt framework so I can start developing some simple gui applications,

anyway I noticed one strange thing in the qt MainWindow.cpp file

1
2
3
4
5
6
  MainWindow::MainWindow(QWidget *parent) :  
    QMainWindow(parent),  
    ui(new Ui::MainWindow)  
{  
    ui->setupUi(this);  
}


so I understand that ui is a pointer and we allocate a new MainWindow object to that pointer

but when I do pretty much the same thing with my own code everytime the program crashes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;


class Sample{

 public:
     Sample* ss;
     string text = "hey ya";
     explicit Sample()
     :ss(new Sample){

       cout << "hi" << endl; // doesn't print hi?
     }

     void print(){
        cout << ss->text << endl;
     }

     ~Sample(){

       delete ss;
     }
};

int main()
{
    cout << "Hello world!" << endl;
    Sample s;
    return 0;
}


at first I thought the crash was because of an infinite recursion each constructor will keep calling a new Sample

but if this was the case shouldn't hey be printed to the console?

also how come this code works in qt yet mine won't work,I mean it's pretty much doing the same thing,

note that in the qt MainWindow header the constructor is mark explicit if that makes any difference,I tried marking my constructor in my Sample class as explicit but still crashes

thanks
at first I thought the crash was because of an infinite recursion each constructor will keep calling a new Sample

Sure looks like that.


but if this was the case shouldn't hey be printed to the console?

Why would it print out? When would any constructor of Sample ever get that far?

also how come this code works in qt yet mine won't work,I mean it's pretty much doing the same thing,


No. In the QT code, you have an object of type MainWindow that creates an object of a different type, Ui::MainWindow.

Your code has an object of type Sample that creates an object of type Sample that creates an object of type Sample that creates an object of type Sample that creates an object of type Sample that creates an object of type Sample that creates an object of type Sample that creates ...
Last edited on
thanks repeater that makes sense :)

and true but how come we wrap the class MainWindow in the Ui namespace?

if I was to take a guess we forward declared the MainWindow class of the QMainWindow.h header to be in the Ui namespace in order to avoid a naming collision with MainWindow::MainWindow?

thanks




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void printHello();
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H 
Last edited on
Topic archived. No new replies allowed.