C++ 封装问题

  • 本文关键字:问题 封装 C++ c++
  • 更新时间 :
  • 英文 :


我正在尝试使用带有自定义比较功能的std::sort。为此,我有一个名为 trySort 的类。这是 trySort 类的示例代码:

trySort.h

class trySort {
public:
private:
    std::string sortThis;
};

尝试排序.cpp

bool sortThisAsc (const trySort & rhs , const trySort & lhs) {
    return lhs.sortThis > rhs.sortThis;
}

如果成员sortThis是私有的,这将引发错误。

如果我sortThis这样的公众成员......

trySort.h

class trySort {
public:
      std::string sortThis;
private:           
};

尝试排序.cpp

bool sortThisAsc (const trySort & rhs , const trySort & lhs) {
    return lhs.sortThis > rhs.sortThis;
}

。它会起作用。看起来好像我sortThis成为公众成员。

所以我的问题是:如果我这样做,我不是封装了我的数据吗?

如果是这样,除了将其作为公共成员之外,还有哪些其他可能的方法?

您可以将

operator >添加到trySort类中。

trySort.h
class trySort {
public:
    bool operator >(trySort const &other) {
        return sortThis > other.sortThis;
    }  
private:
    std::string sortThis;
}

如果您需要更大的灵活性(一些其他类型的比较),只需使用 getter 函数封装sortThis即可。

trySort.h
class trySort {
public:
    std::string const &getSortThis() const {
        return sortThis;
    }
private:
    std::string sortThis;
}

如果您绝对确定sortThis在项目过程中始终是不可变的,因此在构造函数中初始化,则可以将其保留为公共 const 成员,但在大多数情况下,这是非常推测的方法。

trySort.h
class trySort {
public:
    trySort(std::string const sortThis)
        : sortThis(sortThis) {
    }
    const std::string sortThis;
}

或者你可以用 friend ,但我会把它作为最后的手段,因为在大多数情况下,它代表过度的亲密关系(总有一些其他方法可以避免使用 friend s)。在某些情况下,使用 friend 可能是合适的,但它永远不会减少中断封装,不同之处在于friend子句向类客户端发出中断是故意的信号。

使用朋友:

class trySort {
friend bool sortThisAsc (const trySort & rhs , const trySort & lhs);
private:
       std::string sortThis;
}

它将仅提供对此功能的访问权限,而不能访问其他任何内容

至少有两种选择。您可以向此数据成员声明一个公共 getter,例如

class trySort {
public:
       const std::string & get_string() const { return sortThis; }
private:
       std::string sortThis;
}

然后在函数中排序ThisAsc 写入

bool sortThisAsc (const trySort & rhs , const trySort & lhs) {
    return lhs.get_string() > rhs.get_string();
}

或者你可以将函数声明为类的友元函数

class trySort {
public:
       friend bool sortThisAsc (const trySort & rhs , const trySort & lhs);
private:
       std::string sortThis;
}

在这种情况下,函数定义不会更改。

编辑 这摆脱了你提到的编译错误,但只有非成员函数可以传递给 std::sort(),所以这不太有效。

在 cpp 文件中,将函数定义从

bool sortThisAsc (const trySort & rhs , const trySort & lhs) {

bool trySort::sortThisAsc(const trySort & rhs, const trySort & lhs) {

如果没有trySort::函数就不会在trySort类范围内定义(即在该类之外),因此如果您尝试访问 trySort 中的私有变量,编译器会抱怨。

这样,您可以按照最初要求将其保留为私有方法。

最新更新