属性函数样式约定受 void* 影响
我正在用 C++ 设计一个公共 API,并且相信我想保留 C++ 属性函数样式约定,类似于 int& Value() 和 const int& Value() const
与按值返回/分配的 get/set 前缀相反,因为我觉得使用模式更简洁且同样具有可读性,同时很容易混合到现有的 C++ 代码中。
我需要允许程序员提供他自己的元数据。我选择通过公开 void*
属性函数来实现此目的。问题当然是具有如下签名:
class foo {
int& Value();
const int& Value() const;
void* Metadata()
void* const Metadata() const
};
...不起作用,因为以下内容无法编译:
void* ptr = ...;
foo.Metadata() = ptr;
因此,我必须使用类似以下内容才能使其工作:
class foo {
int& Value();
const int& Value() const;
void* GetMetadata();
void SetMetadata(void* const data);
};
但这会导致样式不一致,所以现在我选择在整个 API 中坚持使用 get/set 前缀,因为,例如:
class foo {
int GetValue() const;
void SetValue(int value);
void* GetMetadata() const;
void SetMetadata(void* const data);
};
当然,这不符合我想要使用的语言约定。整个事情还有其他方法吗?如果是这样,那是什么?我是否受困于 get/set 前缀?
注意:我无法将 void*
替换为用户元数据模板,因为 API 应尽可能稳定。
此外,我已经为用户字符串提供了一个类型为无符号 char*
的字符串 get/set 对。
更新: 经过仔细(重新)考虑后,我选择坚持使用 get/set 前缀,我也不会使用 void*
的引用 - 如果 void*
发出闻起来,void*&
几乎就是一个公共垃圾填埋场。因此,我基本上选择了与此类似的内容:
typedef void* Any;
class foo {
...
Any GetObject() const;
void SetObject(Any);
...
};
感谢您的意见。 :)
I'm designing a public API in C++ and believe I'd like to retain C++ property-function style conventions that look like int& Value()
, and const int& Value() const
as opposed to get/set prefixes which return/assign by value because I feel the usage patterns are more concise and equally readable, while blending into existing C++ code very easily.
I need to allow the programmer to supply his own metadata. I've chosen to do so by exposing a void*
property function. The problem is of course that having signatures like:
class foo {
int& Value();
const int& Value() const;
void* Metadata()
void* const Metadata() const
};
...doesn't work because the following won't compile:
void* ptr = ...;
foo.Metadata() = ptr;
Because of this, I would have to use something like the following to make it work:
class foo {
int& Value();
const int& Value() const;
void* GetMetadata();
void SetMetadata(void* const data);
};
But that would render the styles inconsistent, so for now I've opted to stick with get/set prefixes throughout the entire API because of that, eg:
class foo {
int GetValue() const;
void SetValue(int value);
void* GetMetadata() const;
void SetMetadata(void* const data);
};
Of course, that doesn't fit the language convention I'd like to use. Is there an alternative approach to this whole thing? If so, what is it? Am I stuck with get/set prefixes?
Note: I can't replace void*
with templates for user meta-data as the API should be as stable as possible.
Also, I have already supplied a string get/set pair typed unsigned char*
for user strings.
UPDATE:
After some careful (re)consideration I've opted to stick with get/set prefixes, I also won't use references for void*
- if void*
gives off a smell, void*&
is pretty much a public landfill. As such, I've essentially opted for something similar to this:
typedef void* Any;
class foo {
...
Any GetObject() const;
void SetObject(Any);
...
};
Thanks for your input. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并不是说我确信这是最好的设计,但是,坚持这个问题:如果对于“int”类型的属性,您编写:
对“void *”类型的属性执行相同的操作:
Not that I'm sure this is the best design but, sticking to the question: If, for an attribute of type "int", you write:
do the same for an attribute of type "void*":
您可以让您的
Metadata
函数返回对指针的引用:另一种选择是使用重载:
如果您采用这种方法,您还可以让 setter 返回旧值以方便使用。
You could have your
Metadata
functions return references to pointers:Another alternative is to use overloading:
If you go this route, you could also make the setters return the old value for convenience.