我有以下代码:
struct Entity {
Entity() {
std::cout << "[Entity] constructed\n";
}
~Entity() {
std::cout << "[Entity] destructed\n";
}
void Operation(void) {
std::cout << "[Entity] operation\n";
}
};
void funcCpy(Entity ent) {
ent.Operation();
}
int main() {
Entity e1;
funcCpy(e1);
}
这是输出:
[Entity] constructed
[Entity] operation
[Entity] destructed
[Entity] destructed
我希望我的函数使用自定义构造函数,所以输出如下:
[Entity] constructed
[Entity] operation
[Entity] constructed
[Entity] destructed
[Entity] destructed
为什么会这样?我如何使用自定义构造函数呢?
谢谢:)