當編碼被緊緊功能耦合類,但如果你想有一個簡單的界面,在世界其他地區,這將是整齊的,如果我可以做這樣的事情:允許C++類訪問其他類的一些「內部」成員,但不是私有的?
class log
{
private:
log_context& cont;
public:
create_log_section(std::string name)
{
cont.create_log_section(name);// this is fine! It's "internal"
cont.this_is_private(); // doesn't compile. Don't want anyone touching my privates!
}
};
class log_context
{
internal:
void create_log_section(std::string name);
private:
void this_is_private();
internal friend log; // Wow, possible?
}
現在,這將允許日誌訪問相關部分的背景,但不是私人部分。程序的其餘部分應該使用日誌來添加任何上下文。它也可以在日誌之間傳遞強類型log_contexts
,而不需要任何額外的功率。我意識到這個特殊的解決方案是不可能的,但是如果有的話,還有哪些常見的解決方案呢?