为什么重载new操作符是调用构造函数,即使我使用malloc内部重载函数? [英] why overloaded new operator is calling constructor even I am using malloc inside overloading function?

查看:239
本文介绍了为什么重载new操作符是调用构造函数,即使我使用malloc内部重载函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解重载新运算子。我写了如下代码。

I am trying to understand overloading new operator. I wrote the code as below.

#include <iostream>
#include <cstdlib>
#include <new>

using namespace std;

class loc
{
    int lo, la;

    public:
        loc()
        {
        }

        loc(int x, int y)
        {
            cout << "In constructor\n";
            lo = x;
            la = y;
        }

        void show()
        {
            cout << lo << " ";
            cout << la << endl;
        }

        void *operator new(size_t sz);
        void operator delete(void *p);
};

void *loc::operator new(size_t sz)
{
    cout << "in Overloaded new\n";
    void *p = malloc(sz);

    return p;
}

void loc::operator delete(void *p)
{
    cout << "in Overloaded delete\n";
    free(p);
}

int main()
{
    loc *p1 = new loc(10, 20);
    p1->show();
    delete p1;
    return 0;
}

我认为它不会调用构造函数,因为我重载了new运算符malloc函数调用里面的重载函数。但输出如下。

I thought it won't call the constructor because I overloaded the new operator with malloc function call inside overloading function. But the output is as below.

in Overloaded new
In constructor
10 20
in Overloaded delete

这意味着构造函数被调用。这是怎么可能的?这是否意味着将malloc()调用构造函数?

That means constructor is getting called. How this is possible? Does this mean will malloc() call constructor?

推荐答案

A new expression 导致两个独立的事情发生:分配对象开始创建所需的内存,以及对象的初始化。

A new expression results in two separate things happening: allocation of the memory needed by the object begin created, and initialization of the object.

另一方面,新运算符只处理分配部分。当你对一个特定的类重载新的操作符时,你正在替换对象的内存分配。

The new operator, on the other hand, just handles the allocation part. When you overload the new operator with respect to a specific class, you are replacing the allocation of memory to the object.

这个函数的划分是有意义的,当你意识到所有对象都在堆上分配。考虑下面的情况:

This division of functions makes sense when you realize that not all objects are allocated on the heap. Consider the following case:

int main() {
    string someString;
    ..
}

局部变量不是动态分配的; new 未使用;但是对象仍然需要初始化,因此构造函数仍然被调用。注意,你不需要显式地调用构造函数 - 它是隐式的语言,一个适当的构造函数将始终被调用来初始化一个对象,当它被创建时。

The local variable is not dynamically allocated; new is not used; however the object still needs to be initialized so the constructor is still called. Note that you did not need to explicitly call the constructor - it is implicit in the language that an appropriate constructor will always be called to initialize an object when it is created.

当你写一个新表达式,编译器知道发出指令来调用新的操作符(根据需要分配内存),然后调用构造函数(初始化对象)。无论是否重载新运算符,都会发生这种情况。

When you write a 'new expression', the compiler knows to emit instructions to invoke the new operator (to allocate memory as needed) and then to call the constructor (to initialize the object). This happens whether or not you overload the new operator.

这篇关于为什么重载new操作符是调用构造函数,即使我使用malloc内部重载函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆