-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpful.txt
68 lines (55 loc) · 1.33 KB
/
helpful.txt
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
class MyClass {
public:
MyClass() {
std::cout << "Constructor called" << std::endl;
}
~MyClass() {
std::cout << "Destructor called" << std::endl;
}
MyClass(const MyClass& other) {
std::cout << "Copy constructor called" << std::endl;
}
};
void foo(MyClass obj) {
std::cout << "Inside foo function" << std::endl;
}
int main() {
MyClass obj;
foo(obj);
std::cout << "Back in main" << std::endl;
class MyPointer {
private:
int* ptr;
public:
MyPointer(int* p) : ptr(p) {}
// Post-increment operator
MyPointer operator++(int) {
MyPointer temp = *this;
++(*this);
return temp;
}
// Pre-increment operator
MyPointer& operator++() {
++ptr;
return *this;
}
// Subtraction operator
int operator-(const MyPointer& other) const {
return *ptr - *(other.ptr);
}
// Assignment operator
MyPointer& operator=(const MyPointer& other) {
if (this != &other) {
*ptr = *(other.ptr);
}
return *this;
}
// Accessor for dereferencing
int operator*() const {
return *ptr;
}
};
int main() {
int a = 5, b = 7, c = 2;
MyPointer p1(&a), p2(&b), p3(&c);
p1 = p2++ - ++p3;