diff C C++

Discussion in 'C++' started by Rob Hoelz, Dec 20, 2006.

  1. Rob Hoelz

    Rob Hoelz Guest

    I've been compiling a list of the differences between C and C++, and
    I'd like to see how thorough I've been. Could any of you go over this
    list and see if I've missed anything?

    Here's the list:

    Mid-scope declarations
    Default function parameters
    new/delete operators
    Better implementation of const
    References
    Exception handling
    "Catch all" catch block
    Classes
    Constructors/Destructors
    Access modifiers
    Inheritance (multiple inheritance too)
    Templates (classes and functions; template specialization)
    Initialization Lists
    Implicit constructors
    Friend access
    Namespaces
    Anonymous unions
    Better casts (type) type(var) static_cast<type> const_cast<type>
    reinterpret_cast<type> dynamic_cast<type>
    Inline functions
    Enhanced static
    Member function/data pointers
    Virtual functions (pure)
    Const pointers
    mutable keyword
    Anonymous namespaces
    You don't need struct, union, enum tags
    :: (scope resolution operator)
    Function overloading
    Operator overloading
    Runtime type identification
    C++ style includes
    // Comments
    Functions as an l-value (must return reference)
    structs can include functions
    this pointer
    bool keyword (is it a C++ keyword?)

    I/O mechanisms
    STL

    Thanks!

    --
    -Rob Hoelz
    Rob Hoelz, Dec 20, 2006
    #1
    1. Advertising

  2. Rob Hoelz

    Micah Cowan Guest

    Rob Hoelz wrote:
    > I've been compiling a list of the differences between C and C++, and
    > I'd like to see how thorough I've been. Could any of you go over this
    > list and see if I've missed anything?
    >
    > Here's the list:
    >
    > Mid-scope declarations
    > Default function parameters
    > new/delete operators
    > Better implementation of const
    > References
    > Exception handling
    > "Catch all" catch block


    All catches are a difference from C... isn't the above line redundant
    with the preceding line?

    > Classes
    > Constructors/Destructors
    > Access modifiers
    > Inheritance (multiple inheritance too)
    > Templates (classes and functions; template specialization)
    > Initialization Lists
    > Implicit constructors
    > Friend access
    > Namespaces
    > Anonymous unions
    > Better casts (type) type(var) static_cast<type> const_cast<type>
    > reinterpret_cast<type> dynamic_cast<type>
    > Inline functions
    > Enhanced static
    > Member function/data pointers
    > Virtual functions (pure)
    > Const pointers
    > mutable keyword
    > Anonymous namespaces
    > You don't need struct, union, enum tags
    > :: (scope resolution operator)
    > Function overloading
    > Operator overloading
    > Runtime type identification
    > C++ style includes
    > // Comments
    > Functions as an l-value (must return reference)
    > structs can include functions


    As may unions.

    > this pointer
    > bool keyword (is it a C++ keyword?)


    Yes, it's a keyword in C++.

    > I/O mechanisms


    That description's pretty vague... obviously C has I/O mechanisms...

    > STL


    This is more of a list of differences between the 1990 version of C and
    C++, rather than differences between C (which is now defined by the
    1999 standard) and C++. If it were the latter, differences such as //
    comments, bool (though it's not a keyword in C), inline functions, and
    "Mid-scope declarations" would go away (though there would still be
    some related differences). New differences such as C's "restrict"
    keyword, compound literals, C's complex number implementation, and
    Variable Length Arrays would need to be listed.

    Here are some further differences between C90 and C++ that spring to
    mind:

    Removal of non-prototype function declarations.
    Removal of implicit int (also removed from C99)
    The type of string literals (const char[] vs char*)
    The type of character literals (char vs int)
    Implicit conversion to/from void* has been removed.
    main() cannot be called recursively in C++ (can in C)
    Conversion between object pointers and (unsigned char *) is not
    explicitly well-defined as it is in C

    .... if you really want to be pedantic about such differences, you could
    sit the C and C++ language specifications side-by-side, and compare
    differences in related sections. That would take quite a while, but you
    could end up with a pretty exhaustive list that way :)

    -Micah
    Micah Cowan, Dec 20, 2006
    #2
    1. Advertising

  3. Rob Hoelz wrote:
    > I've been compiling a list of the differences between C and C++, and
    > I'd like to see how thorough I've been. Could any of you go over this
    > list and see if I've missed anything?
    >
    > Here's the list:
    >
    > Mid-scope declarations


    Min-scope declarations are in C99 as well

    > Default function parameters
    > new/delete operators
    > Better implementation of const
    > References
    > Exception handling
    > "Catch all" catch block


    I'd say that "Catch all" catch blocks falls under "Exception handling"

    > Classes
    > Constructors/Destructors
    > Access modifiers
    > Inheritance (multiple inheritance too)
    > Templates (classes and functions; template specialization)
    > Initialization Lists
    > Implicit constructors
    > Friend access
    > Namespaces
    > Anonymous unions
    > Better casts (type) type(var) static_cast<type> const_cast<type>
    > reinterpret_cast<type> dynamic_cast<type>
    > Inline functions


    Inline functions are in C99

    > Enhanced static


    What do you mean by this?

    > Member function/data pointers
    > Virtual functions (pure)
    > Const pointers


    Const pointers are in C

    > mutable keyword
    > Anonymous namespaces
    > You don't need struct, union, enum tags
    > :: (scope resolution operator)
    > Function overloading
    > Operator overloading
    > Runtime type identification
    > C++ style includes


    What do you mean by "C++ style includes"?

    > // Comments


    These are available in C99 also

    > Functions as an l-value (must return reference)
    > structs can include functions
    > this pointer
    > bool keyword (is it a C++ keyword?)


    Yes, it is a keyword. (C99 has the _Bool keyword, BTW)

    >
    > I/O mechanisms
    > STL
    >
    > Thanks!
    >



    --
    Clark S. Cox III
    Clark S. Cox III, Dec 20, 2006
    #3
  4. Rob Hoelz

    Rob Hoelz Guest

    "Clark S. Cox III" <> wrote:

    > Rob Hoelz wrote:
    > > I've been compiling a list of the differences between C and C++, and
    > > I'd like to see how thorough I've been. Could any of you go over
    > > this list and see if I've missed anything?
    > >
    > > Here's the list:
    > >
    > > Mid-scope declarations

    >
    > Min-scope declarations are in C99 as well


    I've based this off of C89.

    >
    > > Default function parameters
    > > new/delete operators
    > > Better implementation of const
    > > References
    > > Exception handling
    > > "Catch all" catch block

    >
    > All catches are a difference from C... isn't the above line redundant
    > with the preceding line?
    > I'd say that "Catch all" catch blocks falls under "Exception handling"


    If I post this list elsewhere, I will remove this.

    >
    > > Classes
    > > Constructors/Destructors
    > > Access modifiers
    > > Inheritance (multiple inheritance too)
    > > Templates (classes and functions; template specialization)
    > > Initialization Lists
    > > Implicit constructors
    > > Friend access
    > > Namespaces
    > > Anonymous unions
    > > Better casts (type) type(var) static_cast<type> const_cast<type>
    > > reinterpret_cast<type> dynamic_cast<type>
    > > Inline functions

    >
    > Inline functions are in C99


    Once again, C89.

    >
    > > Enhanced static

    >
    > What do you mean by this?


    I mean that the meaning of static in C++ has been expanded from the
    original meaning in C.

    >
    > > Member function/data pointers
    > > Virtual functions (pure)
    > > Const pointers

    >
    > Const pointers are in C
    >
    > > mutable keyword
    > > Anonymous namespaces
    > > You don't need struct, union, enum tags
    > > :: (scope resolution operator)
    > > Function overloading
    > > Operator overloading
    > > Runtime type identification
    > > C++ style includes

    >
    > What do you mean by "C++ style includes"?


    I mean include statements like this:
    #include <cstring>
    #include <iostream>

    >
    > > // Comments

    >
    > These are available in C99 also
    >
    > > Functions as an l-value (must return reference)
    > > structs can include functions

    > As may unions.


    Thanks for this addition!

    > > this pointer
    > > bool keyword (is it a C++ keyword?)

    >
    > Yes, it is a keyword. (C99 has the _Bool keyword, BTW)
    >
    > >
    > > I/O mechanisms

    >
    > That description's pretty vague... obviously C has I/O mechanisms...


    By this I simply meant the difference between using stdio and iostream.

    > > STL
    > >
    > > Thanks!
    > >

    >
    >


    Thanks for all of the corrections, feel free to keep them coming!

    --
    -Rob Hoelz
    Rob Hoelz, Dec 20, 2006
    #4
  5. Rob Hoelz

    Ian Collins Guest

    Rob Hoelz wrote:

    > structs can include functions


    As they can in C.

    --
    Ian Collins.
    Ian Collins, Dec 20, 2006
    #5
  6. Rob Hoelz

    Rob Hoelz Guest

    Ian Collins <> wrote:

    > Rob Hoelz wrote:
    >
    > > structs can include functions

    >
    > As they can in C.
    >


    rob@TheRing ~ $ gcc -o test test.c
    test.c:6: error: field 'foo' declared as a function

    Doesn't seem like it.

    -Rob Hoelz
    Rob Hoelz, Dec 20, 2006
    #6
  7. Rob Hoelz

    Ian Collins Guest

    Rob Hoelz wrote:
    > Ian Collins <> wrote:
    >
    >
    >>Rob Hoelz wrote:
    >>
    >>
    >>>structs can include functions

    >>
    >>As they can in C.
    >>

    > rob@TheRing ~ $ gcc -o test test.c
    > test.c:6: error: field 'foo' declared as a function
    >
    > Doesn't seem like it.


    struct X
    {
    void (*fn)( struct X* );
    };

    void xFn( struct X* this ) {}

    int main()
    {
    struct X x;
    x.fn = xFn;
    }

    --
    Ian Collins.
    Ian Collins, Dec 20, 2006
    #7
  8. Rob Hoelz

    Rob Hoelz Guest

    Ian Collins <> wrote:

    > Rob Hoelz wrote:
    > > Ian Collins <> wrote:
    > >
    > >
    > >>Rob Hoelz wrote:
    > >>
    > >>
    > >>>structs can include functions
    > >>
    > >>As they can in C.
    > >>

    > > rob@TheRing ~ $ gcc -o test test.c
    > > test.c:6: error: field 'foo' declared as a function
    > >
    > > Doesn't seem like it.

    >
    > struct X
    > {
    > void (*fn)( struct X* );
    > };
    >
    > void xFn( struct X* this ) {}
    >
    > int main()
    > {
    > struct X x;
    > x.fn = xFn;
    > }
    >


    But that's a function pointer, not a function...

    --
    -Rob Hoelz
    Rob Hoelz, Dec 20, 2006
    #8
  9. Rob Hoelz wrote:
    > "Clark S. Cox III" <> wrote:
    >>Rob Hoelz wrote:
    >>
    >>>I've been compiling a list of the differences between C and C++, and
    >>>I'd like to see how thorough I've been. Could any of you go over
    >>>this list and see if I've missed anything?
    >>>


    [snipped items from his list]

    >>>C++ style includes

    >>
    >>What do you mean by "C++ style includes"?

    >
    >
    > I mean include statements like this:
    > #include <cstring>
    > #include <iostream>


    In which regards does this #include statement differ from the C #include
    statement? AFAIK, the pre-compiler from C89 only differs from C++
    pre-compiler with regard to the handling of one-line-comments.

    Regards,
    Stuart
    Stuart Redmann, Dec 20, 2006
    #9
  10. Rob Hoelz

    Rob Hoelz Guest

    Stuart Redmann <> wrote:

    > Rob Hoelz wrote:
    > > "Clark S. Cox III" <> wrote:
    > >>Rob Hoelz wrote:
    > >>
    > >>>I've been compiling a list of the differences between C and C++,
    > >>>and I'd like to see how thorough I've been. Could any of you go
    > >>>over this list and see if I've missed anything?
    > >>>

    >
    > [snipped items from his list]
    >
    > >>>C++ style includes
    > >>
    > >>What do you mean by "C++ style includes"?

    > >
    > >
    > > I mean include statements like this:
    > > #include <cstring>
    > > #include <iostream>

    >
    > In which regards does this #include statement differ from the C
    > #include statement? AFAIK, the pre-compiler from C89 only differs
    > from C++ pre-compiler with regard to the handling of
    > one-line-comments.
    >
    > Regards,
    > Stuart


    Here's the difference:
    #include <string.h> is a C-style include. It includes the file
    extension.

    #include <iostream> is a C++-style include; it doesn't include the
    extension.

    #include <cstring>

    This is another C++-style include; including the standard C string
    library. This has the same effect as the C-style include I demonstrated
    before; but this one will only work in C++.

    I realize the difference is rather superficial, but like I said, I just
    wanted to be thorough.
    --
    -Rob Hoelz
    Rob Hoelz, Dec 20, 2006
    #10
  11. On Dec 20, 11:30 am, Rob Hoelz <> wrote:
    > Stuart Redmann <> wrote:
    > > Rob Hoelz wrote:
    > > > I mean include statements like this:
    > > > #include <cstring>
    > > > #include <iostream>

    >
    > > In which regards does this #include statement differ from the C
    > > #include statement? AFAIK, the pre-compiler from C89 only differs
    > > from C++ pre-compiler with regard to the handling of
    > > one-line-comments.

    >
    > > Regards,
    > > StuartHere's the difference:

    > #include <string.h> is a C-style include. It includes the file
    > extension.
    >
    > #include <iostream> is a C++-style include; it doesn't include the
    > extension.
    >
    > #include <cstring>
    >
    > This is another C++-style include; including the standard C string
    > library. This has the same effect as the C-style include I demonstrated
    > before; but this one will only work in C++.


    Not exactly the same effect AFAIK, <cstring> places the functions and
    whatnot in <string.h> in the std-namespace.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 20, 2006
    #11
  12. Rob Hoelz

    Marcus Kwok Guest

    Rob Hoelz <> wrote:
    > I've been compiling a list of the differences between C and C++, and
    > I'd like to see how thorough I've been. Could any of you go over this
    > list and see if I've missed anything?
    >
    > Here's the list:

    [list redacted]

    A fairly comprehensive document detailing the incompatibilities between
    C++ and various versions of C can be found here:

    http://david.tribble.com/text/cdiffs.htm

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Dec 20, 2006
    #12
  13. Rob Hoelz

    Mike Guest

    Rob Hoelz wrote:
    > Ian Collins <> wrote:
    >
    > > Rob Hoelz wrote:
    > > > Ian Collins <> wrote:
    > > >
    > > >
    > > >>Rob Hoelz wrote:
    > > >>
    > > >>
    > > >>>structs can include functions
    > > >>
    > > >>As they can in C.
    > > >>
    > > > rob@TheRing ~ $ gcc -o test test.c
    > > > test.c:6: error: field 'foo' declared as a function
    > > >
    > > > Doesn't seem like it.

    > >
    > > struct X
    > > {
    > > void (*fn)( struct X* );
    > > };
    > >
    > > void xFn( struct X* this ) {}
    > >
    > > int main()
    > > {
    > > struct X x;
    > > x.fn = xFn;
    > > }
    > >

    >
    > But that's a function pointer, not a function...
    >
    > --
    > -Rob Hoelz


    I think Rob is right. If C can have a function definition inside a
    struct, would the struct become a "class" ?

    Anyway, a struct having a function pointer should be in C from day one.
    Mike, Dec 21, 2006
    #13
  14. Ian Collins wrote:
    > Rob Hoelz wrote:
    >> Ian Collins <> wrote:
    >>
    >>
    >>> Rob Hoelz wrote:
    >>>
    >>>
    >>>> structs can include functions
    >>> As they can in C.
    >>>

    >> rob@TheRing ~ $ gcc -o test test.c
    >> test.c:6: error: field 'foo' declared as a function
    >>
    >> Doesn't seem like it.

    >
    > struct X
    > {
    > void (*fn)( struct X* );
    > };
    >
    > void xFn( struct X* this ) {}
    >
    > int main()
    > {
    > struct X x;
    > x.fn = xFn;
    > }
    >


    That is a struct that contains a pointer (nothing special there), *not*
    a struct that contains a function. A struct that contains a function
    looks like this (valid C++, invalid C):

    struct X
    {
    void fn() {}
    };


    --
    Clark S. Cox III
    Clark S. Cox III, Dec 22, 2006
    #14
  15. Rob Hoelz

    blangela Guest

    Rob Hoelz wrote:
    > I've been compiling a list of the differences between C and C++, and
    > I'd like to see how thorough I've been. Could any of you go over this
    > list and see if I've missed anything?
    >
    > Here's the list:
    >
    > Mid-scope declarations
    > Default function parameters
    > new/delete operators
    > Better implementation of const
    > References
    > Exception handling
    > "Catch all" catch block
    > Classes
    > Constructors/Destructors
    > Access modifiers
    > Inheritance (multiple inheritance too)
    > Templates (classes and functions; template specialization)
    > Initialization Lists
    > Implicit constructors
    > Friend access
    > Namespaces
    > Anonymous unions
    > Better casts (type) type(var) static_cast<type> const_cast<type>
    > reinterpret_cast<type> dynamic_cast<type>
    > Inline functions
    > Enhanced static
    > Member function/data pointers
    > Virtual functions (pure)
    > Const pointers
    > mutable keyword
    > Anonymous namespaces
    > You don't need struct, union, enum tags
    > :: (scope resolution operator)
    > Function overloading
    > Operator overloading
    > Runtime type identification
    > C++ style includes
    > // Comments
    > Functions as an l-value (must return reference)


    I suspect that the above statement is _NOT_ corrrect. Here is an
    example that compiles on Comeau that shows a function being used as an
    l-value returning a pointer, _NOT_ a reference. I suspect this code
    would compile on C compiler as well (substituting printf() etc. for
    cout etc of course).

    #include <iostream>
    int * func();
    int main()
    {
    *func() = 20; // will print 10 the first time
    func(); // should print 20 the second time.
    return 0;
    }

    int * func()
    {
    static int i = 10;
    std::cout << i << std::endl;
    return &i;
    }


    > structs can include functions
    > this pointer
    > bool keyword (is it a C++ keyword?)
    >
    > I/O mechanisms
    > STL
    >
    > Thanks!
    >
    > --
    > -Rob Hoelz
    blangela, Dec 22, 2006
    #15
  16. blangela wrote:
    > Rob Hoelz wrote:
    >> Functions as an l-value (must return reference)

    >
    > I suspect that the above statement is _NOT_ corrrect. Here is an
    > example that compiles on Comeau that shows a function being used as an
    > l-value returning a pointer, _NOT_ a reference.


    The function is not returning an lvalue, it is returning a pointer.
    Dereferencing that pointer (which is an rvalue) yields an lvalue.

    > I suspect this code
    > would compile on C compiler as well (substituting printf() etc. for
    > cout etc of course).
    >
    > #include <iostream>
    > int * func();
    > int main()
    > {
    > *func() = 20; // will print 10 the first time
    > func(); // should print 20 the second time.
    > return 0;
    > }
    >
    > int * func()
    > {
    > static int i = 10;
    > std::cout << i << std::endl;
    > return &i;
    > }


    --
    Clark S. Cox III
    Clark S. Cox III, Dec 22, 2006
    #16
  17. Rob Hoelz

    blangela Guest

    Clark S. Cox III wrote:
    > blangela wrote:
    > > Rob Hoelz wrote:
    > >> Functions as an l-value (must return reference)

    > >
    > > I suspect that the above statement is _NOT_ corrrect. Here is an
    > > example that compiles on Comeau that shows a function being used as an
    > > l-value returning a pointer, _NOT_ a reference.

    >
    > The function is not returning an lvalue, it is returning a pointer.
    > Dereferencing that pointer (which is an rvalue) yields an lvalue.
    >
    > > I suspect this code
    > > would compile on C compiler as well (substituting printf() etc. for
    > > cout etc of course).
    > >
    > > #include <iostream>
    > > int * func();
    > > int main()
    > > {
    > > *func() = 20; // will print 10 the first time
    > > func(); // should print 20 the second time.
    > > return 0;
    > > }
    > >
    > > int * func()
    > > {
    > > static int i = 10;
    > > std::cout << i << std::endl;
    > > return &i;
    > > }

    >
    > --
    > Clark S. Cox III
    >


    The end result is the same - that a function can be invoked on the
    left hand side of an assignment operator. So even why bother mentioning
    the fact that a C++ function can return an L-value?
    blangela, Dec 23, 2006
    #17
  18. blangela wrote:
    > Clark S. Cox III wrote:
    >> blangela wrote:
    >>> Rob Hoelz wrote:
    >>>> Functions as an l-value (must return reference)
    >>> I suspect that the above statement is _NOT_ corrrect. Here is an
    >>> example that compiles on Comeau that shows a function being used as an
    >>> l-value returning a pointer, _NOT_ a reference.

    >> The function is not returning an lvalue, it is returning a pointer.
    >> Dereferencing that pointer (which is an rvalue) yields an lvalue.
    >>
    >>> I suspect this code
    >>> would compile on C compiler as well (substituting printf() etc. for
    >>> cout etc of course).
    >>>
    >>> #include <iostream>
    >>> int * func();
    >>> int main()
    >>> {
    >>> *func() = 20; // will print 10 the first time
    >>> func(); // should print 20 the second time.
    >>> return 0;
    >>> }
    >>>
    >>> int * func()
    >>> {
    >>> static int i = 10;
    >>> std::cout << i << std::endl;
    >>> return &i;
    >>> }

    >
    > The end result is the same - that a function can be invoked on the
    > left hand side of an assignment operator. So even why bother mentioning
    > the fact that a C++ function can return an L-value?


    Because it is a difference between the two languages; and that is what
    the OP was looking for.


    --
    Clark S. Cox III
    Clark S. Cox III, Dec 23, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardip Wadhwa

    Diff. Windows Services

    Hardip Wadhwa, May 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    524
    Eric Marvets
    May 24, 2004
  2. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    508
    Joe Smith
    Jun 29, 2004
  3. Berrucho
    Replies:
    2
    Views:
    631
    Infant Newbie
    Dec 5, 2003
  4. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,918
    Steve Fulton
    Aug 31, 2004
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    185
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page