类成员operator new,调用构造函数两次

时间:2015-10-31 11:53:44

标签: c++ constructor new-operator member-functions

有人知道为什么用于调用的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.



}

所以,我正在将分配与构造混合在一起,这里没有理由这样做,也许在operator new []中有一些用来构造带有默认构造函数以外的构造函数的数组。 / p>

但定义这些成员运算符的最佳方法是:

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;





}

1 个答案:

答案 0 :(得分:3)

使用(keyword)new:它调用分配运算符new和(!)调用构造函数。

注意:当提供新的操作员时,您也应该提供操作员删除(在您的情况下,它将免费拨打)。另外,不要忘记数组版本。