0

I have the following code:


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);
}

This is the output:

[Entity] constructed
[Entity] operation
[Entity] destructed
[Entity] destructed

I expected my function to use the custom constructor, so the output would look like this:

[Entity] constructed
[Entity] operation
[Entity] constructed
[Entity] destructed
[Entity] destructed

Why does this happens? How could I use my custom constructor instead?

Thanks :)

Jes321
  • 3
  • 1

1 Answers1

0

https://en.cppreference.com/w/cpp/language/copy_constructor

object as params will call copy constructor of class

Max Zhao
  • 16
  • 1