如果我们想使用malloc分配内存,如何显式调用构造函数? [英] How to call a constructor explicitly if we want to allocate memory using malloc ?

查看:285
本文介绍了如果我们想使用malloc分配内存,如何显式调用构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些地图作为数据变量的A类。我通过使用malloc分配内存来在堆上创建类A的

对象。这个

将返回我所需的内存,但是对象没有被初始化

正确,因为构造函数不会被调用(根据行为)。


如果我们想使用malloc分配内存

,如何显式调用构造函数?

I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?

推荐答案

shsingh写道:
shsingh wrote:

我有一个包含一些地图作为数据变量的A类。我通过使用malloc分配内存来在堆上创建类A的

对象。这个

将返回我所需的内存,但是对象没有被初始化

正确,因为构造函数不会被调用(根据行为)。


如果我们想使用malloc分配内存

,如何显式调用构造函数?
I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?



你的创建对象是用新的堆,而不是malloc。


A * pA =新的A;


用删除释放内存和对象。


删除pA;

Your create object on the heap with new, not malloc.

A *pA = new A;

You free the memory and object with delete.

delete pA;


shsingh写道:
shsingh wrote:

我有课包含一些地图作为数据变量。我通过使用malloc分配内存来在堆上创建一个类A的对象
对象。

这将返回我所需的内存,但对象不是

正确初始化,因为构造函数不会被调用(根据

行为)。


如果我们想要如何显式调用构造函数使用malloc分配内存


I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc".
This will return me the required memory but the object is not
initialized properly as constructor same is not get called ( as per
the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?



使用placement new (查阅)。


V

-

请在回复e时删除资金'A' -mail

我没有回复最热门的回复,请不要问

Use "placement new" (look it up).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




shsingh写道:

shsingh wrote:

我有一个包含一些地图作为数据变量的A类。我通过使用malloc分配内存来在堆上创建类A的

对象。这个

将返回我所需的内存,但是对象没有被初始化

正确,因为构造函数不会被调用(根据行为)。


如果我们想使用malloc分配内存

,如何显式调用构造函数?
I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).

How to call a constructor explicitly if we want to allocate memory
using malloc ?



你可以使用新的位置。


buffer = malloc(/*...*/);

object = new(buffer)MyClass(/*...*/);


但是后来你也必须明确地调用析构函数:


object-> ~MyClass();

免费(缓冲区);


问候,

Bart。

You can use placement new.

buffer = malloc(/*...*/);
object = new (buffer) MyClass(/*...*/);

But later you have to call the destructor explicitly too:

object->~MyClass();
free(buffer);

Regards,
Bart.


这篇关于如果我们想使用malloc分配内存,如何显式调用构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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