Where C and C++ Differ

Where C and C++ Differ

转自:http://cprogramming.com/tutorial/c-vs-c++.html

C++ was based on C and retains a great deal of the functionality. C++ does not retain complete source-level compatability with C. There are a few gotchas for C++ programmers trying to write C code, and C programmers trying to compile with a C++ compiler.

Gotchas for a C programmer using C++

Implicit Assignment from void*

You cannot implicitly assign from a void* to any other type. For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C)

int *x = malloc(sizeof(int) * 10);

but it won't compile in C++. (Try it yourself!)

The explanation from Bjarne Stroustrup himself is that this isn't type safe. What this means is that you can have a void* that points to anything at all, and if you then assign the address stored in that void* to another pointer of a different type, there isn't any warning at all about it.

Consider the following:

int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;

When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes. Forcing a cast from a void pointer makes the programmer pay attention to these things.

Freeing arrays: new[] and delete[]

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:

int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );

and you always release the memory in the same way:

free( x );
free( x_array );

In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).

int *x = new int;
int *x_array = new int[10];

delete x;
delete[] x;

The short explanation is that when you have arrays of objects, delete[] with properly call the destructor for each element of the array, whereas delete will not.

You must declare functions before use

Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:

#include <stdio.h>
int main()
{
    foo();
    return 0;
}

int foo()
{
    printf( "Hello world" );
}

Gotcha for a C++ programmer using C

Structs and Enums

You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this

struct a_struct
{
    int x;
};

a_struct struct_instance;

and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance:

struct a_struct struct_instance;

In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs:

typedef struct struct_name
{
    /* variables */
} struct_name_t;

Now you can declare a struct with

struct_name_t struct_name_t_instance;

But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a a pointer to the struct.

typedef struct struct_name
{
    struct struct_name instance;
    struct_name_t instance2; /* invalid!  The typedef isn't defined yet */
} struct_name_t;

C++ has a much larger library

C++ has a much larger library than C, and some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:

% g++ foo.cc

or

% gcc foo.c -lm

No Boolean Type

C does not provide a native boolean type. You can simulate it using an enum, though:

typedef enum {FALSE, TRUE} bool;

main Doesn't Provide return 0 Automatically

In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:

int main()
{
    print( "Hello, World" );
}

but in C, you must manually add it:

int main()
{
    print( "Hello, World" );
    return 0;
}

-----

 
### 关于"differ"在编程或IT上下文中的含义 在编程和信息技术领域,“differ”通常用来描述两个对象、数据结构、算法或其他技术组件之间的差异。这种差异可以体现在多个方面,例如类型、行为、性能或者实现方式。 #### 类型上的差异 (Type Difference) 当提到“generic programs differ in types”,这意味着泛型程序的设计独立于任何特定的数据类型[^1]。通过这种方式,程序员可以在运行时指定具体的类型参数来实例化通用代码的行为。因此,在这里“differ”的意义是指不同类型的适用性和灵活性。 #### 跨平台兼容性的区别 (Cross-platform Compatibility Differences) 对于C语言开发人员来说,在处理指针与整数转换问题时会遇到一些挑战,尤其是在64位系统环境下。“Handling Integer-to-Pointer Casting Issues...Conclusion”指出这种方法具有良好的跨体系结构可移植性[^2]。这里的“differ”隐含着各种硬件环境之间可能存在的细微差别以及解决方案如何适应这些变化的能力。 #### 面向对象设计模式下的属性对比 (Object-Oriented Design Attribute Comparison) 考虑一个简单的例子——`BankAccount`类定义了一个基本的银行业务逻辑模型[^3]。如果存在另一个派生自该基类的新版本,则两者可能会因为新增功能而表现出某些方面的不一致之处;比如子类别增加了额外的安全验证机制或是支持多币种操作等功能扩展点上形成显著的区别。这正是面向对象继承关系中常见的“differences”。 ```java public class BankAccount { private double balance; private int limit; // maximum overdraft allowed private String accountId; public BankAccount(double initialBalance, int maxOverdraftLimit, String id){ this.balance = initialBalance; this.limit = Math.abs(maxOverdraftLimit); this.accountId=id; } public void deposit(double amount){ ... } public boolean withdraw(double amount){ ... } public double checkBalance(){ return this.balance;} } ``` 上述代码片段展示了基础版银行账户管理系统的部分实现细节。假设我们创建了高级别的VIP客户专属服务接口VipCustomerServiceInterface并由具体业务单元去完成实际执行流程的话,那么这两个层次间必然存在着某种程度的功能侧重方向调整即所谓的“difference”. ### 总结 综上所述,“differ”一词广泛应用于阐述软件工程实践中各类实体间的异同比较场景之中,无论是抽象层面的概念区分还是具体编码过程里的语法特性考量均有所体现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值