找回密码
 立即注册
搜索
热搜: 活动 交友
楼主: Ray

面向对象程序设计复习贴

[复制链接]

44

主题

139

回帖

5476

积分

版主

积分
5476
 楼主| 发表于 昨天 13:32 | 显示全部楼层
+-*/%^&|~
const T operatorX(const T& l, const T& r);
!&& || < <= >= >
bool operatorX(const T& l, const T& r);
[]
E& T: perator [] (int index);

44

主题

139

回帖

5476

积分

版主

积分
5476
 楼主| 发表于 昨天 15:09 | 显示全部楼层
本帖最后由 Ray 于 6-27-2026 15:11 编辑

Number operator++(int n){ Number tmp=*this; ++(*this); return tmp;}


Number& ++operator(){this->value+=1; return *this;}

44

主题

139

回帖

5476

积分

版主

积分
5476
 楼主| 发表于 昨天 19:30 | 显示全部楼层
#include <iostream>
using namespace std;


void print(int x) { cout << "Normal int: " << x << endl; }


template <typename T>
void print(T x) { cout << "Template: " << x << endl; }


int main() {
    print(10);      // (1)
    print(10.5);    // (2)
    print<int>(20); // (3)
    return 0;
}


Normal int: 10
Template: 10.5
Template: 20



44

主题

139

回帖

5476

积分

版主

积分
5476
 楼主| 发表于 昨天 19:31 | 显示全部楼层
template <typename T>
class Pair {
private:
    T first;
    T second;
public:
    Pair(T a, T b);
    T getMax() const;
};


template <class T>
Pair<T>:air(T a, T b):first(a),second(b){}


template <class T>
T Pair<T>::getMax()const{return (first>second) ? first : second;}

44

主题

139

回帖

5476

积分

版主

积分
5476
 楼主| 发表于 昨天 20:54 | 显示全部楼层
class Guard {
public:
    ~Guard() { cout << "~Guard "; }
};


void inner() { throw 404; }


void outer() {
    Guard g;
    try {
        inner();
    } catch (int e) {
        cout << "Catch in outer ";
        throw;
    }
}


int main() {
    try {
        outer();
    } catch (int e) {
        cout << "Catch in main ";
    }
}


outer()先创建了g,此后inner()产生了error,导致g被析构,输出第一行:
~Guard
随后outer()内部发生了catch,输出第二行:
Catch in outer
随后继续throw 404到外面,main()内部发生了catch,输出第三行:
Catch in main


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|RealDevClub ( 沪ICP备2024093864号-1 )

GMT+8, 6-28-2026 02:18 , Processed in 0.063875 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表