Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about errors when I change my C code to C++

Author: Matt Taylor

Date: 07:48:19 02/28/03

Go up one level in this thread


On February 27, 2003 at 09:55:35, Tom Likens wrote:

>On February 26, 2003 at 17:07:45, Bo Persson wrote:
>
>[--snip--]
>
>
>>>I see that I did not include data.h in data.c but it caused me no problem
>>>with c files
>>
>>You have found an actual difference between the languages!
>>
>>In C++, const variables are considered local unless declared otherwise. In C,
>>all global (file level) variables are extern by default.
>>
>>Bo Persson
>>bop2@telia.com
>
>
>No, no- C++ is just a superset of C.  There is *absolutely*
>no differences between them :-) :-) :-) :-) :-) ;-)
>
>regards,
>--tom

Actually there are many subtle differences. Here's an example:

C:
int *array;
array = malloc(size);

C++:
int *array;
array = malloc(size); // error
array = (int *) malloc(size);
array = new int[size]; // preferred

Also, I have a fair amount of code that looks like this:

char *byteptr;

// some stuff
if (blah)
    x = *((int *) byteptr)++;
else
    x = (int) *byteptr++;

This does not work in C++. This example actually came from an x86 disassembler I
wrote where it was extremely convenient to break type rules again & again. The
only solution I have found in C++ is to use a union with pointers of varying
type.

You are right that most of C and C++ is the same, but some acceptable C programs
will not compile as C++ for various reasons.

-Matt



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.