主頁 > 後端開發 > #《Essential C++》讀書筆記# 第四章 基于物件的編程風格

#《Essential C++》讀書筆記# 第四章 基于物件的編程風格

2020-09-17 17:35:37 後端開發

基礎知識

Class的定義由兩部分組成:class的宣告,以及緊接在宣告之后的主體,主體部分由一對大括號括住,并以分號結尾,主體內的兩個關鍵字public和private,用來標示每個塊的“member訪問權限”,Public member可以在程式的任何地方被訪問,private member只能在member function或是class friend內被訪問,

class的前置宣告(forward declaration),將class名稱告訴編譯器,并未提供此class的任何其他資訊(像class支持的操作行為及所包含的data member等),前置宣告使我們得以進行類指標(class pointer)的定義,或以此class作為資料型別,

所有member function都必須在class主體內進行宣告,至于是否同時進行定義,可自由決定,如果要在class主體內定義,這個member function會自動被視為inline函式,要在class主體之外定義member function,必須使用特殊的語法,目的在于分辨該函式究竟屬于哪一個class,使用雙冒號“::”,即所謂的class scope resolution(類作用域決議)運算子,

對于inline函式而言,定義與class主體內或主體外,并沒有什么分別,然而就像non-member-inline function一樣,它也應該放在頭檔案中,class定義一起inline member function通常都會被放在與class同名的頭檔案中,non-inline member function應該在程式代碼檔案中定義,該檔案通常與class同名,其后接著擴展名.C、.cc、.cpp或.cxx(x代表橫放的+),

初始化data member,魔法不會自動產生,編譯器不會自動為我們處理,如果我們提供一個或多個特別的初始化函式,編譯器就會在每次class object被定義出來時,呼叫適當的函式加以處理,這些特別的初始化函式稱為constructor(建構式),Constructor的函式必須與class名稱相同,語法規定,constructor不應指定回傳型別,亦不用回傳任何值,它可以被多載(overloaded),最簡單的constructor是所謂的default constructor,它不需要任何引數(argument),(沒有引數不需要加括號,加括號會將其解釋為一個函式,這是為了兼容C所致),

和constructor對立的是destructor,所謂destructor乃是用戶自定義的一個class member,一旦某個class提供有destructor,當其object結束生命時,便會自動呼叫destructor處理善后,Destructor主要用來釋放在constructor中或物件生命周期中分配的資源,Destructor的名稱有嚴格規定:class名稱再加上‘~’前綴,它絕對不會有回傳值,也沒有任何引數,由于其引數串列是空的,所以也絕不可能別多載(overloaded),Destructor并非絕對必要,我們沒有義務非得提供destructor,事實上,C++編程的最難部分之一,便是了解何時需要定義destructor而何時不需要,

當我們設計class時,必須問問自己,在此class之上進行“成員逐一初始化”的行為模式是否適當?如果答案肯定,我們就不需要另外提供copy constructor,如果答案否定的,我們就必須另行定義copy constructor,并在其中撰寫正確的初始化操作,如果有必要為某個class撰寫copy constructor,那么同樣有必要為它撰寫copy assignment operator,

static member function 可以在“與任何物件都無瓜葛”的情形之下呼叫,member function 只有在“不訪問任何non-static member”的條件下才能夠被宣告為static,宣告方式是在宣告之前加上關鍵字static,當我們在class主體外部進行member function的定義時,無需重復加上關鍵字static,這個規則也適用于static data member,

運算子多載規則:

  • 不可以引入新的運算子,除了“.”、“.*”、“::”、“?:”四個運算子,其他運算子皆可被多載,
  • 運算子的運算元(operand)個數不可以改變,每個二元運算子都需要兩個運算元,每個一元運算子都需要恰好一個運算元,因此,我們無法定義出一個equality運算子,并令它接受兩個以上或兩個以下的運算元,
  • 運算子的優先級(precedence)不可改變,例如,除法的運算優先級永遠高于加法,
  • 運算子函式的引數串列中,必須至少有一個引數為class型別,也就是說,我們無法為諸如指標之類的non-class型別,重新定義其原已存在的運算子,當然無法為它引進新的運算子,

對于increment遞增運算子(或遞減)的多載,分為前置和后置兩個版本,前置版的引數串列是空的,后置版的引數串列原本也應該時空,然而多載規則要求,引數串列必須獨一無二,因此,C++想出一個變通辦法,要求后置版得有一個int引數,int引數從何發生,又到哪里去,編譯器會自動會后置產生一個int引數(其值必為0),用戶不必為此煩惱,

所謂friend,具備了與class member function相同的訪問權限,可以訪問class的private member,只要在某個函式原型(prototype)前加上關鍵字friend,就可以將它宣告為某個class的friend,這份宣告可以出現在class定義的任意位置上,不受private或public的影響,如果你希望將數個多載函式都宣告為某個class的friend,必須明確地為每個函式加上關鍵字friend,

maximal munch編譯規則,此規則要求,每個符號序列(symbol seuqence)總是以“合法符號序列”中最長的那個解釋,因為>>是個合法的運算子序列,因此如果兩個>之間沒有空白,這兩個符號必定會被合在一起看待,同樣道理,如果我們寫下a+++p,它必定會被解釋為“a++ +p”,

練習題答案

練習4.1 建立Stack.h和Stack.suffix,此處的suffix是你的編譯器所能接受的擴展名,或是你的專案所使用的擴展名,撰寫main()函式,練習操作Stack的所有公開介面,并加以編譯執行,程式代碼檔案和main()都必須包含Stack.h:#include “Stack.h”

Stack.h
#include <string>
#include <vector>

using namespace std;

class Stack
{
public:
    bool push(const string&);
    bool pop(string& elem);
    bool peek(string& elem);
    bool empty() const { return _stack.empty(); }
    bool full() const { return _stack.size() == _stack.max_size(); }
    int size() const { return _stack.size(); }
private:
    vector<string> _stack;
};

Stack.cpp
#include "Stack.h"

bool Stack::pop(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    _stack.pop_back();
    return true;
}

bool Stack::peek(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    return true;
}

bool Stack::push(const string& elem)
{
    if (full()) return false;
    _stack.push_back(elem);
    return true;
}

main.cpp
#include "Stack.h"
#include <iostream>

int main()
{
    Stack st;
    string str;
    while (cin >> str && !st.full())
    {
        st.push(str);
    }
    if (st.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    st.peek(str);
    if (st.size() == 1 && str.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    cout << '\n' << "Read in " << st.size() << " strings!\n"
        << "The strings, in reverse order: \n";
    while (st.size())
    {
        if (st.pop(str))
            cout << str << ' ';
    }
    cout << '\n' << "There are now " << st.size()
        << " elements in the stack!\n";
}

練習4.2 擴展Stack功能,以支持find()和count()兩個操作,find()會查看某值是否存在而回傳true或false,count()回傳某字串的出現次數,重新實作練習4.1的main(),讓它呼叫這兩個函式,

Stack_2.h
#include <string>
#include <vector>

using namespace std;

class Stack
{
public:
    bool push(const string&);
    bool pop(string& elem);
    bool peek(string& elem);
    bool empty() const { return _stack.empty(); }
    bool full() const { return _stack.size() == _stack.max_size(); }
    int size() const { return _stack.size(); }
    bool find(const string& elem) const;
    int count(const string& elem) const;
private:
    vector<string> _stack;
};

Stack_2.cpp
#include "Stack_2.h"
#include <algorithm>

bool Stack::pop(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    _stack.pop_back();
    return true;
}

bool Stack::peek(string& elem)
{
    if (empty())    return false;
    elem = _stack.back();
    return true;
}

bool Stack::push(const string& elem)
{
    if (full()) return false;
    _stack.push_back(elem);
    return true;
}

bool Stack::find(const string& elem) const
{
    vector<string>::const_iterator end_it = _stack.end();
    return ::find(_stack.begin(), end_it, elem) != end_it;
}

int Stack::count(const string& elem) const
{
    return ::count(_stack.begin(), _stack.end(), elem);
}

main.cpp
#include "Stack_2.h"
#include <iostream>

int main()
{
    Stack st;
    string str;
    while (cin >> str && !st.full())
    {
        st.push(str);
    }
    if (st.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    st.peek(str);
    if (st.size() == 1 && str.empty())
    {
        cout << '\n' << "Oops: no strings were read -- bailing out\n ";
        return 0;
    }
    cout << '\n' << "Read in " << st.size() << " strings!\n";
    cin.clear();    //清除end-of-file的設定
    cout << "what word to search for? ";
    cin >> str;
    bool found = st.find(str);
    int count = found ? st.count(str) : 0;
    cout << str << (found ? " is " : "isn't ") << "in the stack.";
    if (found)
        cout << "It occurs " << count << " times\n";
}

練習4.3 考慮以下所定義的全域(global)資料:

string program_name;
string vector_stamp;
int version_number;
int tests_run;
int tests_passed;

撰寫一個用以包裝這些資料的類,

#include <string>

using std::string;

class globalWrapper
{
public:
    static int tests_passed() { return _tests_passed; }
    static int tests_run() { return _tests_run; }
    static int version_number() { return _version_number; }
    static string version_stamp() { return _version_stamp; }
    static string program_name() { return _program_name; }

    static void tests_passed(int nval) { _tests_passed = nval; }
    static void tests_run(int nval) { _tests_run = nval; }

    static void version_stamp(const string&nstamp)
    {
        _version_stamp = nstamp;
    }

    static void version_number(int nval)
    {
        _version_number = nval;
    }

    static void program_name(const string& npn)
    {
        _program_name = npn;
    }

private:
    static string _program_name;
    static string _version_stamp;
    static int _version_number;
    static int _tests_run;
    static int _tests_passed;
};

string globalWrapper::_program_name;
string globalWrapper::_version_stamp;
int globalWrapper::_version_number;
int globalWrapper::_tests_run;
int globalWrapper::_tests_passed;

練習4.4 一份“用戶概況記錄(user profile)”內含以下資料:登陸記錄、實際姓名、登入次數、猜過次數、猜對次數、等級——包括初級、中級、高級、專家、以及猜對百分比(可實時計算獲得,或將其值儲存起來備用),請寫出一個名為UserProfile的class,提供以下操作:輸入、輸出、相等測驗、不等測驗,其constructor必須能夠處理默認的用戶等級、默認的登陸名稱(“guest”),對于同樣的名為guest的多個用戶,你如何保證每個guest有他自己獨有的登陸會話(login session),不會和其他人混淆?

UserProfile.h
#include <iostream>
#include <string>
#include <map>

using namespace std;

class UserProfile
{
public:
    enum uLevel
    {
        Beginner, Intermediate, Advanced, Guru
    };
    UserProfile(string login, uLevel = Beginner);
    UserProfile();

    //default memberwise initilaization和default memberwise copy已足夠所需,
    //不必另行設計copy constructor或copy assignment operator,
    //也不需要destructor

    bool operator==(const UserProfile&);
    bool operator!=(const UserProfile& rhs);

    //以下函式用來讀取資料
    string login()const { return _login; }
    string user_name() const { return _user_name; }
    int login_count() const { return _times_logged; }
    int guess_count() const { return _guesses; }
    int guess_correct() const{ return _correct_guesses; }
    double guess_average() const;
    string level() const;

    //以下函式用來寫入資料
    void reset_login(const string& val) { _login = val; }
    void user_name(const string& val) { _user_name = val; }

    void reset_level(const string&);
    void reset_level(uLevel newlevel) { _user_level = newlevel; }

    void reset_login_count(int val) { _times_logged = val;}
    void reset_guess_count(int val) { _guesses = val; }
    void reset_guess_correct(int val) { _correct_guesses = val; }

    void bump_login_count(int cnt = 1) { _times_logged += cnt; }
    void bump_guess_count(int cnt=1) { _guesses = cnt; }
    void bump_guess_correct(int cnt = 1) { _correct_guesses = cnt; }

private:
    string _login;
    string _user_name;
    int _times_logged;
    int _guesses;
    int _correct_guesses;
    uLevel _user_level;

    static map<string, uLevel> _level_map;
    static void init_level_map();
    static string guest_login();
};

inline double UserProfile::guess_average() const
{
    return _guesses
        ? double(_correct_guesses) / double(_guesses) * 100
        : 0.0;
}

inline UserProfile::UserProfile(string login,uLevel level)
    :_login(login),_user_level(level),
    _times_logged(1), _guesses(0), _correct_guesses(0) {}

#include <cstdlib>

inline UserProfile::UserProfile()
    : _login("guest"), _user_level(Beginner),
    _times_logged(1), _guesses(0), _correct_guesses(0)
{
    static int id = 0;
    char buffer[16];

    //_itoa()是C標準庫所提供的的函式,會將整數轉換為對應的ASCII字串形式
    _itoa(id++, buffer, 10);

    //針對guest,加入一個獨一無二的會話識別符號(session id)
    _login += buffer;
}

inline bool UserProfile::
operator==(const UserProfile& rhs)
{
    if (_login == rhs._login && _user_name == rhs._user_name)
        return true;
    return false;
}

inline bool UserProfile::operator!=(const UserProfile& rhs)
{
    return !(*this == rhs);
}

inline string UserProfile::level() const
{
    static string _level_table[] = {
        "Beginner","Intermediate","Advanced","Guru"
    };
    return _level_table[_user_level];
}

//以下難度頗高,不過恰可作為示范
map<string, UserProfile::uLevel> UserProfile::_level_map;

void UserProfile::init_level_map()
{
    _level_map["Beginner"] = Beginner;
    _level_map["Intermediate"] = Intermediate;
    _level_map["Advanced"] = Advanced;
    _level_map["Guru"] = Guru;
}

inline void UserProfile::reset_level(const string& level)
{
    map<string, uLevel>::iterator it;
    if (_level_map.empty())
        init_level_map();

    //確保level的確代表一個可識別的用戶等級
    _user_level = ((it = _level_map.find(level)) != _level_map.end()) ? it->second : Beginner;
}

main.cpp
#include "UserProfile.h"
#include <iostream>

ostream& operator <<(ostream& os, const UserProfile& rhs)
{
    //輸出格式,如:stanl Beginner 12 100 10 10%
    os << rhs.login() << ' '
        << rhs.level() << ' '
        << rhs.login_count() << ' '
        << rhs.guess_count() << ' '
        << rhs.guess_correct() << ' '
        << rhs.guess_average() << endl;
    return os;
}

istream& operator >>(istream& is, UserProfile& rhs)
{
    //是的,以下假設所有輸入都有效,不做錯誤檢驗
    string login, level;
    is >> login >> level;
    int lcount, gcount, gcorrect;
    is >> lcount >> gcount >> gcorrect;
    rhs.reset_login(login);
    rhs.reset_level(level);
    rhs.reset_login_count(lcount+10);
    rhs.reset_guess_count(gcount);
    rhs.reset_guess_correct(gcorrect);
    return is;
}

int main()
{
    UserProfile anon;
    cout << anon;    //測驗output運算子
    UserProfile anon_too;    //看看我們是否取得一份獨一無二的識別符號
    cout << anon_too;

    UserProfile anna("Annal", UserProfile::Guru);
    cout << anna;
    anna.bump_guess_count(27);
    anna.bump_guess_correct(25);
    anna.bump_login_count();
    cout << anna;
    cin >> anon;    //測驗input運算子
    cout << anon;
    return 0;
}

練習4.5 請實作一個4*4的Martix class,至少提供以下介面:矩陣加法、矩陣乘法、列印函式print()、復合運算子+=,以及一組支持下標操作(subscripting)的function call運算子,像下面這樣:

float& operator()(int row, int cloumn);
float operator()(int row, int cloumn) const;

請提供一個default constructor,可選擇地接受16個資料值,再提供一個constructor,可接受一個擁有16個元素的陣列,你不需要為此class 提供copy constructor,copy assignment operator、destructor,第六章重新實作Matrix class時才會需要這幾個函式,用以支持任意行列的矩陣,

Matrix.h
#include <iostream>

using namespace std;

typedef float elemType;    //方便我們轉為template

class Matrix
{
    //friend宣告不受訪問權限的影響
    //我喜歡把它們放在class一開始處
    friend Matrix operator+(const Matrix&, const Matrix&);
    friend Matrix operator*(const Matrix&, const Matrix&);

public:
    Matrix(const elemType*);
    Matrix(const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.,
        const elemType = 0., const elemType = 0., const elemType = 0., const elemType = 0.);

    //不需要為Matrix提供copy constructor、destructor、
    //copy assignment operator

    //簡化“轉換至通用型矩陣(general Matrix)”的程序
    int rows() const { return 4; }
    int cols() const { return 4; }

    ostream& print(ostream&) const;
    void operator+=(const Matrix&);
    elemType operator()(int row, int column) const
    {
        return _Matrix[row][column];
    }
    elemType& operator()(int row, int column) 
    {
        return _Matrix[row][column];
    }

private:
    elemType _Matrix[4][4];
};

inline ostream& operator<<(ostream& os, const Matrix& m)
{
    return m.print(os);
}

Matrix operator+(const Matrix& m1, const  Matrix& m2)
{
    Matrix result(m1);
    result += m2;
    return result;
}

Matrix operator*(const Matrix& m1, const Matrix& m2)
{
    Matrix result;
    for (int ix = 0;ix < m1.rows();ix++)
    {
        for (int jx = 0;jx < m1.cols();jx++)
        {
            result(ix, jx) = 0;
            for (int kx = 0;kx < m1.cols();kx++)
            {
                result(ix, jx) += m1(ix, kx) * m2(kx, jx);
            }
        }
    }
    return result;
}

void Matrix::operator+=(const Matrix& m)
{
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx)
        {
            _Matrix[ix][jx] += m._Matrix[ix][jx];
        }
    }
}

ostream& Matrix::print(ostream& os) const
{
    int cnt = 0;
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx, ++cnt)
        {
            if (cnt && !(cnt % 8)) 
                os << endl;
            os << _Matrix[ix][jx] << ' ';
        }
    }
    os << endl;
    return os;
}

Matrix::Matrix(const elemType* array)
{
    int array_index = 0;
    for (int ix = 0;ix < 4;++ix)
    {
        for (int jx = 0;jx < 4;++jx)
            _Matrix[ix][jx] = array[array_index++];
    }
}

Matrix::Matrix(elemType a11, elemType a12, elemType a13, elemType a14,
    elemType a21, elemType a22, elemType a23, elemType a24,
    elemType a31, elemType a32, elemType a33, elemType a34,
    elemType a41, elemType a42, elemType a43, elemType a44)
{
    _Matrix[0][0] = a11;_Matrix[0][1] = a12;
    _Matrix[0][2] = a13;_Matrix[0][3] = a14;
    _Matrix[1][0] = a21;_Matrix[1][1] = a22;
    _Matrix[1][2] = a23;_Matrix[1][3] = a24;
    _Matrix[2][0] = a31;_Matrix[2][1] = a31;
    _Matrix[2][2] = a33;_Matrix[2][3] = a34;
    _Matrix[3][0] = a41;_Matrix[3][1] = a42;
    _Matrix[3][2] = a43;_Matrix[3][3] = a44;
}

main.cpp
#include "Matrix.h"

int main()
{
    Matrix m;
    cout << m << endl;
    elemType ar[16] = {
        1.,0.,0.,0.,0.,1.,0.,0.,
        0.,0.,1.,0.,0.,0.,0.,1.,
    };
    Matrix identity(ar);
    cout << identity << endl;

    Matrix m2(identity);
    m = identity;
    cout << m2 << endl;
    cout << m << endl;

    elemType ar2[16] = {
        1.3,0.4,2.6,8.2,6.2,1.7,1.3,8.3,
        4.2,7.4,2.7,1.9,6.3,8.1,5.6,6.6
    };
    Matrix m3(ar2);
    cout << m3 << endl;
    Matrix m4 = m3 * identity;
    cout << m4 << endl;
    Matrix m5 = m3 + m4;
    cout << m5 << endl;
    m3 += m4;
    cout << m3 << endl;
    return 0;
}
end,

“沒有一個冬天不可逾越,沒有一個春天不會來臨,”

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67303.html

標籤:C++

上一篇:【C++初學者自學筆記一】(把自己剛學到的東西做一下總結,如果有哪些地方不足請留言指正)

下一篇:AtCoder agc007_d - Shik and Game

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more