首页 \ 问答 \ 问题与C ++中的代理模式(Issue with Proxy Pattern in c++)

问题与C ++中的代理模式(Issue with Proxy Pattern in c++)

我被告知在我的程序中使用代理模式,这对我来说不是很清楚。

我有一些Proxy &operator*() ,我不知道应该返回什么来获取文件中当前索引的值。

我之前有这个并且它有效:

int &operator*()
{
    return ptr->getValue(index);
}

但我被告知要将其更改为Proxy

如果我使用Proxy &operator*()进行编译,那么我就会收到

错误:从'int'类型的表达式'IntFile :: Proxy&'类型的引用的无效初始化

完整代码:

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

class IntFile
{
public:
    int value;
    FILE* file;

    IntFile()
    {
        file = fopen("text.txt", "r+b");
    }

    ~IntFile()
    {
        fclose(file);
    }

    virtual int& getValue(int index)
    {
        fseek(file, 4*index, SEEK_SET);
        fread(&value, 4, 1, file);
        return value;
    }

    virtual int setValue(int value)
    {
        fseek(file, 0, SEEK_CUR);
        fwrite(&value, 4, sizeof(value),file);
        return 0;
    }

    class Proxy
    {
    private:
        IntFile *ptr;
        long index;
    public:
        Proxy(IntFile* ptr3)
        {
            ptr = ptr3;
        }
        Proxy& operator=(int value)
        {
            ptr->setValue(value);
        }
        operator int() const
        {
            return ptr->getValue(index);
        }
    };

    friend struct iterator;
    struct iterator
    {
        int index;
        int value2;
        IntFile* ptr;

        iterator(IntFile* ptr2, int idx, FILE* ptrfile)
        {
            ptr = ptr2;
            index = idx;
            fseek(ptrfile, 4*index, SEEK_SET);
        }

        bool operator==(const iterator&other) const
        {
            return index == other.index;
        }

        bool operator!=(const iterator&other) const
        {
            return index!=other.index;
        }

        Proxy &operator*()
        {
            // How to do that?
        }

        int &operator=(int value)
        {
            this->value2 = value;
        }

        iterator&operator++()
        {
            this->index = index+1;
        }

        iterator&operator--()
        {
            this->index = index -1;
        }
    };

    iterator begin()
    {
        return iterator(this, 0, file);
    }

    iterator end(int number)
    {
        return iterator(this, number, file);
    }

    iterator rbegin(int number)
    {
        return iterator(this, number-1, file);
    }

    iterator rend()
    {
        return iterator(this, -1, file);
    }
};

int main()
{
    IntFile myfile;

    int number;
    cout << "Enter number of elements: " << endl;
    cin >> number;

    vector <int> myVector;

    cout << "Enter your numbers: ";
    for ( int i = 0; i < number; i++)
    {
        cin >> myfile.value;
        myVector.push_back(myfile.value);
    }

    fwrite(&myVector[0], sizeof(vector<int>::value_type), myVector.size(),myfile.file);

    cout << endl << "FORWARD 1 by 1: " << endl;
    for (IntFile::iterator i = myfile.begin(); i != myfile.end(number); ++i)
    {
        cout << *i << " ";
    }

    cout << endl << "BACKWARD 1 by 1: " << endl;
    for (IntFile::iterator i = myfile.rbegin(number); i != myfile.rend(); --i)
    {
        cout << *i << " ";
    }

    cout << endl;
    return 0;
}

I was told to use proxy pattern in my program which is not that clear to me.

I have some issues with Proxy &operator*() , I don't know what should I return there to get a value of current index in file.

I had this before and it worked:

int &operator*()
{
    return ptr->getValue(index);
}

But I was told to change it to Proxy.

If I compile with Proxy &operator*() then I'm getting

error: invalid initialization of reference of type 'IntFile::Proxy&' from expression of type 'int'|

Full code:

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

class IntFile
{
public:
    int value;
    FILE* file;

    IntFile()
    {
        file = fopen("text.txt", "r+b");
    }

    ~IntFile()
    {
        fclose(file);
    }

    virtual int& getValue(int index)
    {
        fseek(file, 4*index, SEEK_SET);
        fread(&value, 4, 1, file);
        return value;
    }

    virtual int setValue(int value)
    {
        fseek(file, 0, SEEK_CUR);
        fwrite(&value, 4, sizeof(value),file);
        return 0;
    }

    class Proxy
    {
    private:
        IntFile *ptr;
        long index;
    public:
        Proxy(IntFile* ptr3)
        {
            ptr = ptr3;
        }
        Proxy& operator=(int value)
        {
            ptr->setValue(value);
        }
        operator int() const
        {
            return ptr->getValue(index);
        }
    };

    friend struct iterator;
    struct iterator
    {
        int index;
        int value2;
        IntFile* ptr;

        iterator(IntFile* ptr2, int idx, FILE* ptrfile)
        {
            ptr = ptr2;
            index = idx;
            fseek(ptrfile, 4*index, SEEK_SET);
        }

        bool operator==(const iterator&other) const
        {
            return index == other.index;
        }

        bool operator!=(const iterator&other) const
        {
            return index!=other.index;
        }

        Proxy &operator*()
        {
            // How to do that?
        }

        int &operator=(int value)
        {
            this->value2 = value;
        }

        iterator&operator++()
        {
            this->index = index+1;
        }

        iterator&operator--()
        {
            this->index = index -1;
        }
    };

    iterator begin()
    {
        return iterator(this, 0, file);
    }

    iterator end(int number)
    {
        return iterator(this, number, file);
    }

    iterator rbegin(int number)
    {
        return iterator(this, number-1, file);
    }

    iterator rend()
    {
        return iterator(this, -1, file);
    }
};

int main()
{
    IntFile myfile;

    int number;
    cout << "Enter number of elements: " << endl;
    cin >> number;

    vector <int> myVector;

    cout << "Enter your numbers: ";
    for ( int i = 0; i < number; i++)
    {
        cin >> myfile.value;
        myVector.push_back(myfile.value);
    }

    fwrite(&myVector[0], sizeof(vector<int>::value_type), myVector.size(),myfile.file);

    cout << endl << "FORWARD 1 by 1: " << endl;
    for (IntFile::iterator i = myfile.begin(); i != myfile.end(number); ++i)
    {
        cout << *i << " ";
    }

    cout << endl << "BACKWARD 1 by 1: " << endl;
    for (IntFile::iterator i = myfile.rbegin(number); i != myfile.rend(); --i)
    {
        cout << *i << " ";
    }

    cout << endl;
    return 0;
}

原文:https://stackoverflow.com/questions/47049524
更新时间:2024-02-09 08:02

最满意答案

我们刚刚为我们的团队评估了一堆库。

我们决定坚持使用ExtJS,尽管成本和许可证,因为它很好,这就是我们所知道的。

我们考虑过Vaadin,但与ExtJS相比,它还没有适用于小部件。 此外,他们的附加组件也很昂贵。 我喜欢它。 我喜欢它的纯Java方面。

Dojo / Dijit是我们的#2,如果我们没有ExtJS经验,我们就会去那里。 他们也有很好的maquetta设计师工具。

ExtJS当然拥有最好的图表,并且它们还具有用于高端地图支持的geoext。

ExtJS不是速度恶魔,但在现代浏览器上它很好。

您可能需要在列表中考虑KendoUI。


We just evaluated a bunch of libraries for our team.

We decided to stick with ExtJS, despite the cost and license, because it's good and it's what we know.

We considered Vaadin, but it's not quite there yet for widgets, compared to ExtJS. Also, their add-ons are expensive. I like it though. I like the pure Java aspect of it.

Dojo/Dijit was our #2, and we would have gone there if we didn't have ExtJS experience. They have the nice maquetta designer tool too.

ExtJS certainly has the best charts, and they also have geoext for high-end map support.

ExtJS is not known to be a speed demon, but it's fine on modern browsers.

You may want to consider KendoUI in your list.

相关问答

更多
  • 不,您可以静态链接大多数流行的GUI框架,包括MFC,Qt,ATL / WTL和wxWidgets。 我不了解GTK,但我认为你也可以静态链接它。 静态链接意味着您可以将代码直接链接到您的可执行文件,而不是动态链接到生活在DLL中的库代码,从而生成一个独立的EXE文件,无需任何外部依赖。 但是,当然,这些依赖关系仍然存在,它们仍然会膨胀可执行文件的大小,这可能是一个问题,具体取决于您的部署机制。 另外,对于接近金属的编程有一些要说的,所以直接使用Win32 API绝对是一种选择。 这将产生尽可能最小,最轻的 ...
  • 如果你正在寻找类似于JBoss的.NET的东西,那么不,在.NET世界中没有一种产品提供了全部的框架。 但是,有很多产品,主要是.NET 3.5堆栈本身,可以拼凑在一起提供一个坚实的框架。 Windows Communication Foundation(网络); Windows Workflow Foundation(工作流程); 企业库应用程序块(缓存,日志记录等); 对象关系映射器 实体框架; NHibernate的; LLBLGEN 控制/依赖注入的反转 统一 Spring.NET 温莎城堡 Nin ...
  • 首先,澄清一下:GWT主要是一个UI库 - 也就是说,你用Java编写你的界面,它将它转换为JavaScript,你可以在浏览器中运行它。 那说: 您使用的模式独立于库。 如果你使用GWT eclipse的项目创建者,它会将你的项目分成三个包(客户端,服务器和共享)......你可以从那里获取它。 GWT通常使用池模型,客户端向服务器请求数据。 但是你可以尝试一些推动 。 服务器端代码只是Java,不涉及GWT。 您可以在服务器上执行任何“java”操作,然后将其发送到客户端代码。 如果您在公司内部网上使用 ...
  • 一种方法,你可以做到这一点 另一个为桌面GUI应用程序实现的框架 是通过使用Sikuli 。 由于它采用图像识别技术,可跨所有桌面应用程序使用,也可由多种语言驱动并免费 。 为了满足这个答案,我会补充说,你可以将它与黄瓜和詹金斯结合起来,就像这里所示 。 更新 将脚本导出到Java 在这个意义上是不可能的,因为Sikuli脚本是用Python脚本语言编写的,而脚本本身是使用Jython解释器(Java语言的Python基础实现)运行的。 看到这个答案和这个答案 。 看看这些资源如何 : 如何对写的Java程 ...
  • 我使用的是Oracle应用程序开发框架。 它是一个完全支持的完整框架,Oracle自己使用它来构建自己的企业应用程序。 它附带了很多非常容易绑定到底层数据对象的JSF组件。 我会为所有需要数据库数据的Java应用程序推荐此选项。 你可以在Oracle Wiki上找到它的讨论: http : //wiki.oracle.com/page/ADF+Methodology+-+Work+in+Progressent The one I use is Oracle Application Development F ...
  • 定制软件的容量估算没有规则。 你只需要测试它。 有很多工具可以帮助你。 您可能会想要创建一些验收标准,例如; 在Y分钟内处理X个客户订单的能力 能够在响应时间<200ms的情况下为页面Z提供服务 等等 一旦确定了接受标准,过程通常是这样的; 找出可能的使用模式/用户旅程。 写一些测试产生符合你的发现1的流量,还有一些测试验收标准。 在参考硬件上运行这些测试(只是选择一些东西,并不重要),并发用户数不同,例如10,100,1000。 衡量响应时间随用户数量的不同而改变。 您现在有一个参考点/性能基准。 您可以 ...
  • 目标设备 - 如果您的应用程序需要在移动设备上运行,那么您的应用程序需要在客户端轻量级(不要太复杂的JavaScript,轻型DOM结构...) 可视性 - 您的应用程序的内容是否应由搜索引擎索引? 如果是,那么你必须记住,例如基于AJAX和Flash的解决方案将不会被索引(存在解决方法)。 可用性 - 如果您的应用程序需要高可用性,那么您应该考虑创建桌面应用程序 - 换句话说,富Internet应用程序(RIA) 安全 - 无论您回答以前的问题,您都必须记住安全。 使用以客户端为中心的解决方案(用客户端代 ...
  • 我们刚刚为我们的团队评估了一堆库。 我们决定坚持使用ExtJS,尽管成本和许可证,因为它很好,这就是我们所知道的。 我们考虑过Vaadin,但与ExtJS相比,它还没有适用于小部件。 此外,他们的附加组件也很昂贵。 我喜欢它。 我喜欢它的纯Java方面。 Dojo / Dijit是我们的#2,如果我们没有ExtJS经验,我们就会去那里。 他们也有很好的maquetta设计师工具。 ExtJS当然拥有最好的图表,并且它们还具有用于高端地图支持的geoext。 ExtJS不是速度恶魔,但在现代浏览器上它很好。 ...
  • 一些域名似乎更适合我的头顶原生应用: 应用程序断开数据集。 移动应用程序不能总是依靠互联网连接。 原生应用很好地处理了这种情况 对于数据输入工具尤其如此。 如果您在输入数据时在浏览器中收到呼叫,则重新启动Safari后重新加载页面时可能会丢失工作。 需要用户上传媒体的应用,如照片,视频和录音。 目前,无法通过MobileSafari上传本地iPhone媒体。 本地应用处理这种情况。 保险和房地产可能是很好的市场目标。 高级处理应用。 例如,如果您想要一个可以使用iPhone摄像头读取条形码的库存管理应用程序 ...
  • 很棒的问题,云雀! 由于我们在Qt Framework分支中,我会告诉你它是如何在Qt中完成的,但我相信你可以应用于类似的框架或库,如GTK等。 因此,Qt启动了一个框架,可以为各种平台提供一个API,从而可以非常轻松地将应用程序从一个平台移植到另一个平台,同时在这些平台上提供原生外观。 因此,例如在MacOSX上,按钮将具有这种蓝色Aqua样式,而在Windows XP上,它看起来就像Windows平台上的按钮。 奇趣科技付出了很多努力,并且在使GUI看起来很原生方面做得很好。 它是如何工作的? 好吧,所 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)