C++ 名称混淆和Extern “C”

C++ 名称混淆和Extern “C”

Java和C++编程语言都支持方法和函数的重载。函数的重载可以通过参数的数量或数据类型的差异来区分。真正的问题是,Java和C++编程语言的编译器如何区分一个函数和另一个函数。

对于这个复杂问题的简单答案是,编译器会从其端添加更多信息。在执行过程中,它将方法或参数保存在堆栈内存中。C++编译器没有任何与其他编译器不同的特定技术,它使用相同的名称混淆概念来解决函数重载的问题。

C++中的函数重载-1

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of function overloading with its relevant code 
// and output supported by valid syntax wherever necessary
#include 
using namespace std;

// function add in its avatar of adding two variables here of integer data
void add(int a, int b)
{
cout << "sum of two integer variables is= " << (a + b);
}
// function add in its avatar of adding two variables here of double data
void add(double a, double b)
{
    cout << endl << "sum of two double variables is= " << (a + b);
}

// the main driver code functionality starts from here
int main()
{
    add(130, 92);
    add(85.3, 36.2);

    return 0;
// the main driver code functionality ends from here
}

输出:

The sum of two integer variables is= 222
The sum of the two double variables is= 121.5

在C++中的函数重载 -2

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of function overloading with its relevant code 
// and output supported by valid syntax wherever necessary
#include 
using namespace std;
// function add in its avatar of adding two variables here of integer data
void add(int a, int b)
{
cout << "sum of two integer variables is= " << (a + b);
}
// function add in its avatar of adding three variables here of integer data
void add(int a, int b, int c)
{
    cout << endl << "sum of three integer variables is= " << (a + b + c);
}

// the main driver code functionality starts from here
int main()
{
    add(410, 20);
    add(55, 66, 44);

    return 0;
// the main driver code functionality ends from here
}

输出:

The sum of two integer variables is= 430
The sum of three integer variables is= 165

在C++中的函数重载-3

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of function overloading with its relevant code 
// and output supported by valid syntax wherever necessary
#include 
using namespace std;

void print(int i) {
cout << " Here we have the integer  " << i << endl;
}
void print(double f) {
cout << " Here we have the float " << f << endl;
}
void print(char const *c) {
cout << " Here we have the character " << c << endl;
}
// the main driver code functionality starts from here
int main() {
print(100);
print(100.100);
print("hundred");
return 0;
// the main driver code functionality ends from here
}

输出:

 Here we have the integer  100
 Here we have the float 100.1
 Here we have the character hundred

名称混淆和C++中的Extern ‘C’

演示代码

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of name mangling with its relevant code 
int __f_v(void) { return 1; }

int __f_i(int) { return 0; }

void __g_v(void) { int i = __f_v(), j = __f_i(0); }

输出:

/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status

C++ 代码

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of name mangling with its relevant code 
// showing C code doesn't support name mangling
int printf(const char* format,...);

// The main driver code functionality starts from here
int main()
{
    printf("javaTpoint");
    return 0;
// The main driver code functionality ends from here
}

输出:

/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status

Extern C

// Here we are writing down the C++ programming language code to 
// demonstrate the concept of name mangling with its relevant code 
// C++ code to demonstrate Extern "C"

extern "C" {
int printf(const char* format,...);
}

// The main driver code functionality starts from here
int main()
{
    printf("javaTpoint");
    return 0;

// The main driver code functionality ends from here

}

输出:

javaTpoint

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程