JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"?
Is there any reason or concern JAVA implements it but C++ doesn't?
JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"?
Is there any reason or concern JAVA implements it but C++ doesn't?
huh?
Maybe because they're both the freakin' same thing?
Is precisely the same as:Code:x instanceof y
It's syntax. Nothing but syntax.Code:typeid(x) == typeid(y())
Or is your question actually, "Why aren't these two languages identical?" Which is an awfully weird question.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Note that there's no need to actually instantiate the target type.Code:template <typename Of, typename What> inline bool instanceof(const What &w) { return typeid(w) == typeid(Of); } instanceof<Foo>(bar)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
The reference is being passed by value. The point is, you can't modify the actual argument - you can't modify what the reference points to, or the value of the integer. That's why Java is pass-by-value, always.
I just realized my little function is incorrect. It doesn't test inheritance correctly. This one works:
Of course, that's completely pointless. Now that you have determined that w is an instance of Of, you still have to cast it. You can't use a static_cast (it could be a cross-cast instead of a simple downcast). You can use a dynamic_cast, but you just did, so there's clearly a waste going on here.Code:template <typename Of, typename What> inline bool instanceof(const What &w) { return dynamic_cast<const Of*>(&w) != 0; }
That's why the standard dynamic_cast usage looks like this:
Code:if(Bar *pb = dynamic_cast<Bar*>(po)) { // Use pb } // pb is not in scope and cannot be used, which is good, because it's not valid
Java's instanceof is a stupid idea anyway. There's very few situationa where you want to do a typecheck and not actually use the object afterwards. Thus, Java's type selection is always a mess of redundant type comparison. Take your average Object.equals implementation:
Note that there are actually two typechecks here, one for the instanceof, and another for the cast. (Some VMs optimize the second away.) Completely redundant. C#'s 'as' operator is much more useful (and, incidently, equivalent in usage to dynamic_cast.)Code:public boolean equals(Object o) { if(o instanceof Foo) { Foo f = (Foo)o; return this.garble == f.garble; } return false; }
I don't know if the code below is actually correct about the declaration in the condition and implicit bool conversion.
Code:public bool Equals(Object o) { if(Foo f = o as Foo) { return this.garble == f.garble; } return false; }
Last edited by CornedBee; 10-24-2008 at 01:03 AM.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Yeah ... the problem is that, depending on context, a Java function can be passed an argument by reference (and can modify whatever is referenced) or the same function can be passed something else by value.
Passing arguments always involves passing something by value - in any programming language. But the way it's described in Java is a problem: the mechanism of what is passed changes with context.
Not true. In C++, for example, references aren't objects, so you're not passing anything by value if the parameter is a reference.Passing arguments always involves passing something by value - in any programming language.
In C#, if you have a ref or out parameter, you don't pass anything by value.
The underlying implementation may use (will use almost always, actually) pointers, but on the language level, there aren't any.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
No, primitive types are passed by value, but objects are passed by reference. Obviously the references themselves are passed by value, just like pointers, but no copy of the object is created, and unless the object is immutable like Strings, you can modify the object that is passed to a function by using the object's member functions.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Objects are passed by value. The thing is it isn't passing the object itself but instead it is passing a pointer... by value.
SoIf it was pass by reference then i would be 6 after the function. Really what you have is something like this in c++ (assume an Integer class has been created)Code:Integer i = new Integer(5); Foo.meh(i); public void meh(Integer j) { j = new Integer(6); }
Code:Integer *i = new Integer(5); Foo.meh(i); void meh(Integer *j) { j = new Integer(6); }
Anything that can be initialised has a value (where value is "some attribute that can be used to identify, specify, or quantify" - one dictionary definition). The value of a reference is some identifier of (or information that uniquely identifies) an object. Try and create a reference variable in C++ without initialising it.
As far as the underlying implementation is concerned the value of a reference will be that thing which is "almost always" a pointer. The concept of a reference having a value is easier to grasp when you dig down to the detail of underlying implementation, but still has meaning in the higher level language.
The difference is that the higher level languages (C++, Java) constrain how references can be used. For example, once a reference is initialised in C++ it is not possible to make it refer to something else. But the fact it must be initialised (ie some attribute of it must be set) means that a reference has a value.
A reference is just another way of expressing a pointer. I mean if you write a C++ function that calls for a pointer to be past into it, and use that program from your C code, you will need to pass a pointer into that function. A reference isn't anything magical. Its just a semantic thing.
But that would be a fallacy when discussing the theoretical semantics of the language.The concept of a reference having a value is easier to grasp when you dig down to the detail of underlying implementation
While there is a meaning, it is not useful in considering the implications of pass by reference and pass by value.but still has meaning in the higher level language.
It's pass by reference if you can modify the accessible entity you passed (barring write restrictions imposed through other means), pass by value if you can't. And the accessible entity in the case of Java is the object reference, not the object itself.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law