关于智能指针的快速问题 [英] Quick question on smart pointers

查看:61
本文介绍了关于智能指针的快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我只是在阅读有关智能指针的内容..

我有一些现有的C代码,我想提供包装类

for。具体来说,我想为两个stucts提供包装

定义为ff:


typedef struct {

float * data;

int count;

}系列;


typedef struct {

void * clientdata;

FUNC_PTR1 cb1;

FUNC_PTR2 cb2;

struct param_ pdata;

int size;

}客户

我正在考虑为第一个提供包装为ff:


class Series {

std :: auto_pointer<系列> sPtr;

....

}


几个快速问题:


1)。我可以这样做(我可能应该检查一下是否会首先编译


2)。根据我对auto_ptr的理解,当类系列超出范围时,变量sPtr会自动释放

(我无法想象

这是可能的)

3)。上面两个语句的回复是否适用于像上面的客户那样更加复杂的结构?


谢谢

Hi,

I''m just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined as ff:

typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client
I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_pointer<series> sPtr ;
....
}

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can''t imagine how
this is possible)
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?

Thanks

推荐答案

Susan Baker写道:
Susan Baker wrote:



我只是在阅读有关智能指针的内容..
我有一些现有的C代码,我想提供包装类
。具体来说,我想提供两个结构的包装器
定义为ff:

typedef struct {
float * data;
int count;
}系列;

typedef struct {
void * clientdata;
FUNC_PTR1 cb1;
FUNC_PTR2 cb2;
struct param_ pdata;
int size;
}客户

我正在考虑为第一个提供包装作为ff:

类系列{
标准:: auto_pointer< series> sPtr;
....
}


嗯,你可以做到。但它可能更容易将其更改为


typedef std :: vector<浮动>系列;


导致整个结构看起来非常像编码动态的C方式

可调整大小的数组。

几个快速问题:

1)。我可以这样做(我可能应该检查一下它是否会首先编译)


你可以做到。

但是我推荐首先退一步问一个问题:这个结构的目的是什么?
?和有一个共同的C ++方法来实现这个目的吗?

2)。根据我对auto_ptr的理解,当类系列超出范围时,变量sPtr会自动释放(我无法想象
这是可能的)


它很容易完成:

std :: auto_ptr是一个像任何其他对象一样的对象。当它超出范围时,它的

析构函数被调用。现在猜猜析构函数是做什么的。

3)。对于像上面的客户这样更复杂的结构,前两个陈述的回答是否适用?

Hi,

I''m just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined as ff:

typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client

I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_pointer<series> sPtr ;
....
}
Well, You could do that. But it would probably be easier to change it to

typedef std::vector< float > Series;

cause the whole structure looks extremely like the C way to code a dynamicaly
resizable array.

Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
You can do it.
But I recommend to first take a step back and ask a question: "What is the purpose
of that struct?" and "Is there a common C++ method to achieve that purpose?"
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can''t imagine how
this is possible)
It is done easily:
A std::auto_ptr is an object like any other object. When it goes out of scope, its
destructor is called. And now guess what that destructor does.
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?




您正在寻找一本烹饪书籍食谱。我不认为你会找到一个。

这使编程有时令人沮丧,但另一方面

这使编程变得有趣:一个尺寸适合所有很少

适用于编程。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


>我正在考虑为第一个提供包装作为ff:
> I am thinking of providing a wrapper for the first one as ff:

类系列{
std :: auto_pointer< series> sPtr;
....
}


这很容易出错。考虑:


系列s1;

系列s2 = s1; // !!!

几个简单的问题:

1)。我可以这样做(我可能应该检查一下它是否会先编译)


你真的想要/需要使用智能指针吗?只需使用简单的RAII

与私人拷贝构造函数和assingment运算符。

2)。根据我对auto_ptr的理解,当类系列超出范围时,变量sPtr会自动释放(我无法想象
这是可能的)


魔术被称为析构函数(它只是Java程序员的魔力)。

3)。对于像上面的客户这样更复杂的结构,前两个陈述的回答是否适用?

class Series {
std::auto_pointer<series> sPtr ;
....
}
This is very error prone. Consider:

Series s1;
Series s2 = s1; //!!!
Couple of quick questions:

1). Can I do this (I probably should have checked to see if it would
compile first)
Do you really want/need to use a smart pointer? Just use plain RAII
with private copy constructor and assingment operator.
2). From my understanding of auto_ptr, variable sPtr gets freed
automagically when class Series goes out of scope (I can''t imagine how
this is possible)
The magic is called destructor (it''s magic only for Java programmers).
3). Will the responses for the previous two statements hold true for a
more complicated structure like client above?




如果你保持''创建者是删除''规则很多''问题''在C ++中消失了





If you maintain the ''creator is deleter'' rule many ''problems'' disappear
in C++.


Susan Baker< sb **** @ no。 spam.net> schrieb:
Susan Baker <sb****@no.spam.net> schrieb:
typedef struct {
float * data;
int count;
} series;

typedef struct {
void * clientdata;
FUNC_PTR1 cb1;
FUNC_PTR2 cb2;
struct param_ pdata;
int size;
} client
我正在考虑为第一个提供包装作为ff:

类系列{
标准:: auto_pointer< series> sPtr;
....
}
typedef struct {
float *data ;
int count ;
} series ;

typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client

I am thinking of providing a wrapper for the first one as ff:

class Series {
std::auto_pointer<series> sPtr ;
....
}




我不明白你的计划。为什么不使用真正的类

而不是系列结构? auto_ptr应该在这里做什么?


我建议不要使用这个auto_ptr的东西。这是为那些's
右手的人不知道左手在做什么。你总是可以在你的班级中有一个简单的指针,并自己删除它。这是

destructors的用途。


T.M.



I don''t understand what you are planning. Why not use a real class
instead of the series struct? What should the auto_ptr do here?

I suggest not to use this auto_ptr stuff. This is for people who''s
right hand doesn''t know what the left hand is doing. You can always
have a simple pointer in your class and delete it yourself. This is
what destructors are made for.

T.M.


这篇关于关于智能指针的快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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