安置新的,这是可以接受的吗? [英] placement new , is this acceptable?

查看:90
本文介绍了安置新的,这是可以接受的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class MyClass

{

static void * Allocate(int size);

static void Free(void * mem);

static MyClass * CreateInstance();

void DestroyInstance();

};


MyClass * MyClass :: CreateInstance()

{

void * p = Allocate(sizeof(MyClass));


返回new( p)MyClass;

}


void MyClass :: DestroyInstance()

{

MyClass * i = this;

i-> ~MyClass();

免费(i);

}

class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}

推荐答案



" lallous" < LA ***** @ lgwm.org>在留言中写道

news:c0 ************* @ ID-161723.news.uni-berlin.de ...

"lallous" <la*****@lgwm.org> wrote in message
news:c0*************@ID-161723.news.uni-berlin.de...
class MyClass
{
static void * Allocate(int size);
static void Free(void * mem);
static MyClass * CreateInstance();
void DestroyInstance ();
};

MyClass * MyClass :: CreateInstance()
{
void * p = Allocate(sizeof(MyClass));


如果p == 0怎么办?

返回新的(p)MyClass;
}

void MyClass: :DestroyInstance()
{
MyClass * i = this;
i-> ~MyClass();
Free(i);
}
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));
what if p == 0?

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}




谁调用CreateInstace和DestroyInstance?两者都是私人的。


这可以使用,但为什么?为类提供自定义

分配工具的标准方法是提供自定义成员

运算符new和delete。有时候使用工厂函数而不是公共的

构造函数设为私有?

Jonathan



Who calls CreateInstace and DestroyInstance? Both are private.

This can be made to work, but why? The standard way to provide custom
allocation facilities for a class is to provide custom member
operators new and delete. Using a factory function instead of a public
constructor is appropriate sometimes, but is usually independent of
the decision to provide custom allocation. Also, why make the
constructor private?

Jonathan


" Jonathan Turkanis" < TE ****** @ kangaroologic.com>在消息中写道

news:c0 ************* @ ID-216073.news.uni-berlin.de ...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c0*************@ID-216073.news.uni-berlin.de...

lallous < LA ***** @ lgwm.org>在消息中写道
新闻:c0 ************* @ ID-161723.news.uni-berlin.de ...

"lallous" <la*****@lgwm.org> wrote in message
news:c0*************@ID-161723.news.uni-berlin.de...
class MyClass
{
static void * Allocate(int size);
static void Free(void * mem);
static MyClass * CreateInstance();
void DestroyInstance();
};

MyClass * MyClass :: CreateInstance()
{
void * p = Allocate(sizeof(MyClass));
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));



如果p == 0怎么办?



what if p == 0?


返回新的(p)MyClass;
}

void MyClass: :DestroyInstance()
{
MyClass * i = this;
i-> ~MyClass();
Free(i);
}

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}



谁调用CreateInstace和DestroyInstance?两者都是私人的。

这可以起作用,但为什么呢?为类提供自定义
分配工具的标准方法是提供自定义成员运算符new和delete。有时使用工厂函数而不是公共构造函数是合适的,但通常独立于提供自定义分配的决定。另外,为什么要将
构造函数设为私有?

Jonathan



Who calls CreateInstace and DestroyInstance? Both are private.

This can be made to work, but why? The standard way to provide custom
allocation facilities for a class is to provide custom member
operators new and delete. Using a factory function instead of a public
constructor is appropriate sometimes, but is usually independent of
the decision to provide custom allocation. Also, why make the
constructor private?

Jonathan



你好


考虑所有方法public,并将实例创建为:


MyClass * a = MyClass :: CreateInstance();

a-> DestroyInstance() ;


什么是工厂功能?你能提供链接吗?


-

Elias


Hello

Consider all method public, and an instance is created as:

MyClass *a = MyClass::CreateInstance();
a->DestroyInstance();

What is a factory function? can you provide links?

--
Elias


" lallous" < LA ***** @ lgwm.org>写了
"lallous" <la*****@lgwm.org> wrote
类MyClass
{
static void * Allocate(int size);
static void Free(void * mem);
static MyClass * CreateInstance ();
void DestroyInstance();
};

MyClass * MyClass :: CreateInstance()
{
void * p = Allocate(sizeof) (MyClass));

返回new(p)MyClass;
}
void MyClass :: DestroyInstance()
{
MyClass * i = this;
i-> ~MyClass();
免费(i);
}
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}




除了事实上,这是一种过于复杂的做事方式,你的b $ b方法并不支持继承。你如何分配/取消分配一个实例

派生自MyClass的类?


Claudio Puviani



Aside from the fact that this is an overly convoluted way to do things, your
approach doesn''t support inheritance. How do you allocate/deallocate an instance
of a class derived from MyClass?

Claudio Puviani


这篇关于安置新的,这是可以接受的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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