C++: "Type Checking"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Web Developer

    C++: "Type Checking"

    Hi,

    I come across the term "type checking" very often in my readings on C++, and
    have never heard it in Java. Besides the simplistic answer that it checks
    the "type", what more does it mean?


    WD



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003


  • John Harrison

    #2
    Re: "Type Checking"


    "Web Developer" <nospam@hotmail .com> wrote in message
    news:3f373851_1 @news.iprimus.c om.au...[color=blue]
    > Hi,
    >
    > I come across the term "type checking" very often in my readings on C++,[/color]
    and[color=blue]
    > have never heard it in Java. Besides the simplistic answer that it checks
    > the "type", what more does it mean?
    >
    >
    > WD
    >[/color]

    I think its a question of timing. All languages check types, you aren't
    allowed to multiply two strings in any language (AFAIK). But some languages
    delay type checking until the program runs. C++ does most of its type
    checking at compile type. This results in faster code, smaller data and
    quicker detection of type errors, but it also means a more complex language.

    We say C++ is a strongly typed language. In general I would say strong
    typing is a good thing but there are occasions where you want to relax it
    and check types at run time. That's where virtual functions and RTTI come
    in.

    john


    Comment

    • Alf P. Steinbach

      #3
      Re: &quot;Type Checking&quot;

      On Mon, 11 Aug 2003 05:20:48 -0400, "ghl" <glabowitz@comc ast.net> wrote:
      [color=blue]
      >"John Harrison" <john_andronicu s@hotmail.com> wrote in message
      >news:bh7dn6$tv ruf$1@ID-196037.news.uni-berlin.de...[color=green]
      >>
      >> "Web Developer" <nospam@hotmail .com> wrote in message
      >> news:3f373851_1 @news.iprimus.c om.au...[color=darkred]
      >> > Hi,
      >> >
      >> > I come across the term "type checking" very often in my readings on C++,[/color]
      >> and[color=darkred]
      >> > have never heard it in Java. Besides the simplistic answer that it[/color][/color]
      >checks[color=green][color=darkred]
      >> > the "type", what more does it mean?
      >> >
      >> >
      >> > WD
      >> >[/color]
      >>
      >> I think its a question of timing. All languages check types, you aren't
      >> allowed to multiply two strings in any language (AFAIK). But some[/color]
      >languages[color=green]
      >> delay type checking until the program runs. C++ does most of its type
      >> checking at compile type. This results in faster code, smaller data and
      >> quicker detection of type errors, but it also means a more complex[/color]
      >language.[color=green]
      >>
      >> We say C++ is a strongly typed language. In general I would say strong
      >> typing is a good thing but there are occasions where you want to relax it
      >> and check types at run time. That's where virtual functions and RTTI come
      >> in.[/color]
      >
      >Java is also a strongly typed language and checks for compatible types for
      >method arguments and return values at compile time.[/color]

      Nope. Consider classes A, B and C in separate files, where A uses B uses
      C, and A is the "main" program. Compile all. Change C to be incompatible
      with B. Compile class A using Sun's compiler: no errors reported. Run.
      Runtime error.

      That could not happen in C++.

      C++ has _global_ static type checking; Java has runtime type checking.

      Comment

      • Alf P. Steinbach

        #4
        Re: &quot;Type Checking&quot;

        On Mon, 11 Aug 2003 13:34:20 GMT, tom_usenet@hotm ail.com (tom_usenet) wrote:
        [color=blue]
        >On Mon, 11 Aug 2003 10:23:27 GMT, alfps@start.no (Alf P. Steinbach)
        >wrote:
        >[color=green]
        >>Nope. Consider classes A, B and C in separate files, where A uses B uses
        >>C, and A is the "main" program. Compile all. Change C to be incompatible
        >>with B. Compile class A using Sun's compiler: no errors reported. Run.
        >>Runtime error.
        >>
        >>That could not happen in C++.
        >>
        >>C++ has _global_ static type checking; Java has runtime type checking.[/color]
        >
        >The Java language has stronger static type checking than C++. e.g.
        >implicit conversions from double to int, etc. don't exist. C++ however
        >has better idioms for static type checking, thanks to using generics
        >rather than an Object base-of-all class. Java encourages use of the
        >evil instanceof operator.
        >
        >I think you're talking about the link time safety that C++ has thanks
        >to name mangling. But C++ will just crash if you replace a DLL with
        >another one with incompatible types, so I don't get your point.[/color]

        DLLs are not part of C++.

        [color=blue]
        >Java will detect the problem if you recompile[/color]

        Nope. Java will only detect the problem if you recompile class B.
        You have an executable system without recompiling class B (and this is
        not theoretical, it has been a major problem in some large Java projects).

        [color=blue]
        >just as happens with C++.[/color]

        Nope. With C++ you have global type-checking. You cannot obtain an
        executable with static type inconsistencies .

        Comment

        • ghl

          #5
          Re: &quot;Type Checking&quot;

          "Alf P. Steinbach" <alfps@start.no > wrote in message
          news:3f376df1.4 31596921@News.C IS.DFN.DE...[color=blue]
          > On Mon, 11 Aug 2003 05:20:48 -0400, "ghl" <glabowitz@comc ast.net> wrote:
          >[color=green]
          > >"John Harrison" <john_andronicu s@hotmail.com> wrote in message
          > >news:bh7dn6$tv ruf$1@ID-196037.news.uni-berlin.de...[color=darkred]
          > >>
          > >> "Web Developer" <nospam@hotmail .com> wrote in message
          > >> news:3f373851_1 @news.iprimus.c om.au...
          > >> > Hi,
          > >> >
          > >> > I come across the term "type checking" very often in my readings on[/color][/color][/color]
          C++,[color=blue][color=green][color=darkred]
          > >> and
          > >> > have never heard it in Java. Besides the simplistic answer that it[/color]
          > >checks[color=darkred]
          > >> > the "type", what more does it mean?
          > >> >
          > >> >
          > >> > WD
          > >> >
          > >>
          > >> I think its a question of timing. All languages check types, you aren't
          > >> allowed to multiply two strings in any language (AFAIK). But some[/color]
          > >languages[color=darkred]
          > >> delay type checking until the program runs. C++ does most of its type
          > >> checking at compile type. This results in faster code, smaller data and
          > >> quicker detection of type errors, but it also means a more complex[/color]
          > >language.[color=darkred]
          > >>
          > >> We say C++ is a strongly typed language. In general I would say strong
          > >> typing is a good thing but there are occasions where you want to relax[/color][/color][/color]
          it[color=blue][color=green][color=darkred]
          > >> and check types at run time. That's where virtual functions and RTTI[/color][/color][/color]
          come[color=blue][color=green][color=darkred]
          > >> in.[/color]
          > >
          > >Java is also a strongly typed language and checks for compatible types[/color][/color]
          for[color=blue][color=green]
          > >method arguments and return values at compile time.[/color]
          >
          > Nope. Consider classes A, B and C in separate files, where A uses B uses
          > C, and A is the "main" program. Compile all. Change C to be incompatible
          > with B. Compile class A using Sun's compiler: no errors reported. Run.
          > Runtime error.[/color]

          Wrong. Been there. Tried that.
          Created classes as above, all returning String type reference variable.
          Changed return type of C to int. Message on recompiling A gives:
          ..\C.java:3: incompatible types
          found : java.lang.Strin g
          required: int
          public int info = "I'm c";
          ^
          ..\C.java:13: incompatible types
          found : int
          required: java.lang.Strin g
          return info;
          ^
          2 errors
          --
          Gary


          Comment

          • ghl

            #6
            Re: &quot;Type Checking&quot;

            "Alf P. Steinbach" <alfps@start.no > wrote in message
            news:3f37cc95.4 55824625@News.C IS.DFN.DE...[color=blue]
            > On Mon, 11 Aug 2003 13:06:42 -0400, "ghl" <glabowitz@comc ast.net> wrote:
            >[color=green]
            > >"Alf P. Steinbach" <alfps@start.no > wrote in message
            > >news:3f376df1. 431596921@News. CIS.DFN.DE...[/color][/color]
            <<large snip>>[color=blue]
            > "Change" means: change the source code of class C, compile class C.
            >
            > Then compile class A, or not (it makes no difference), and run:
            >
            >
            > Exception in thread "main" java.lang.NoSuc hMethodError:[/color]
            C.result()Ljava/lang/String;[color=blue]
            > at B.result(B.java :5)
            > at A.main(A.java:5 )
            >
            >
            > Got it?[/color]

            Ah, I get what you're driving at. Yes, you get that. Does the same thing
            happen in C++ with the linker detecting the change?
            The Java error at runtime is detected because there is no linker and the
            type checking can only occur when class C is actually loaded and checked.
            I get your point.
            --
            Gary


            Comment

            • Alf P. Steinbach

              #7
              Re: &quot;Type Checking&quot;

              On Mon, 11 Aug 2003 13:36:19 -0400, "ghl" <glabowitz@comc ast.net> wrote:
              [color=blue]
              >"Alf P. Steinbach" <alfps@start.no > wrote in message
              >news:3f37cc95. 455824625@News. CIS.DFN.DE...[color=green]
              >> On Mon, 11 Aug 2003 13:06:42 -0400, "ghl" <glabowitz@comc ast.net> wrote:
              >>[color=darkred]
              >> >"Alf P. Steinbach" <alfps@start.no > wrote in message
              >> >news:3f376df1. 431596921@News. CIS.DFN.DE...[/color][/color]
              ><<large snip>>[color=green]
              >> "Change" means: change the source code of class C, compile class C.
              >>
              >> Then compile class A, or not (it makes no difference), and run:
              >>
              >>
              >> Exception in thread "main" java.lang.NoSuc hMethodError:[/color]
              >C.result()Ljav a/lang/String;[color=green]
              >> at B.result(B.java :5)
              >> at A.main(A.java:5 )
              >>
              >>
              >> Got it?[/color]
              >
              >Ah, I get what you're driving at. Yes, you get that. Does the same thing
              >happen in C++ with the linker detecting the change?[/color]

              In practice the linker detects the change, yes.

              In theory there need not be a linker in a C++ implementation, but the
              _language_ (standard) guarantees global static type-checking whatever
              execution system is actually used.

              There's no runtime error because you don't get an executable.

              Comment

              Working...