我有一节课:
class SymbolIndexer {
protected:
SymbolIndexer ( ) { }
public:
static inline SymbolIndexer & GetUniqueInstance ( )
{
static SymbolIndexer uniqueinstance_ ;
return uniqueinstance_ ;
}
};
Run Code Online (Sandbox Code Playgroud)
我该如何修改它以禁用如下代码:
SymbolIndexer symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
Run Code Online (Sandbox Code Playgroud)
并且只允许以下代码:
SymbolIndexer & ref_symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( );
Run Code Online (Sandbox Code Playgroud) 假设我有一个类,我想确保我的编译器(在这种情况下是GCC)不合成任何构造函数或赋值方法.我找到了一种方法来做这个,只是在类中包含一个const int成员,但这并没有让我感觉良好.是否有属性或某些东西表示这一点.
我试图熟悉C++中的构造函数和析构函数.下面的程序只是创建一个复数,在stdio上打印并退出.我创建了3个对象(1.使用默认构造函数,2.使用显式构造函数,第三个使用复制构造函数.在退出之前,它破坏了4个对象.为什么我的程序下面会破坏比构造函数更多的对象?
#include <iostream>
using namespace std;
class complex
{
private: float a; float b;
public: float real(){return a;};
float imag(){return b;};
complex(){a=0.0; b=0.0;cout << "complex no. created."<<endl;};
complex(float x, float y){a=x; b=y;};
~complex(){cout << "complex no. with real part " << this->real() << " destroyed" << endl;};
void display(){cout << a << '+' << b << 'i';}
friend ostream& operator<< (ostream & sout, complex c)
{
sout << c.a << '+' << c.b << 'i' << "\n";
return sout;
} …Run Code Online (Sandbox Code Playgroud)