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