diff C C++

R

Rob Hoelz

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!
 
M

Micah Cowan

Rob said:
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...

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
 
C

Clark S. Cox III

Rob said:
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)
 
R

Rob Hoelz

Clark S. Cox III said:
Min-scope declarations are in C99 as well

I've based this off of C89.
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.
Inline functions are in C99

Once again, C89.
What do you mean by this?

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


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

I mean include statements like this:
#include <cstring>
#include said:
These are available in C99 also

As may unions.

Thanks for this addition!
Yes, it is a keyword. (C99 has the _Bool keyword, BTW)


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

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

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

Rob Hoelz

Ian Collins said:
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
 
I

Ian Collins

Rob said:
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;
}
 
R

Rob Hoelz

Ian Collins said:
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...
 
S

Stuart Redmann

[snipped items from his list]
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
 
R

Rob Hoelz

Stuart Redmann said:
[snipped items from his list]
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

#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.
 
M

Marcus Kwok

Rob Hoelz said:
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
 
M

Mike

Rob said:
But that's a function pointer, not a function...

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.
 
C

Clark S. Cox III

Ian said:
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() {}
};
 
B

blangela

Rob said:
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;
}
 
C

Clark S. Cox III

blangela said:
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.
 
B

blangela

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

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?
 
C

Clark S. Cox III

blangela said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top