1、C++中的可变参数
#include <iostream>
#include <stdarg.h> // 可变参数的支持
using namespace std;
void testParams(int size, ...) {
va_list vp;//可变参数的动作
//开启
//参数一:可变参数开始的动作vp
//参数二:内部需要一个 存储地址用的参考值,如果没有第二个参数,内部他无法处理存放参数信息
va_start(vp, size);
for (int i = 0; i < size; i++) {
//取出可变参数
int number = va_arg(vp, int);
cout << number << endl;
}
//关闭
va_end(vp);
}
int main() {
testParams(4, 2, 3, 4, 5);
return 0;
}
2
3
4
5
2、C++ static关键字
class Dog {
public:
char *name;
//错误写法,静态变量不能设置默认值
//static int weight = 0;
static int weight;
static void addWeight();
void addWeight2();
};
/**
* static修饰的静态函数
*/
void Dog::addWeight() {
//静态函数,不能调用普通函数
// addWeight2();
//静态函数,不能访问静态属性
// cout<<name<<endl;
cout <<"addWeight "<< weight << endl;
}
/**
* 普通函数
*/
void Dog::addWeight2() {
//普通函数可以直接访问静态属性
cout <<"addWeight2 "<< weight << endl;
//普通函数可以直接访问静态函数
// addWeight2();
}
//静态属性要先在类里声明,然后再在类外初始化
int Dog::weight = 100;
int main() {
Dog dog;
dog.addWeight();
dog.addWeight2();
Dog::addWeight();
cout <<"main "<< Dog::weight << endl;
return 0;
}
addWeight 100
addWeight2 100
addWeight 100
main 100
static关键小结: 1、绝大多数和Java一样,静态的函数或者成员,可以直接通过类来访问(Dog::addWeight()),也可以支持对象访问( dog.addWeight();),但是普通函数不能访问静态的函数或者成员。 2、有个特殊的地方就是C++中的静态成员属性,要先在类中声明,然后在类外中初始化。不能直接在类中设置默认值。
3、C++ this关键字
class Student {
private:
int age;
public:
void setAge(int age);
int getAge()const;
Student(int age);
};
#include "Student.h"
void Student::setAge(int age) {
this->age = age;
}
int Student::getAge() const {
return this->age;
}
/**
* this 等价于 const * Student student ,常量指针,不能修改地址,可以修改值。
* @param age
*/
Student::Student(int age) {
this->age = age;
cout << age << endl;
}
int main() {
Student student(123);
Student student1(345);
return 0;
}
123
345
在Java中this关键字我们可以理解为当前对象。在 C++中也可以这么理解,同时在C++中this等价于当前对象的常量指针。因此我们可以修改对象中的值,但是不能修改地址。(this 等价于 const * Student student ,常量指针,不能修改地址,可以修改值。)
4、友元函数
当我们的一个类中的属性是私有的,我们在自己的函数中像通过对象调用这个私有属性是没办法的,那么我们可以在类中使用friend关键字定义友元函数和我们自己的函数一样。这样就能访问私有属性。
class Student {
private:
int age;
public:
Student(int age);
friend void showAge(Student *student, int age);
};
#include "Student.h"
/**
* this 等价于 const * Student student ,常量指针,不能修改地址,可以修改值。
* @param age
*/
Student::Student(int age) {
this->age = age;
// cout << age << endl;
}
void showAge(Student *student, int age) {
student->age = age;
cout << student->age << endl;
}
int main() {
Student student(0);
showAge(&student, 100);
return 0;
}
我们自己定义的showAge函数,传入了Student 类型指针,我们想访问私有成员属性age,那么我们之间在Student类中定义一个友远函数即可。指针使用->访问,引用直接使用.号访问
friend void showAge(Student *student, int age);
当然这里我们看到了使用的是指针,直接使用引用也可以。
friend void showAge(Student &student, int age);
void showAge(Student &student, int age) {
student.age = age;
cout << student.age << endl;
}
int main() {
Student student(0);
showAge(student, 100);
return 0;
}
5、友元类
class ImageView {
private:
int size;
//定义友元类,使得class可以访问自己的私有属性
friend class Class;
};
class Class {
public:
void updateSize(ImageView &imageView, int size) {
imageView.size = size;
cout << imageView.size << endl;
};
};
int main() {
ImageView imageView;
Class aClass;
aClass.updateSize(imageView,1000);
return 0;
}
1000
当一个类中的成员是私有的,我们在另外一个类中创建这个类的对象,访问其私有属性是不行的。那么我们可以通过定义友远类,这样使得可以访问。 有点类似Java中的Class,普通的对象在外部我们是无法访问其私有成员的,但是我们可以通过获取该类的Class对象,来访问修改其私有属性。 ###6、操作符重载 在c++中我们可以重载操作符实现我们需要的功能
- 重载+号操作符 模拟实现两个对象相加
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Dog {
private:
int age;
string name;
public:
int getAge() {
return this->age;
}
string getName() {
return this->name;
}
Dog(int age, string name) : age(age), name(name) {
}
/**
* 重载+号操作符
* @param dog
* @return
*/
Dog operator+(const Dog &dog) {
int newAge = this->age + dog.age;
//字符串拼接
string newName = this->name + dog.name;
return Dog(newAge, newName);
}
};
int main() {
Dog dog = Dog(10, "diphthong") + Dog(20, "hominin");
cout << dog.getAge() << "-" << dog.getName() << endl;
return 0;
}
30-diphthonghominin
- 重载[]操作符 模拟实现一个简单的ArrayList
class ArrayList {
private:
int index = 0;
int list[10];
public:
void add(int value) {
this->list[index] = value;
this->index++;
}
int operator[](const int &position) {
return (this->list[position]);
}
};
ArrayList list;
list.add(1);
list.add(2);
list.add(3);
cout<<list[0]<<endl;
cout<<list[1]<<endl;
cout<<list[2]<<endl;
1
2
3