类成员运算符new,调用构造函数两次

class member operator new, calling constructor twice

本文关键字:两次 构造函数 调用 成员 运算符 new      更新时间:2023-10-16

有人知道为什么Latter语法调用Dog::operator new在完成分配后调用默认构造函数,最终调用2个构造函数?

我想知道我是否做错了什么,我该怎么做:

Dog *ptr = new("arg") Dog();

而不调用2个构造函数。并且不使用任何技巧,比如在对象已经构造好的情况下检查默认构造函数。这是代码:

class Dog
{
public:
    Dog() // default
    {
        std::cout << "default Dog constructor [" << this << "]" <<  std::endl;
    }
    Dog(int x)  // int argument
    {
        std::cout << "dog constructor int " << x << "[" << this << "]" << std::endl;
    }
    Dog(const std::string& word) // std::string argument
    {
        std::cout << "dog constructor std::string: " << word << " ["<< this << "]" << std::endl;
    }
    Dog(std::string &&word) // rvalue string argument
    {
        std::cout << "dog constructor std::string&& rvalue: " << word << " [" << this << "]" << std::endl;
    }

    // custom operator new
    static void *operator new(std::size_t size) noexcept // for default constructor
    {
        Dog *ptr = (Dog*)malloc(size); // allocate memory
        if (ptr) // if allocate ok
        {
            ::new(ptr) Dog(); // call default constructor on object in memory
            return ptr; // returns
        }
        else
            return nullptr;
    }
    template<class T>
    static void * operator new(std::size_t size, T&& value) noexcept // for argument constructor
    {
        Dog *ptr = (Dog*) malloc(size); // allocate the memory
        if (ptr)
        {
            ::new (ptr)  Dog(std::forward<T>(value)); // pass the argument exactly as was passed to operator new,
                                                        // using perfect forwarding
            return ptr;
        }
        else
            return nullptr;
    }

    ~Dog() { std::cout << "destructor " << std::endl;  }
};

int main(void)
{


    Dog *d = (Dog*) Dog::operator new(sizeof(Dog), "Const Char * Argument"); // argument version
    Dog *d2 = (Dog*)Dog::operator new(sizeof(Dog)); // default constructor argument
    //1 this works as expected, do what you specified in the member operator new, everything goes normal.


    Dog *d3 = new Dog(); // default constructor
    Dog *d4 = new("Const Char * Argument") Dog(); // argument constructor
    // this is shorter, goes into your member operator new, BUT when it returns to this scope,
    // call the default constructor for *d3, and for *d4 too.
    // so this ends up calling constructors twice for both objects.

}

所以,我把分配和构造混合在一起,这里没有理由这样做,也许在运算符new[]中,用默认构造函数之外的构造函数来构造数组有一些用处。

但定义这些成员运营商的最佳方式是:

class Dog {
public:
// .......
        // custom operator new
    static void *operator new(std::size_t size) noexcept // for default constructor
    {
        void *memory = malloc(size); // allocate memory
        if (memory) // if allocate ok
        {   
            return memory; // returns
        }
        else
            return nullptr;
    }
    static void *operator new[](std::size_t size) noexcept
    {
        void *memory = malloc(size); // allocate memory
        if (memory) // if allocate ok
        {
            return memory; // returns
        }
        else
            return nullptr;
    }
    static void operator delete(void *block) noexcept
    {
        free(block);
    }
    static void operator delete[](void *block) noexcept
    {
        free(block);
    }
    ~Dog() { std::cout << "destructor " << std::endl;  }
};

int main(void)
{
    // now we can use new operator normaly without complications
    Dog *d1 = new Dog[10]; // default constructor on all objects
    Dog *d2 = new Dog("const char * argument"); // call std::string&& constructor
    delete[] d1;
    delete d2;


}

使用(关键字)new:它调用分配运算符new并(!)调用构造函数。

注意:当提供一个新的运营商时,你也应该提供一个运营商删除(在你的情况下,它会免费呼叫)。另外,不要忘记数组版本。