Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • C++ does support reference type semantics like Java and C# - moreover, if you use reference semantics in the same way, you do get the expected polymorphic behaviour. Commented Oct 31, 2010 at 21:53
  • @Arafangion Oh really? So enlighten me please, why OP got object slicing with the code that would work in C#? Commented Oct 31, 2010 at 22:02
  • The OP specifically said in his code that he wanted an object of type Objeto. He initialized that object with an Esfera object, so some kind of conversion took place. If he said instead that he merely wanted to refer to the Esfera object, then everything would've worked as he expected. Commented Oct 31, 2010 at 23:21
  • 1
    @Arafangion Yeah if he said he wanted to refer. The point is, that he had to say it explicitly. Here is the difference in the way you and I understand language support for reference semantics. Fine C++ does support it, but you have to write wrappers for pointers yourself and overload operator= for them. int* ip = &i; is not a reference semantics. It's a value semantics for a pointer. Anyway, I changed my answer to say, that the support is not in the same way. I hope you agree with that. Commented Oct 31, 2010 at 23:29
  • Correct those are value semantics for pointers. Use reference semantics instead, ie "int& ip = i;", and there is no need to overload operator= for them, either. I still don't like the new reworded answer either, but it's better than before and I've no reason to downvote it anymore. The reason I don't like it is because C++ does support reference semantics very well - it is, however a fundamentally different language to C# and Java - using reference semantics if you did not specify that you wanted that semantic would be surprising. Java does not have value semantics except for primitive types Commented Nov 1, 2010 at 0:43