-
Notifications
You must be signed in to change notification settings - Fork 2
/
math.h
96 lines (92 loc) · 3.33 KB
/
math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef _MATH_H_
#define _MATH_H_
#include <iostream>
class MyMath {
//protected:
private:
int number;
int *data;
int data_length;
public:
static int user;
static MyMath instance;
int getNumber()const; //with const qualifier
void getData(int *buf) const; //const function, denoted with the keyword const after a function declaration
//, makes it a compiler error for this class function to change a member variable of the class.
//However, reading of a class variables is okay inside of the function
//, but writing inside of this function will generate a compiler error.
//To allow some of the variables to be writable even when the function is marked as a const function
//, these class variables are marked with the keyword mutable. (C++11)
void setNumber(int num);
void setData(int *src, int length);
#if 1
static MyMath& decode(MyMath math);
static MyMath& decodeWithRef(MyMath& math);
static MyMath decodeWithRefRVO(MyMath& math);
static MyMath& decodeWithRefReturnRef(MyMath& math);
#else
static MyMath& decode(MyMath math) {
std::cout << "[MyMath::decode()]: ... got math:" << &math << std::endl;
//instance = *(new MyMath(math)); //this new object would be a leak
instance = math;
//MyMath instance = math;
std::cout << "[MyMath::decode()]: ... return instance:" << &instance << std::endl;
return instance;
}
static MyMath& decodeWithRef(MyMath& math) {
std::cout << "[MyMath::decodeWithRef()]: ... got math:" << &math << std::endl;
//instance = *(new MyMath(math)); //this new object would be a leak
instance = math;
//MyMath instance = math;
std::cout << "[MyMath::decodeWithRef()]: ... return instance:" << &instance << std::endl;
return instance;
}
#endif
#if 0
MyMath* operator+(MyMath* math);
#elif 0
void operator+(MyMath& math);
#else
/**
* Although canonical form of pre-increment/pre-decrement returns a reference
* , as with any operator overload, the return type is user-defined;
*/
//MyMath operator+(MyMath& math);
MyMath& operator+(MyMath& math);
#endif
//MyMath* operator++();
MyMath& operator++();
MyMath& operator++(int);
//MyMath* operator--();
MyMath& operator--();
MyMath& operator--(int);
bool operator==(const MyMath& math);
bool operator!=(const MyMath& math);
bool operator<(const MyMath& math) const;
bool operator>(const MyMath& math) const;
#if 0
MyMath* operator=(MyMath &math);
#else
MyMath& operator=(const MyMath &math);
#endif
//friend void operator+(MyMath &math1, MyMath &math2);
friend int operator-(MyMath& m1, MyMath& m2);
friend void operator++(MyMath &math);
friend std::ostream &operator<<(std::ostream &stream, MyMath &math);
friend std::istream &operator>>(std::istream &stream, MyMath &math);
MyMath(); //default constructor
MyMath(int num);
MyMath(MyMath& math); //copy constructor with a reference to lvalue
MyMath(MyMath&& math); //move constructor with a reference to rvalue
MyMath(const MyMath &math); //const type or leads vector::push_back() err
static void setUser(int user_id){
user = user_id;
}
static int getUser(){
return user;
}
~MyMath();
};
std::istream &operator>>(std::istream &stream, MyMath &math);
std::ostream &operator<<(std::ostream &stream, MyMath &math);
#endif