简体   繁体   English

请解释一下如何实现

[英]Please explain how this is implemented

What I've seen and been taught about encapsulation is that we can have data members as private and member functions are public. 我所看到和被教授的关于封装的内容是我们可以将数据成员作为私有,成员函数是公共的。

But C++ Primer defines encapsulation as: 但是C ++ Primer将封装定义为:

Separation of implementation from interface; 从接口分离实现; encapsulation hides the implementation details of a type. encapsulation隐藏了类型的实现细节。 In C++, encapsulation is enforced by putting the implementation in the private part of a class. 在C ++中,通过将实现放在类的私有部分来强制执行封装。

The last line is the confusing part: putting the implementation in the private part of a class . 最后一行是令人困惑的部分: 将实现放在类的私有部分中 By implementation, he means the function definitions, right? 通过实现,他意味着函数定义,对吗? I mean, I've never seen a function declared public (as prototype) and implemented in private. 我的意思是,我从未见过一个声明为public(作为原型)并在私有中实现的函数。

I'm really confused. 我真的很困惑。 Please explain what he's trying to say with a simple example. 请用一个简单的例子来解释他想说的话。

The concept being explained is more abstract than you're thinking about it. 被解释的概念比你想到的更抽象。 The "interface" is "what callers expect the object to do". “接口”是“呼叫者期望对象做什么”。 And the implementation is technically how the class actually carries out those operations. 从技术上讲,实现是如何实际执行这些操作的。

So for example, take a List class. 例如,拿一个List类。 Code that uses a List should only care about its interface: things like addObject() , clear() , getSize() , sort() . 使用 List代码应该只关心它的接口: addObject()clear()getSize()sort()

But the implementation of List -- which callers should not care about -- may vary drastically on how to accomplish this. 但是List实现 - 呼叫者不应该关心 - 可能在如何实现这一点上有很大的不同。 For example the data could be stored as a linked list. 例如,数据可以存储为链表。 Or maybe as a dynamic array. 或者可能是动态数组。 This is "private" implementation details that only the List class needs to care about. 这是“私有”实现细节,只有List类需要关注。 There will indeed probably be a private: method called reallocate() or so. 确实可能有一个名为reallocate()private:方法。 Callers should never use this; 来电者不应该使用这个; it's an implementation detail -- not part of the public interface. 它是一个实现细节 - 不是公共接口的一部分。 Callers should not care how the List allocates its storage. 调用者不应该关心List如何分配其存储。

Similarly, a sort() method implies it will sort objects in the list. 类似地, sort()方法意味着它将对列表中的对象进行排序。 That's the interface. 那就是界面。 But the implementation may use any number of algorithms to perform the sort. 但是实现可以使用任意数量的算法来执行排序。 The implementation is a private detail - and may be done in private methods. 实现是一个私人细节 - 可以用私人方法完成。 In any case it's hidden from callers. 在任何情况下,它都对呼叫者隐藏。

template<typename T>
class List
{
public:
    // public members are part of the interface: the externally-visible behavior "contract" your object guarantees.
    void addObject(const T& obj)
    {
        // call private methods to help carry out implementation.
        ensureCapacity(this->sizeAllocated + 1);
        this->sizeAdvertized += 1;
        // ... and copy the object into the right position in this->data...
    }

    size_t getSize() const
    {
        return this->sizeAdvertized;
    }

    // ... other members like clear(), sort()...

private:
    T* data;
    size_t sizeAllocated;
    size_t sizeAdvertized;

    // this needs to be private because it's not part of the interface. Callers
    // should never need to call this.
    void ensureCapacity(size_t requestedCapacity)
    {
        if(sizeAllocated >= requestedCapacity)
            return;// already have enough storage available.

        // ... reallocate this->data, copy existing items to the new array...

        this->sizeAllocated = requestedCapacity;
    }
};

And finally to address what you said: 最后要解决你说的话:

I mean, I've never seen a function declared public (as prototype) and implemented in private. 我的意思是,我从未见过一个声明为public(作为原型)并在私有中实现的函数。

"Private" again is more abstract than you're thinking. “私人”再次比你想象的更抽象。 The implementation of a class is basically always "private" in that it's hidden from callers. 类的实现基本上总是 “私有”,因为它对调用者是隐藏的。 It doesn't necessarily mean the private: access specifier. 它不一定意味着private: access说明符。 It just means that it's not exposed to callers. 它只是意味着它不会暴露给呼叫者。 Implementation of public methods fits this description. 公共方法的实施符合这种描述。

You can indeed have private functions (methods) in a class. 你确实可以在一个类中拥有私有函数(方法)。 Consider: 考虑:

class X 
{
public:
  // public interface - this never changes and you provide documentation for it
  void do_something();

private:
  // this private interface is likely to change. consumers of this class
  // are prevented from calling these methods unless they are friends
  void first_private_thing();
  void second_private_thing();

};

// definition
void X::do_something()
{
  // private implementation in public method
  first_private_thing();
  second_private_thing();
}

there are further extensions on this theme, for example: 这个主题还有进一步的扩展,例如:

class Base
{
public:
  // public non-virtual interface
  void do_something();

private:
  // calls private virtual implementation
  virtual void do_something_impl() = 0;

};

// definition:
void Base::do_something()
{
  do_something_impl();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM