的eC(Ecere)如何不担心类的私有数据域 [英] eC (Ecere) how to not worry about private data fields of a class

查看:166
本文介绍了的eC(Ecere)如何不担心类的私有数据域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在暴露的C ++(或Java)文库的接口,人们必须提供类的私人字段,这是understable,因为编译器需要知道类的结构中,为了能够来计算,例如,的sizeof()

但是,为什么这是需要怎么可能得到缓解?因为,对我来说,它出现在封装概念违约:为什么用户要担心或有权访问被认为是私人的东西。

的一个解决方案是定义为每个对象的尺寸()函数,但是这将是在运行时的负担。

不过,一种语言(EC / ecere)声称,[1]:

库开发者不需要担心类定义的私有内容由最终用户,只是什么声明为public将可见被视为

这怎么欧共体实现,怎么可能类似于Java或C来实现++?

[1] http://www.ecere.com/technologies.html


解决方案

仅仅因为程序员或编译器可以看到一个私人的类型,并不意味着它违反了封装。考虑封装为一个合同(你不应该使用它,但你仍然可以看到它)。

... ...但是...

在回答你的问题,如果你真的想躲底层重新presentation,是使用不透明的指针:

下面是在C ++中的例子:


  

http://www.tilander.org/aurora2/Stupid_Cpp_Tricks/index.html


  
  

一个早期的书,我对C ++买是詹姆斯Coplien的酸书
  (正如迈尔斯调用它)。大部分在那里的东西是越来越多的面包
  和奶油的东西,虽然它你没有看过它,你应该。其中之一
  事詹姆斯(吉姆或者,怎一个名字的漂亮是)推出了
  平普尔IDOM。私有实现是一个快乐的跨pretation
  怪异的名称,更可信的是指针的实现。在
  简单来说,它是一个编译器的防火墙,或不透明类型的
  有效地隐藏任何类别的从外部的执行


 在头//
Foo类
{
上市:
    美孚();
    〜美孚();私人的:
    结构平普尔; //向前声明的内部结构
    平普尔·米; //不透明指针实际数据
};//在cpp文件
结构美孚::平普尔
{
    性病::字符串名称;
};富::富()
    :M(新平普尔)
{
}富::〜美孚()
{
    删除米;
}

In exposing the C++ (or Java) interface of a library, one has to provide the "private" fields of classes, and this is understable, because the compiler needs to know the structure of the class, in order to be able to compute, for example, sizeof().

But why this is needed and how could be alleviated? Because, for me, it appears as a breach in the encapsulation concept: why the user shall worry or have access to something that is considered to be private?

One solution would be to define a size() function for each object, but this will be burdensome at runtime.

Still, one language (eC/ecere) claims that [1]:

"Library developers don't need to worry about the private content of the class definition being seen by the end user, only what is declared public will be visible"

How is that achieved in eC and how could similar be implemented in Java or C++?

[1] http://www.ecere.com/technologies.html

解决方案

Just because the programmer or the compiler can "see" a private type, doesn't mean it violates "encapsulation". Consider encapsulation as a "contract" (you're not supposed to use it, but you can still see it).

... HOWEVER ...

the answer to your question, if you really want to "hide" the underlying representation, is to use opaque pointers:

Here's an example in C++:

http://www.tilander.org/aurora2/Stupid_Cpp_Tricks/index.html

One of the early books I bought on C++ was James Coplien's 'Acid Book' (as Meyers calls it). Much of the stuff in there is today more bread and butter things, although it you haven't read it, you should. One of the things James (or Jim, how nice of a name is that) introduced was the Pimpl idom. Private Implementation is a happy interpretation of the weird name, the more plausible is pointer to implementation. In simple terms it's a compiler firewall, or an opaque type that effectively hides the implementation of any class from the outside.

// in the header
class Foo
{
public:
    Foo();
    ~Foo();

private:
    struct Pimpl; // forward declaration to internal structure
    Pimpl* m; // opaque pointer to actual data
};

// in the cpp file
struct Foo::Pimpl
{
    std::string name;
};

Foo::Foo()
    : m( new Pimpl)
{
}

Foo::~Foo()
{
    delete m;
}

这篇关于的eC(Ecere)如何不担心类的私有数据域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆