我怎样才能确保必须在堆中分配类的对象? [英] How can I insure that the objects of a class must be allocated in the heap?

查看:66
本文介绍了我怎样才能确保必须在堆中分配类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




也许这是一个简单的问题。我如何确保必须在堆中分配

a类的对象?


谢谢,


推荐答案

Pe ****** *@gmail.com 写道:


也许这是一个简单的问题。我怎样才能确保一个类的对象必须在堆中分配?

谢谢,
Peng
Hi,

Maybe this is an simple question. How can I insure that the objects of
a class must be allocated in the heap?

Thanks,
Peng




将所有构造函数设为私有,然后将友元函数声明为

分配对象


class X

{

private:

X();

X(int,int);

X(const X&);

public:

...

朋友X * make_X(){return new X(); } $ / $
朋友X * make_X(int a,int b){return new X(a,b); }

朋友X * copy_X(X * rhs){return new X(* rhs); }

};


john



Make all the constructors private and then declare friend functions to
allocate objects

class X
{
private:
X();
X(int, int);
X(const X&);
public:
...
friend X* make_X() { return new X(); }
friend X* make_X(int a, int b) { return new X(a, b); }
friend X* copy_X(X* rhs) { return new X(*rhs); }
};

john


* Pe ******* @ gmail.com

也许这是一个简单的问题。我怎样才能确保必须在堆中分配类的对象?


有两种方法:(1)限制对构造函数的访问_and_为所有相关类中的所有相关构造函数提供

工厂函数,

或(2)限制访问您的析构函数。


(1)

包含在常见问题解答中 - 去查找! ;-)


(2)

我更喜欢(工作少,一劳永逸),

至少有人告诉我Big Drawback是什么,部分

涵盖在< url:

http://home.no的1.3.1节中。 net / dubjai / win32cpptut / special / pointers / preview / pointers_01__alpha3.doc.pdf>


除了讨论之外 - 这是一份未完成的文件 -

如果

一劳永逸的支持包含类似


的东西,你可能会成为一个更快乐,更有成效的程序员模板<

typename T,

void(* defaultDestructionFunction)(T *)=& :: callDelete< T>

Maybe this is an simple question. How can I insure that the objects of
a class must be allocated in the heap?
There are two ways: (1) restrict access to constructors _and_ provide
factory functions for all relevant constructors in all relevant classes,
or (2) restrict access to your destructor.

(1)
is covered in the FAQ -- go find! ;-)

(2)
which I like much better (less work, doing things once and for all), at
least until someone tells me what the Big Drawback is, is partially
covered in section 1.3.1 of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha3.doc.pdf>

In addition to the discussion there -- it''s an unfinished document --
you''d probably be a happier and more productive programmer if the
once-and-for-all support includes something like

template<
typename T,
void (*defaultDestructionFunction)(T*) = &::callDelete<T>



class SharedPtr:public boost :: shared_ptr< T>

{

private:

typedef boost :: shared_ptr< T>基础;

公开:

//普通建筑:

SharedPtr():Base(){}

SharedPtr(Base const& r)throw():Base(r){}

SharedPtr(T * p):Base(p,defaultDestructionFunction){}

template< typename Destroyer>

SharedPtr(T * p,Destroyer d):Base(p,d){}


//特殊转换构造:

模板< Y类>

SharedPtr(boost :: shared_ptr< Y> const& r)throw():Base(r){}

template< Y类>

显式SharedPtr(boost :: weak_ptr< Y> const& r):Base(r){}

模板< Y类>

显式SharedPtr(std :: auto_ptr< Y>& r):Base(r){}

};

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


class SharedPtr: public boost::shared_ptr<T>
{
private:
typedef boost::shared_ptr<T> Base;
public:
// Ordinary construction:
SharedPtr(): Base() {}
SharedPtr( Base const& r ) throw(): Base( r ) {}
SharedPtr( T* p ): Base( p, defaultDestructionFunction ) {}
template< typename Destroyer >
SharedPtr( T* p, Destroyer d ): Base( p, d ) {}

// Special converting construction:
template< class Y >
SharedPtr( boost::shared_ptr<Y> const& r ) throw(): Base( r ) {}
template< class Y >
explicit SharedPtr( boost::weak_ptr<Y> const& r ): Base( r ) {}
template< class Y >
explicit SharedPtr( std::auto_ptr<Y>& r ): Base( r ) {}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Pe*******@gmail.com 写道:
Pe*******@gmail.com wrote:


也许这是一个简单的问题。我怎样才能确保一个类的对象必须在堆中分配?
Hi,

Maybe this is an simple question. How can I insure that the objects of
a class must be allocated in the heap?




首先,我不太明白,为什么你想要做一些类似

的事情。


关于你如何去做,这里有一个想法:你可以制作

构造函数私有。现在,这听起来很愚蠢,因为它阻止了施工。

但是,你可以有一个朋友。这样,只有朋友类能够构建对象。如果那个朋友恰好只通过新的分配对象,那么你很好。例如:

#include< algorithm>

#include< iostream>


类句柄;


类禁止{


朋友类句柄;


int i;


禁止(int j = 0)

:i(j)

{

std :: cout<< " created\\\
";

}


~禁止(无效){

std :: cout<< ; sold\\\
;

}


禁止(禁止const& other);

禁止& operator =(forbidden const& other);


public:


int get(void)const {

return(i);

}


int set(int j){

i = j;

返回j;

}


}; //禁止上课


班级句柄{


禁止* ptr;


public:


句柄(int i = 0)

:ptr(新禁止(i))

{}


句柄(句柄const和其他)

:ptr(new forbidden(other.get()))

{}


~handle(无效){

删除ptr;

}


句柄& operator =(handle const& other){

handle dummy(other);

std :: swap(this-> ptr,dummy.ptr);

返回(* this);

}


int get(void)const {

return( ptr-> get());

}


int set(int i){

return(ptr-> ; set(i));

}


}; //句柄


int main(void){

句柄x(5);

std :: cout<< ; x.get()<< ''\ n'';

}

请注意,此句柄具有值语义。相反,很容易实现

引用语义。


如果你只是想允许指向禁止对象但不禁止的指示

对象,你可以做这样的事情:


#include< algorithm>

#include< iostream>


struct handle;


类禁止{


朋友类句柄;


int i;


禁止(int j = 0)

:i(j)

{

std :: cout<< " created\\\
";

}


禁止(禁止const& other);

禁止& operator =(forbidden const& other);


public:


int get(void)const {

return(i);

}


int set(int j){

i = j;

返回j;

}


~禁止(无效){

std :: cout<< destroyed\\\
;

}


}; //禁止上课


struct handle {


static

forbidden * new_forbidden(int i = 0){

返回(新禁止(i));

}


}; //句柄


int main(void){

forbidden * f_ptr = handle :: new_forbidden(5);

std :: cout<< f_ptr-> get()<< ''\ n'';

删除f_ptr;

}


无论如何,它似乎只是想要制作为您的代码客户提供更难的生活。一般来说,这不是一个好主意:下周,有人会发布关于如何规避你的措施的问题。

Best

Kai-Uwe Bux



First, I do not quite understand, why you would want to do something like
that.

As for how you could go about it, here is an idea: You can make the
constructors private. Now, that sounds silly as it prevents construction.
However, you can have a friend. This way, only the friend class is able to
construct objects. If that friend happens to only allocate objects via new,
then you are fine. E.g.:
#include <algorithm>
#include <iostream>

class handle;

class forbidden {

friend class handle;

int i;

forbidden ( int j = 0 )
: i ( j )
{
std::cout << "created\n";
}

~forbidden ( void ) {
std::cout << "destroyed\n";
}

forbidden ( forbidden const & other );
forbidden& operator= ( forbidden const & other );

public:

int get ( void ) const {
return ( i );
}

int set ( int j ) {
i = j;
return j;
}

}; // class forbidden

class handle {

forbidden* ptr;

public:

handle ( int i = 0 )
: ptr ( new forbidden ( i ) )
{}

handle ( handle const & other )
: ptr ( new forbidden ( other.get() ) )
{}

~handle ( void ) {
delete ptr;
}

handle & operator= ( handle const & other ) {
handle dummy ( other );
std::swap( this->ptr, dummy.ptr );
return ( *this );
}

int get ( void ) const {
return ( ptr->get() );
}

int set ( int i ) {
return( ptr->set(i) );
}

}; // handle

int main ( void ) {
handle x ( 5 );
std::cout << x.get() << ''\n'';
}
Note that this handle has value semantics. It is quite easy to implement
reference semantics instead.

If you just want to allow pointers to forbidden object but not forbidden
objects, you could do something like this:

#include <algorithm>
#include <iostream>

struct handle;

class forbidden {

friend class handle;

int i;

forbidden ( int j = 0 )
: i ( j )
{
std::cout << "created\n";
}

forbidden ( forbidden const & other );
forbidden& operator= ( forbidden const & other );

public:

int get ( void ) const {
return ( i );
}

int set ( int j ) {
i = j;
return j;
}

~forbidden ( void ) {
std::cout << "destroyed\n";
}

}; // class forbidden

struct handle {

static
forbidden* new_forbidden ( int i = 0 ) {
return( new forbidden ( i ) );
}

}; // handle

int main ( void ) {
forbidden* f_ptr = handle::new_forbidden ( 5 );
std::cout << f_ptr->get() << ''\n'';
delete f_ptr;
}

Anyway, it just seems like you want to make life harder for the clients of
your code. Generally, that is not a good idea: next week, someone will post
the question as to how your measures can be circumvented.
Best

Kai-Uwe Bux


这篇关于我怎样才能确保必须在堆中分配类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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