API设计选择指针与对象 [英] API design choice pointer vs. object

查看:57
本文介绍了API设计选择指针与对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在设计一个API,而我遇到的问题更多的是一个

设计问题。在我的API中说我有A级和B级,如下所示


A级{

公开:

void doSomethingWithB (B * b)

{

//做一些事情b

//可能存储在列表中

listB.push_back(b);

}

private:

std :: vector< B *> listB;

};


B级

{

///一些声明和/或数据成员

};


现在问题是doSomethingWithB的界面可能是


void doSomethingWithB (B * b)



void doSomethingWithB(B& b)


如果我使用指针,他们会随附所有混乱,然后我可能不得不

做一些引用计数他们告诉别人我是

存储指向B的指针并可能调用delete或unref on

如果引用计数变为0,我将它们从列表中删除。这为上面的部分增加了很多

的内存管理开销/代码。


如果我使用引用而不是指针,那么可能我没有给API的用户充分的灵活性

。如果我不提供指针界面,我不太确定它会有什么优点。


坦率地说,直到现在我差不多用于成员

对象,方法参数,方法返回类型的指针。而且我觉得这很糟糕。


如果人们可以提供/反对指针/参考的原因

界面那么它会有很多帮助。


谢谢,

Divick

Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWithB( B * b)
{
//do something with b
//possibly store in a list
listB.push_back(b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWithB could be

void doSomethingWithB( B * b)
or
void doSomethingWithB( B & b)

If I use pointers they come with all the mess as then I might have to
do some sort of reference counting on them to tell somebody that I am
storing the pointer to B and possibly call delete or unref on them when
I remove them from the list if the ref count goes to 0. This adds lots
of memory management overhead /code to the piece above.

If I use reference rather then pointers, then probably I am not giving
the user of the API the full flexibility. I am not very sure that what
pros /cons will it have if I don''t provide a interface with pointers.

Frankly until now I almost used to use pointers everywhere for member
objects, for method parameters, method return type. And this I feel is
bad.

If people can provide reasons for/against the pointer/reference
interface then it would help a lot.

Thanks,
Divick

推荐答案

Divick写道:
[..]
如果人们可以提供/反对指针/参考
接口的原因那么它会有很大的帮助。
[..]
If people can provide reasons for/against the pointer/reference
interface then it would help a lot.




你能说,Google? 指针与参考讨论是这样的,我几乎看不到鞭子上的tracks $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ dead dead dead <<<<<<<<<< >
V

-

请在邮寄回复时从我的地址中删除资金



Can you say, "Google"? "Pointer versus reference" discussion is such
a dead horse that I can barely see the tracks in the dust where the whip
hit it last time we were beating it.

V
--
Please remove capital As from my address when replying by mail




Divick写道:

Divick wrote:

我正在设计一个API,而我遇到的问题更多的是设计问题。在我的API中说我有A级和B级,如下所示

A级{
公开:
void doSomethingWithB(B * b)
{
//
//可能存储在列表中
listB.push_back(b);
}
私有:
std :: vector< B *> listB;
};

B类
{
///一些声明和/或数据成员
};
现在的问题是doSomethingWithB的界面可能是

void doSomethingWithB(B * b)

void doSomethingWithB(B& b)
Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWithB( B * b)
{
//do something with b
//possibly store in a list
listB.push_back(b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWithB could be

void doSomethingWithB( B * b)
or
void doSomethingWithB( B & b)




B是多态的吗?如果是,你有答案,那一定是前者。


你想保留B或实际B本身的副本吗?如果副本那么

它可以是后者,但必须是前者。


对于参考计数等,如果你有选择你应该使用

Boost'的shared_ptr。如果没有,那就创建一个...这不是太难了。



Is B polymorphic? If yes you have your answer, it must be the former.

Do you want to keep copies of B or the actual B itself? If copies then
it can be the later but otherwise must be former.

For reference counting and such if you have the option you should use
Boost''s shared_ptr. If not then create one...it isn''t too tough.


Noah Roberts< ro ********** @ gmail.com>写道:
Noah Roberts <ro**********@gmail.com> wrote:

Divick写道:

Divick wrote:
现在问题是doSomethingWithB的界面可能是

void doSomethingWithB(B * b )

void doSomethingWithB(B& b)
Now the problem is either the interface to doSomethingWithB could be

void doSomethingWithB( B * b)
or
void doSomethingWithB( B & b)



B是多态的吗?如果是,你有答案,那一定是前者。



Is B polymorphic? If yes you have your answer, it must be the former.




我认为多态也适用于引用:

#include< iostream> ;


A级{

公开:

虚拟~A(){}

虚拟void do_something(){std :: cout<< " A :: do_something()\\\
英寸; }

};


B级:公开A {

公开:

虚拟空虚do_something (){std :: cout<< " B :: do_something()\\\
英寸; }

};


void do_it(A& a)

{

a.do_something ();

}


int main()

{

B b;

do_it(b);


返回0;

}

输出:

B :: do_something()

-

Marcus Kwok



I think polymorphism works with references too:
#include <iostream>

class A {
public:
virtual ~A() { }
virtual void do_something() { std::cout << "A::do_something()\n"; }
};

class B : public A {
public:
virtual void do_something() { std::cout << "B::do_something()\n"; }
};

void do_it(A& a)
{
a.do_something();
}

int main()
{
B b;
do_it(b);

return 0;
}
Output:
B::do_something()
--
Marcus Kwok


这篇关于API设计选择指针与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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