Why C++ does not have a similar method like "instanceof" in JAVA?

This is a discussion on Why C++ does not have a similar method like "instanceof" in JAVA? within the C++ Programming forums, part of the General Programming Boards category; JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"? Is there any reason or concern JAVA implements it but ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why C++ does not have a similar method like "instanceof" in JAVA?

    JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"?
    Is there any reason or concern JAVA implements it but C++ doesn't?

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    huh?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by meili100 View Post
    JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"?
    Is there any reason or concern JAVA implements it but C++ doesn't?
    Maybe because they're both the freakin' same thing?

    Code:
    x instanceof y
    Is precisely the same as:

    Code:
    typeid(x) == typeid(y())
    It's syntax. Nothing but syntax.

    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);
    //}

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by meili100 View Post
    JAVA has "instanceof". Why C++ only has "typeid" and "dynamic_cast"?
    Is there any reason or concern JAVA implements it but C++ doesn't?
    Why doesn't Java have a const keyword? (and no, final != const)
    Why doesn't Java have pass by value or real references or a typedef keyword...?
    "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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by cpjust View Post
    Why doesn't Java have pass by value or real references or a typedef keyword...?
    What do you mean? Everything in Java is pass by value...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Code:
    template <typename Of, typename What>
    inline bool instanceof(const What &w)
    {
      return typeid(w) == typeid(Of);
    }
    
    instanceof<Foo>(bar)
    Note that there's no need to actually instantiate the target type.
    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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    5,851
    Quote Originally Posted by brewbuck View Post
    What do you mean? Everything in Java is pass by value...
    That's what programming guides/tutorials/etc for Java say but it's not true. Look up the way in which object references are passed, and ask yourself what is being actually being passed by value.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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:
    Code:
    template <typename Of, typename What>
    inline bool instanceof(const What &w)
    {
      return dynamic_cast<const Of*>(&w) != 0;
    }
    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.
    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:
    Code:
    public boolean equals(Object o)
    {
      if(o instanceof Foo) {
        Foo f = (Foo)o;
        return this.garble == f.garble;
      }
      return false;
    }
    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.)
    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

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    5,851
    Quote Originally Posted by CornedBee View Post
    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.
    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.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Passing arguments always involves passing something by value - in any programming language.
    Not true. In C++, for example, references aren't objects, so you're not passing anything by value if the parameter is a reference.
    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

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    What do you mean? Everything in Java is pass by value...
    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

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by cpjust View Post
    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.
    Objects are passed by value. The thing is it isn't passing the object itself but instead it is passing a pointer... by value.

    So
    Code:
    Integer i = new Integer(5);
    Foo.meh(i);
    
    public void meh(Integer j)
    {
      j = new Integer(6);
    }
    If 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);
    
    void meh(Integer *j)
    {
      j = new Integer(6);
    }

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    5,851
    Quote Originally Posted by CornedBee View Post
    Not true. In C++, for example, references aren't objects, so you're not passing anything by value if the parameter is a reference.
    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.
    Quote Originally Posted by CornedBee View Post
    The underlying implementation may use (will use almost always, actually) pointers, but on the language level, there aren't any.
    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.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The concept of a reference having a value is easier to grasp when you dig down to the detail of underlying implementation
    But that would be a fallacy when discussing the theoretical semantics of the language.

    but still has meaning in the higher level language.
    While there is a meaning, it is not useful in considering the implications of pass by reference and pass by value.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. java for c++ programmers book
    By qwertiop in forum Tech Board
    Replies: 3
    Last Post: 05-18-2007, 03:17 PM
  4. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  5. Against/For Java???
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-30-2001, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21