C++ 中的“&”、“*”、“.”、“->”、“:”和“::”运算符介绍

C++ 中的“&”、“*”、“.”、“->”、“:”和“::”运算符介绍_c++中->-CSDN博客

以下所有代码均在C++6.0环境下编译运行。是对上面这一行链接内容的上机实践。

1. 

#include "stdafx.h"

#include <iostream.h>

int main() {
int a = 10;
int* ptr = &a; // 取变量a的地址并赋值给指针ptr
cout << "a 的地址: " << ptr << endl;
return 0;
};

 2.

#include "stdafx.h"

#include <iostream.h>

int main() {
int a = 5; // 二进制表示:0101
int b = 3; // 二进制表示:0011
int result = a & b; // 按位与操作,结果为0001(1)
cout << "按位与结果: " << result << endl;
return 0;
}

 3.

#include "stdafx.h"

#include <iostream.h>

int main() {
int a = 10;
int& ref = a; // 定义一个引用ref,它是变量a的别名
ref = 20; // 修改引用ref的值,也会修改变量a的值
cout << "a 的值: " << a << endl;
return 0;
}

 

4. 可用作函数参数的引用传递

C++中的引用传递也称为按引用传递或引用参数。这种方式允许函数通过引用来操作原始变量,而不是通过复制变量的值。

通过引用传递函数参数,可以将一个变量作为参数传递给函数,并在函数内部直接操作该变量,而无需创建副本。

函数参数声明为引用类型(使用&符号),即可实现引用传递。

#include "stdafx.h"

#include <iostream>

using namespace std;

//函数参数声明为引用类型(使用&符号),即可实现引用传递。
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int x = 10;
int y = 20;

cout << "交换之前:" << endl;
cout << "x 的值: " << x << endl;
cout << "y 的值: " << y << endl;

swap(x, y); // 调用swap函数,传递x和y的引用

cout << "交换之后:" << endl;
cout << "x 的值: " << x << endl;
cout << "y 的值: " << y << endl;

return 0;
}

 

 

"*"运算符

在C++中,星号“*”按不同的应用场景,可以有多种用途,以下是其用法及示例说明。

1.乘法运算符:a * b;表示将a和b的值相乘,结果存储在c中。下面是示例:

#include <iostream>
using namespace std;

int main() {
int a = 10;
int b = 2;

// 使用星号符作为乘法运算符
int result1 = a * b;
cout << "result1 = " << result1 << endl; // 输出 result1 = 20

return 0;
}

 

2.指针类型声明符:char* ptr1;表示声明一个指向字符类型的指针变量ptr1,该变量存储指向字符类型数据的内存地址。下面是示例:

#include <iostream>
using namespace std;

int main() {
int a = 10;

// 使用星号符作为指针类型声明符
int* ptr1 = &a; // 声明一个指向整型变量 a 的指针
cout << "ptr1 = " << ptr1 << endl; // 输出 ptr1 = 0x6ffe14 (取决于编译器分配的内存地址,以16进制表示)

return 0;
}

 3. 指针解引用运算符/间接寻址运算符:*ptr1表示使用指针变量ptr1存储的地址,从内存中读取它所指向的值。下面是示例:

#include <iostream>
using namespace std;

int main() {
int a = 10;

// 使用星号符作为指针类型声明符
int* ptr1 = &a; // 声明一个指向整型变量 a 的指针
// 使用星号符作为指针解引用运算符
cout << "*ptr1 = " << *ptr1 << endl; // 输出 10

return 0;
}

 

 

 

2. 访问结构体的成员变量或成员函数:

#include <iostream>
#include <math.h>
using namespace std;

struct Point {
double x;
double y;
double distance() { return sqrt(x * x + y * y); }
};

int main() {
Point p = {3, 4};
double d = p.distance();
cout << "Distance: " << d <<endl;
}

 3. 调用类的静态函数或访问静态变量:

#include <iostream>
using namespace std;

class MyClass {
public:
static int count;
static void printCount() { cout << count << endl; }
};

int MyClass::count = 10; //静态变量需要在类定义外进行初始化

int main() {
MyClass::count++;
MyClass::printCount();
return 0;
}

 

4. 访问命名空间中的变量或函数:

#include <iostream>
using namespace std;

namespace MyNamespace {
int x;
void foo() { cout << "Hello from MyNamespace!" << endl; }
};

int main() {
MyNamespace::x = 10;
MyNamespace::foo();
return 0;
}

 

#include <iostream>
using namespace std;

namespace MyNamespace {
int x=3;
void foo() { cout << "Hello from MyNamespace!" << x << endl; }
};

int main() {
MyNamespace::x = 10;
MyNamespace::foo();
return 0;
}

 

":" 运算符有多种用法

1. 用于初始化成员变量

在类的构造函数中,可以使用 ":" 运算符来初始化成员变量。例如:

#include<iostream>
using namespace std;

class MyClass {
public:
int a,b,c; // 成员变量
MyClass(int x, int y, int z):a(x), b(y), c(z){} // 构造函数
};

int main() {
MyClass obj(1, 2, 3); // 创建对象并初始化成员变量
cout << "a: " << obj.a << endl;
cout << "b: " << obj.b << endl;
cout << "c: " << obj.c << endl;
return 0;
}

 

2. 用于调用父类的构造函数

如果一个子类继承自一个父类,那么在定义子类的构造函数时,可以使用 ":" 运算符来调用父类的构造函数。例如

#include <iostream>
using namespace std;

class Parent {
public:
Parent(int x) {
cout << "Parent constructor called with parameter " << x << endl;
}
};

class Child : public Parent {
public:
Child(int y) : Parent(y) {
cout << "Child constructor called with parameter " << y << endl;
}
};

int main() {
Child c(10);
return 0;
}

 

3. 用于标识基类访问限定符

在派生类中,可以使用 ":" 运算符将基类的 public、protected 或 private 访问限定符进行分类。例如:

#include <iostream>
using namespace std;

class Base {
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};

class Derived : public Base {
public:
void setValues() {
public_var = 1; // 可以访问公有成员
protected_var = 2; // 可以访问保护成员
// private_var = 3; 不能访问私有成员
}
};

int main() {
Derived d;
d.setValues();
cout << "Public variable: " << d.public_var << endl;
// cout << "Protected variable: " << d.protected_var << endl; 不能访问
// cout << "Private variable: " << d.private_var << endl; 不能访问
return 0;
}

 

"::" 运算符

作用域解析运算符或命名空间限定符,用于访问命名空间、全局变量或静态成员变量。例如:

//作用域解析运算符

#include <iostream>
using namespace std;

namespace MyNamespace {
int a = 10;
}

int main() {
cout << "MyNamespace::a = " << MyNamespace::a << endl;
return 0;
}

 

"::" 运算符的多种用法

1. 访问命名空间中的变量或函数:

#include <iostream>
//using namespace std;

// 自定义命名空间
namespace MyNamespace {
int add(int a, int b) { return a + b; }
};

int main() {
std::cout << "Hello World!" << std::endl; // 访问 std 命名空间中的 cout 对象
int result = MyNamespace::add(2, 3); //访问自定义命名空间中的函数
std::cout << "result = " << result << std::endl;

return 0;
}

 

2. 访问类中的静态成员变量或静态成员函数:

#include <iostream>
using namespace std;

class MyClass {
public:
static int count;
static void printCount() { cout << count << endl; }
};

int MyClass::count = 10; //静态变量需要在类定义外进行初始化

int main() {
MyClass::count++; // 访问 MyClass 类中的 count 静态成员变量
MyClass::printCount(); // 访问 MyClass 类中的 printCount 静态成员函数
return 0;
}

3. 访问全局变量或函数:

#include <iostream>
using namespace std;

int global_var = 10; // 全局变量

void printGlobalVar() { // 全局函数
cout << "Global variable: " << global_var << endl;
}

class MyClass {
public:
void printGlobalVar() { // 成员函数
int global_var = 5; // 局部变量
cout << "Local variable: " << global_var << endl;
cout << "Global variable: " << ::global_var << endl; // 使用"::"运算符访问全局变量
::printGlobalVar(); // 使用"::"运算符访问全局函数
}
};

int main() {
MyClass myObj;
myObj.printGlobalVar();
return 0;
}

 

posted @ 2023-12-08 11:54  Thomas2023  阅读(94)  评论(0编辑  收藏  举报