<分区>
我正在尝试实现一个返回该类实例的方法,但它在第一次尝试创建实例时崩溃了。我不知道如何在C++/QT中实现单例
主要
#include <QCoreApplication>
#include "carro.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Carta* nueva;
nueva->getInstance();
nueva->setColor("rojo");
cout << nueva->getColor() << endl;
return a.exec();
}
卡罗.h
#ifndef CARRO_H
#define CARRO_H
#include <string>
#include <iostream>
using namespace std;
class Carta{
private:
string cara; //valor
string palo; //simbolo
string color;
string direccion;
static Carta* m_instance;
public:
//constructor
Carta(){
}
static Carta* getInstance(){
if(!m_instance){
m_instance = new Carta;
}
return m_instance;
}
string getDireccion(){
return direccion;
}
void setColor(string pcolor){
color = pcolor;
}
string getColor(){
return this->color;
}
string getPalo(){
return this->palo;
}
string getCara(){
return this->cara;
}
//print
string print(){
return (cara + " de " + palo + " Color: "+color);
}
};
#endif // CARRO_H