我的团队对于特定上下文的指针容器使用存在一些分歧。请考虑:
int main() {
// Top level. This is an important fact to the context
// i.e. that the following instance is at this level
// so that its members are essentially at program scope.
MainClass mainClass;
mainClass.run();
}
// A instance of a class derived from Buffer does something very complex
// (it has various handles to resources/allocated memory) so that you would
// never want to copy an instance.
class Buffer;
class MainClass {
#make_decision_here_based_on_stack_overflow_responses
shared_ptr<Buffer> buffer; // 1
scoped_ptr<Buffer> buffer; // 2
#end_decision
MainClass() {
// non-trivial initialisation of buffer with any of a number of derived classes:
buffer.reset( ... )
}
void run() {
#make_decision_here_based_on_stack_overflow_responses
SomeSubservientClass sub(*buffer); // a
SomeSubservientClass sub(buffer.get()); // b
SomeSubservientClass sub(buffer); // c
#end_decision
sub.do_stuff()
}
};
(我希望你能理解这里实际上并不存在的特殊预处理器代码,但如果它存在就好了:)
我们目前拥有的代码处于状态“1b”(shared_ptr 成员,传递裸 ptr),但我们认为这不是应该的样子。我很想知道任何人乍一看认为是最自然/合理和最安全的事情和理由。或者,如果有人想建议“3”或“d”。我自己有一个意见,但还不想分享。