我有一个模板 class A
template <unsigned int m>
class A
{
public:
A(int) {}
};
Run Code Online (Sandbox Code Playgroud)
哪个有构造函数int
.我有一个手术:
template<unsigned int m>
A<m> operator+(const A<m>&, const A<m>&)
{
return A<m>(0);
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时:
A<3> a(4);
A<3> b = a + 5;
A<3> c = 5 + a;
Run Code Online (Sandbox Code Playgroud)
我想int
隐式转换为A,但编译器会抛出错误.
有没有优雅的方法来启用隐式转换而不使用以下解决方案:
a + A<m>(5)
operator+<3>(a, 5)
就像它已经写在这里 Qt到目前为止有8个特定的智能指针类.它看起来就像你需要的一切.但是,为了使用这些智能指针中的任何一个,您的类必须从QObject派生,这并不总是方便的.在Qt中是否有其他智能指针实现可以与任意类一起使用?
我们认为可能会发生初始化异常.所以我们编写try/catch块.
int f(){
throw 1;
}
class A
{
public:
A() try : _k(f())
{}
catch (int)
{
std::cout << "Exception 1" << std::endl;
}
private:
int _k;
};
Run Code Online (Sandbox Code Playgroud)
但是这个问题在一个层面上重新排除了异常.这意味着下一个代码
try
{
A a;
} catch(int)
{
std::cout << "Exception 2" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
将输出:
Exception 1
Exception 2
Run Code Online (Sandbox Code Playgroud)
为什么这个try/catch块的行为与普通的try/catch块不同?
完整代码示例:http://ideone.com/XjY2d
c++ exception-handling exception try-catch in-class-initialization
我已经在文件夹中创建src/modules/my-module/
了package.json
了导出我们需要的所有内容的主文件。
我现在可以从中导入import {A} from '../../modules/my-module'
我想将语法更改为import {A} from 'my-module'
,我有几个原因:
我已经成功地通过添加来编译它tsconfig.json
"paths": {
"my-module": ["src/modules/my-module"]
}
Run Code Online (Sandbox Code Playgroud)
但我无法通过 node.js 运行结果,因为节点找不到模块。在这种情况下有没有办法使用非相关模块引用。
很多类的赋值运算符(operator =)与析构函数中的代码相同,而且复制构造函数的代码非常相似.
那么以这种方式实现作业是否是个好主意?
Point& operator=(const Point& point)
{
if(&point != this)
{
//Call the destructor
this->~Point();
//Make the placement new
//Assignment is made because some compilers optimise such code as just
// new Point;
Point* p_n = new (this) Point(point);
//We where placing in this place so pointers should be equal
assert(p_n == this);
}
return *this;
}
Run Code Online (Sandbox Code Playgroud) 我想创建无法复制的类,所以我将复制构造函数放入私有部分:
class NotCopyable
{
public:
NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
~NotCopyable(void) {}
private:
NotCopyable& operator=(const NotCopyable&);
NotCopyable(const NotCopyable&);
double _attr1;
double _attr2;
};
Run Code Online (Sandbox Code Playgroud)
一切都很好,除非我想分配数组:
NotCopyable arr[] =
{
NotCopyable(1, 0),
NotCopyable(2, 3)
};
Run Code Online (Sandbox Code Playgroud)
编译器说她无法访问复制构造函数,因为它在私有部分.当我把它放在公共部分时:
class NotCopyable
{
public:
NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
~NotCopyable(void) {}
NotCopyable(const NotCopyable&)
{
std::cout << "COPYING" << std:: endl;
}
private:
NotCopyable& operator=(const NotCopyable&);
double _attr1;
double _attr2;
};
Run Code Online (Sandbox Code Playgroud)
程序编译没有错误,但不调用复制构造函数.所以问题是:我如何禁止复制但仍有可能分配数组?
为什么这段代码:
class B:
val = []
for i in range(0, 5):
obj = B()
print(obj.val)
obj.val.append('a')
Run Code Online (Sandbox Code Playgroud)
有这样的输出?
[]
['a']
['a', 'a']
['a', 'a', 'a']
['a', 'a', 'a', 'a']
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,都会创建新的B对象.为什么它具有前一个的价值?
c++ ×5
constructor ×1
exception ×1
javascript ×1
new-operator ×1
node-modules ×1
node.js ×1
python ×1
qt ×1
templates ×1
try-catch ×1
typescript ×1