C++学习笔记(一)
1. 仿函数(函数对象) operator() 的用法
c++工程文件结构:

test.h具体:
#include<iostream>
using namespace std;
class V_3d{
public:
	double x,y,z;
	V_3d(double x_,double y_, double z_):x(x_),y(y_),z(z_)
	{
		cout<<x<<","<<y<<","<<z<<endl;
	} 
	
	 bool operator() (){
	 	cout<<"你好啊,重载的()"<<endl;
	 	if(x>0&&y>0&&z>0)
	 		return 1;
	 	else
	 		return 0;
	 }
};
test.cpp具体:
#include"test.h"
using namespace std;
int main(int argc,char* argv[]){
cout<<"hello world, hello operator"<<endl;
V_3d p0={-10,0,0};
V_3d p1={0,0,0};
V_3d p2={2,3,4};
V_3d p3={3,5,7};
cout<<p1.x<<","<<p1.y<<","<<p1.z<<endl;
cout<<p3.x<<","<<p3.y<<","<<p3.z<<endl;
cout<<p0()<<endl;
cout<<p1()<<endl;
cout<<p3()<<endl;
return 0;
}
CMakeLists.txt 具体:
cmake_minimum_required(VERSION 3.10)
project(test)
include_directories(include)
add_executable(test test.cpp)
运行结果

 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号