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; Originally Posted by CornedBee It's pass by reference if you can modify the accessible entity you passed (barring write restrictions ...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    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.
    I'm not sure I'd agree with that. Pass by value means you make a copy of something. Otherwise with your description, passing a const char* in C/C++ is passing by value since you can't modify the char* string (without a cast at least).

    In Java, it helps to throw away the word 'reference' and replace it with 'pointer', since that's really what it is. If you pass a pointer to an object in Java, you can modify that object with the objects set() functions, but obviously you can't reassign it with the = sign, since that would only change the local pointer (reference), but not the pointer outside the function.
    "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

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    5,857
    Quote Originally Posted by CornedBee View Post
    But that would be a fallacy when discussing the theoretical semantics of the language.
    In the "theoretical semantics of the language" a reference is an entity that contains or presents information that identifies another object, through which that other object can be accessed or modified.
    Quote Originally Posted by CornedBee View Post
    While there is a meaning, it is not useful in considering the implications of pass by reference and pass by value.
    I disagree.
    Quote Originally Posted by CornedBee View Post
    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.
    And, again, a concern with Java is that depending on the context in which a function is called, the argument supplied by the caller is either passed by reference (and that reference is passed by value) or the supplied argument itself is passed by value. This is a bit of unnecessary mental gymnastics in understanding how to do things in Java.
    Last edited by grumpy; 10-24-2008 at 08:24 PM.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Otherwise with your description, passing a const char* in C/C++ is passing by value since you can't modify the char* string (without a cast at least).
    It's pass by value because you can't modify the pointer. If it was about the string, it's the reason I said "barring other write restrictions". The const is such a restriction.

    In the "theoretical semantics of the language" a reference is an entity that contains or presents information that identifies another object, through which that other object can be accessed or modified.
    No. In C++, for example, a reference is simply an alias for an object.

    In the end, it comes down to this. Given an entity t of type T
    Code:
    T t;
    and a function call
    Code:
    foo(t)
    then the language natively supports pass by reference if the shallow value (as in, not the value of something t references) can be changed, given a proper implementation of foo. This is the only meaningful definition of support for pass by reference.
    Java does not support this. If T is int, then I can't change the value. If T is a reference type, I can't change what it references.
    C does not support this either.
    C++ does, as does VisualBasic. C# kind of allows it - the fact that you have to give the ref or out keyword on argument passing muddles the situation a bit.

    C allows emulation of it through pointers. Java kind of allows emulation through wrapper objects, but that still doesn't change t.
    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

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    5,857
    Quote Originally Posted by CornedBee View Post
    No. In C++, for example, a reference is simply an alias for an object.
    Now we're making points on the same side of the discussion - the only difference is that we're highlighting the significance of different (but related) aspects. Using a simple example in C++;
    Code:
       int  i;
       int &x(i);
    x meets the definition "a reference is an entity that contains or presents information that identifies another object, through which that other object can be accessed or modified" I gave earlier. Related to this, x can also be used as an alias (alternate name) for i.

    The discussion is about chicken and egg at present: you're chasing the chicken and I'm chasing the egg.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The discussion is about chicken and egg at present: you're chasing the chicken and I'm chasing the egg.
    Perhaps. But I think the issue that led us to this was about the taste of chicken meat.
    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

  6. #21
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by CornedBee View Post
    //correct one
    Code:
    template <typename Of, typename What>
    inline bool instanceof(const What &w)
    {
      return dynamic_cast<const Of*>(&w) != 0;
    }
    //doesn't check inheritance
    Code:
    template <typename Of, typename What>
    inline bool instanceof(const What &w)
    {
      return typeid(w) == typeid(Of);
    }
    So, to clarify a bit things.
    The first is like instanceof in Java. It will probably though convert the type even if you just wanted a check, thus be slower.
    The second checks if the types are the same, but doesn't check if one in inherited by the other. Correct?

    In any case, C++ offers two things, so it should be more efficient.
    And, as a final note, there isn't something EXACTLY like instanceof in C++

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It will probably though convert the type even if you just wanted a check, thus be slower.
    Not really. The check whether the conversion is allowed is expensive. Performing the conversion is just an afterthought. In fact, I'd wager the additional cost is actually zero on superscalar CPUs, as the necessary instructions are executed in parallel with whatever else is around them.

    The second checks if the types are the same, but doesn't check if one in inherited by the other.
    Correct.

    In any case, C++ offers two things, so it should be more efficient.
    Comparing typeids is typically one indirect access plus one comparison, but could also be three indirect accesses and a string comparison. Depends on the platform, the compiler, and the capabilities of the linker.
    Doing a dynamic_cast is typically linear in the total number of direct and indirect base classes, doing most of the work of a typeid comparison at each step.

    And, as a final note, there isn't something EXACTLY like instanceof in C++
    Well, given that dynamic_cast isn't significantly (or even measurably, probably) slower than a pure type check, I'd say my function is exactly like instanceof.
    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

  8. #23
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    Perhaps. But I think the issue that led us to this was about the taste of chicken meat.
    Actually, I think it was whether or not Java supports pass by value that did it.
    You're saying that it ONLY supports pass by value, since the references are passed by value, but that's irrelavent, because the same is true of pointers in C/C++. The problem is that there is no way to pass the actual object by value in Java, and thereby create a copy of the object.
    The reason is because all objects in Java are created on the heap and there is no way to create an object on the stack, so rather than the objects being passed by value, their references are passed by value instead, which is what I meant by saying Java doesn't support pass by value.
    "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

  9. #24
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is better to say that Java doesn't support "object variables". Meaning that you cannot declare an object with a certain name. You can only declare a reference. The object doesn't have a name only its reference.
    So when you write Object O in java it means a reference to an object, not an object, even though that seems a bit strange. So a function that is void foo(Object O) wants a reference to an object and deals with it as it will deal with it with an int. There is no difference.
    In C++, Object O means a object variable. So it means the same for void foo(Object O).
    But C++ also offers you to pass an object by reference. ONLY C++ offers passing by reference, Java doesn't have the feature. Neither does C. In order to distinguish passing by value and passing by reference modes you use the & symbol.
    So Java supports pass-by-value ONLY, C++ supports both. C++ also supports "object variables", thus allocating on the stack". Java only supports reference variables.

Page 2 of 2 FirstFirst 12
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