Constructor returning a pointer (noob question)

xuanbao · · 20 次点击    
<p>Is it idiomatic for a constructor to return a pointer to a newly created struct? Or does it depend on the size of the newly constructed struct? Most examples I see return a pointer but I don&#39;t know why.</p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>It depends on the use case. Do you want the methods of the struct to generally modify the struct in place, or do you want the user to need to create or copy to a new struct when they want to make modifications?</p> <p>Generally, if the struct is rather large, a pointer is preferred for efficiency but most structs won&#39;t be large enough for that decision to matter most of the time.</p></pre>jerf: <pre><p>Contra the other posts in this thread as I write this, whether or not the struct needs to be modified by the user is <em>not</em> a determining factor in the decision. <a href="" rel="nofollow">What a method does is based on the method signature, not what you have</a>; pointer methods can be called on non-pointer structs with no problem.</p> <p>(You do need to pick either pointer methods or non-pointer methods to implement a given interface, but that&#39;s another question.)</p> <p>The questions around whether you return a struct or not are all much more subtle than that and I typically don&#39;t spend a lot of time thinking about it. A direct return involves a copy into the caller, and you probably don&#39;t want to do that with big structs very often. Returning a pointer involves less copying. Either way, the caller can convert to what they want on the spot, either by taking the address of the returned value or dereferencing the pointer. Returning a struct directly probably gives the escape analyzer a better clue that this doesn&#39;t escape, there may be scenarios where that may make a difference.</p> <p>Mostly I wouldn&#39;t worry too much about this either way until you&#39;ve got some code that is doing the wrong thing and it&#39;s a performance problem.</p></pre>Sythe2o0: <pre><p>I would say that mixing pointer and non-pointer methods is very unintuitive (and will be caught by linters), so you should probably only be using one or the other, and while you can return a struct and have it use the pointer methods, that struct won&#39;t satisfy any interfaces the struct&#39;s pointer does:</p> <p><a href="" rel="nofollow"></a></p> <p>So while its true that the type you return doesn&#39;t determine how methods will work, the type does determine what interfaces are satisfied by the result which can cause confusion or forced casting if the method receiver doesn&#39;t line up with the constructor return value. I&#39;m in favor of having a &#39;canonical&#39; type for each struct, either a pointer or not, and having all receivers of that struct use that type and all constructors return that.</p></pre>everdev: <pre><p>Code tends to read a little better with struct pointers because any modifications to the struct pointer are persisted. Without a pointer, you could modify the struct values in one function but forget to replace the original struct with the modified one in the calling function. I can&#39;t think of many common use cases where you&#39;d want to modify a copy of a struct and then ignore the modified copy. So, if you want to modify your struct values, easier to use a pointer.</p></pre>md2perpe: <pre><p>If you want to put &#34;methods&#34; on the struct and some methods might modify the content in the struct, then you need a pointer.</p></pre>
20 次点击  
加入收藏 微博
0 回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传