首页 \ 问答 \ 为什么使用引用而不是指针?(Why use references rather than pointers?)

为什么使用引用而不是指针?(Why use references rather than pointers?)

当没有对引用进行编译时类型检查时,为什么要使用引用而不是指针?

想象一下,你正在创建一个Person类型的对象,需要持有一个引用/指针到他们的House (非常随机) - 所以person对象将把house引用/指针作为数据成员。 想象一下,这是通过参考完成的:

class Person{
   public:
      Person(House& myHouse) : house(myHouse) { }
   private:
      House& house;
};

我看到的问题是,如果您忘记在数据成员声明中包含&,那么您认为是引用的内容将成为副本。 当然,传递和保存指针会更安全,所以这个错误会在编译时被标记出来吗?

我没有说明使用其中一个 - 我问为什么引用用于指针来保存数据成员?


Why to use references rather than pointers when there is no compile time type checking on references?

Imagine that you are creating an object of type Person that needs to hold a reference/pointer to their House (pretty random) - so the person object would hold the house reference/pointer as a data member. Imagine this was done with references:

class Person{
   public:
      Person(House& myHouse) : house(myHouse) { }
   private:
      House& house;
};

The issue I see though is if you forget to include the & in the data member declaration that thing you thought was a reference becomes a copy. Surely it would be safer to pass and save a pointer so this error would be flagged up at compile time?

I'm not making a point to use one or the other - I'm asking why are references used over pointers to save data members?


原文:https://stackoverflow.com/questions/15045863
更新时间:2023-11-24 09:11

最满意答案

我认为在你的select标签中th:值不正确,id没有正确设置。

即对于监护人来说应该是这样的

<select th:field="*{guardian}">
      <option th:each="guardian : ${guardians}" 
             th:value="${guardian.id}" 
             th:text="${guardian.name}"></option>
</select>

对于timeLapse应该这样声明

<select th:field="*{timeLapse}">
     <option th:each="timeLapse : ${timeLapses}" 
         th:value="${timeLapse.id}" 
         th:text="${timeLapse.description}"></option>
</select>  

而对于关卡,我认为你通过了错误的领域,即它可能看起来像那样

<select th:field="*{level}" multiple="multiple">
             <option th:each="level : ${alarmLevels}" 
                 th:value="${level}" 
                 th:text="${level}"></option>
        </select>

我希望这有帮助。


I think that in your select tags the th:value is not correct, the id is not set correctly.

ie for the guardian it should be like this

<select th:field="*{guardian}">
      <option th:each="guardian : ${guardians}" 
             th:value="${guardian.id}" 
             th:text="${guardian.name}"></option>
</select>

for the timeLapse is should be declared like that

<select th:field="*{timeLapse}">
     <option th:each="timeLapse : ${timeLapses}" 
         th:value="${timeLapse.id}" 
         th:text="${timeLapse.description}"></option>
</select>  

and for the level I think that you passed the wrong field ie it might look like that

<select th:field="*{level}" multiple="multiple">
             <option th:each="level : ${alarmLevels}" 
                 th:value="${level}" 
                 th:text="${level}"></option>
        </select>

I hope this helps.

相关问答

更多
  • 您需要更新依赖项。 在Package Explorer上: - >右键单击您的项目 - > Gradle - >启用依赖关系管理 - >再次右键单击它 - >刷新依赖关系 You need to update dependencies. On Package Explorer: -> right click your project -> Gradle ->Enable Dependency Management -> right click it again -> Refresh Dependencies ...
  • 使用productname而不是key和productamount而不是value 。 希望它能解决你的问题。 H A The Error was at model.addAttribute("entries", products);, shou ...
  • 您的发布示例中存在一些问题。 我们可以简化/现代化它来: @Controller public class BasketController { @GetMapping("/partPage") //or whatever page you're requesting public String getPartPage(Model model) { model.addAttribute("part", new Part()); //make sure it's ad ...
  • 我认为在你的select标签中th:值不正确,id没有正确设置。 即对于监护人来说应该是这样的 对于timeLapse应该这样声明

    相关文章

    更多
  • 请教怎样根据对象的引用得到包含它的对象的引用呢?
  • js怎么在引号里面引用变量
  • 我也要学C语言-第十七章:指针数组
  • 微信API 引用
  • 声明函数时候,形参为什么有时候用指针对象,有时候使用普通类对象?
  • Tomcat 找不到引用项目类的问题
  • jsp中使用JavaBean出现“引用类不可用”
  • 各们帮忙看一下,为什么spring引用不到jdbcTemplate?
  • 用扁平化的界面设计吸引用户
  • jsp中引用的js有一个找不到,可能是什么原因?

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。