【问题标题】:Can I Use Insert Snippet to Create Setters and Getters in Visual C++我可以使用插入片段在 Visual C++ 中创建 Setter 和 Getter
【发布时间】:2015-06-03 10:59:21
【问题描述】:

This answer 建议创建一个代码片段以允许“插入片段”菜单包含 Visual C# 中的 setter 和 getter。

我去查看答案的链接:https://msdn.microsoft.com/en-us/library/ms165392.aspx

但它确实没有关于插入固定字符串以外的任何信息。

我想要的是能够右键单击成员变量,选择“插入片段”,然后插入 setter 或 getter 的选项将根据上下文出现。有没有办法在 Visual C++ 中实现这一点?

【问题讨论】:

    标签: c++ visual-c++ visual-studio-2013 properties code-snippets


    【解决方案1】:

    您可以在 Visual C++ 中插入 sn-ps,但不是您想要的方式。

    Visual C# 允许用户在属性上添加 getter 和 setter,例如,如果我有以下属性:

    public double foo
    

    如果我选择foo,很容易编写一个插入的sn-p:

    { get; set; }
    

    请注意,名称foo 没有重复使用。

    如果我想在 C++ 中创建 getter 和 setter,我需要执行以下操作:

    double getFoo(){ return foo; }
    void setFoo(double foo){ this->foo = foo; }
    

    从 Visual Studio 2013 开始,我无法使用 sn-ps 执行此操作,因为我不能多次使用 $selected$https://msdn.microsoft.com/en-us/library/ms171418.aspx#code

    所以我可以写一个 sn-p,比如 doubleGetSet,插入代码:

    double get*(){ return *; }
    void set*(double *){ this->* = *; }
    

    但是sn-p的插入器必须负责将*分别更改为Foo/foo

    但是,当您这样做时,就不清楚为什么您不能一开始就输入这个。

    总而言之,在$selected$ 成为文字以便可以在 sn-ps 中重用之前,使用 sn-p 创建 getter 和 setter 是不可行的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多