主頁 > 後端開發 > 使C語言實作面向物件的三個要素,你掌握了嗎?

使C語言實作面向物件的三個要素,你掌握了嗎?

2020-11-12 05:56:32 後端開發

使C語言實作面向物件的三個要素,你掌握了嗎??

 

編排 | strongerHuang

微信公眾號 | strongerHuang

 

不知道有多少人去了解過語言的發展史,早期C語言的語法功能其實比較簡單,隨著應用需求和場景的變化,C語言的語法功能在不斷升級變化,

 

雖然我們的教材有這么一個結論:C語言是面向程序的語言,C++是面向物件的編程語言,但面向物件的概念是在C語言階段就有了,而且應用到了很多地方,比如某些作業系統內核、通信協議等,

 

面向物件編程,也就是大家說的OOP(Object Oriented Programming)并不是一種特定的語言或者工具,它只是一種設計方法、設計思想,它表現出來的三個最基本的特性就是封裝、繼承與多型

 

為什么要用C語言實作面向物件

 

閱讀文本之前肯定有讀者會問這樣的問題:我們有C++面向物件的語言,為什么還要用C語言實作面向物件呢?

 

C語言這種非面向物件的語言,同樣也可以使用面向物件的思路來撰寫程式的,只是用面向物件的C++語言來實作面向物件編程會更簡單一些,但是C語言的高效性是其他面向物件編程語言無法比擬的,

 

當然使用C語言來實作面向物件的開發相對不容易理解,這就是為什么大多數人學過C語言卻看不懂Linux內核原始碼,

 

所以這個問題其實很好理解,只要有一定C語言編程經驗的讀者都應該能明白:面向程序的C語言和面向物件的C++語言相比,代碼運行效率、代碼量都有很大差異,在性能不是很好、資源不是很多的MCU中使用C語言面向物件編程就顯得尤為重要,

 

具備條件

 

要想使用C語言實作面向物件,首先需要具備一些基礎知識,比如:(C語言中的)結構體、函式、指標,以及函式指標等,(C++中的)基類、派生、多型、繼承等,

 

首先,不僅僅是了解這些基礎知識,而是有一定的編程經驗,因為上面說了“面向物件是一種設計方法、設計思想”,如果只是停留在字面意思的理解,沒有這種設計思想肯定不行,

 

因此,不建議初學者使用C語言實作面向物件,特別是在真正專案中,建議把基本功練好,再使用,

 

利用C語言實作面向物件的方法很多,下面就來描述最基本的封裝、繼承和多型,

 

C/C++的學習裙【七一二 二八四 七零五 】,無論你是小白還是進階者,是想轉行還是想入行都可以來了解一起進步一起學習!裙內有開發工具,很多干貨和技術資料分享!

 

封裝

 

封裝就是把資料和函式打包到一個類里面,其實大部分C語言編程者都已經接觸過了,

 

C 標準庫中的 fopen(), fclose(), fread(), fwrite()等函式的操作物件就是 FILE,資料內容就是 FILE,資料的讀寫操作就是 fread()、fwrite(),fopen() 類比于建構式,fclose() 就是解構式,

 

這個看起來似乎很好理解,那下面我們實作一下基本的封裝特性,

/n/n// Shape 的屬性/ntypedef struct {/n    int16_t x; /n    int16_t y; /n} Shape;/n/n// Shape 的操作函式,介面函式/nvoid Shape_ctor(Shape * const me, int16_t x, int16_t y);/nvoid Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);/nint16_t Shape_getX(Shape const * const me);/nint16_t Shape_getY(Shape const * const me);/n/n#endif /* SHAPE_H */","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

// Shape 的屬性
typedef struct {
    int16_t x; 
    int16_t y; 
} Shape;

// Shape 的操作函式,介面函式
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape const * const me);
int16_t Shape_getY(Shape const * const me);

#endif /* SHAPE_H */

 

這是 Shape 類的宣告,非常簡單,很好理解,一般會把宣告放到頭檔案里面 “Shape.h”,來看下 Shape 類相關的定義,當然是在 “Shape.c” 里面,

x = x;/n    me->y = y;/n}/n/nvoid Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) /n{/n    me->x += dx;/n    me->y += dy;/n}/n/n// 獲取屬性值函式/nint16_t Shape_getX(Shape const * const me) /n{/n    return me->x;/n}/nint16_t Shape_getY(Shape const * const me) /n{/n    return me->y;/n}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "shape.h"

// 建構式
void Shape_ctor(Shape * const me, int16_t x, int16_t y)
{
    me->x = x;
    me->y = y;
}

void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) 
{
    me->x += dx;
    me->y += dy;
}

// 獲取屬性值函式
int16_t Shape_getX(Shape const * const me) 
{
    return me->x;
}
int16_t Shape_getY(Shape const * const me) 
{
    return me->y;
}

 

再看下 main.c

  /* for printf() */n/nint main() /n{/n    Shape s1, s2; /* multiple instances of Shape */n/n    Shape_ctor(&s1, 0, 1);/n    Shape_ctor(&s2, -1, 2);/n/n    printf(/"Shape s1(x=%d,y=%d)//n/", Shape_getX(&s1), Shape_getY(&s1));/n    printf(/"Shape s2(x=%d,y=%d)//n/", Shape_getX(&s2), Shape_getY(&s2));/n/n    Shape_moveBy(&s1, 2, -4);/n    Shape_moveBy(&s2, 1, -2);/n/n    printf(/"Shape s1(x=%d,y=%d)//n/", Shape_getX(&s1), Shape_getY(&s1));/n    printf(/"Shape s2(x=%d,y=%d)//n/", Shape_getX(&s2), Shape_getY(&s2));/n/n    return 0;/n}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "shape.h"  /* Shape class interface */
#include <stdio.h>  /* for printf() */

int main() 
{
    Shape s1, s2; /* multiple instances of Shape */

    Shape_ctor(&s1, 0, 1);
    Shape_ctor(&s2, -1, 2);

    printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
    printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));

    Shape_moveBy(&s1, 2, -4);
    Shape_moveBy(&s2, 1, -2);

    printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
    printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));

    return 0;
}

 

編譯之后,看看執行結果:

Shape s1(x=0,y=1)
Shape s2(x=-1,y=2)
Shape s1(x=2,y=-3)
Shape s2(x=0,y=0)

 

整個例子,非常簡單,非常好理解,以后寫代碼時候,要多去想想標準庫的檔案IO操作,這樣也有意識地去培養面向物件編程的思維,

使C語言實作面向物件的三個要素,你掌握了嗎??

 

繼承

 

繼承就是基于現有的一個類去定義一個新類,這樣有助于重用代碼,更好的組織代碼,在 C 語言里面,去實作單繼承也非常簡單,只要把基類放到繼承類的第一個資料成員的位置就行了,

 

例如,我們現在要創建一個 Rectangle 類,我們只要繼承 Shape 類已經存在的屬性和操作,再添加不同于 Shape 的屬性和操作到 Rectangle 中,

 

下面是 Rectangle 的宣告與定義:

#ifndef RECT_H
#define RECT_H

#include "shape.h" // 基類介面

// 矩形的屬性
typedef struct {
    Shape super; // 繼承 Shape

    // 自己的屬性
    uint16_t width;
    uint16_t height;
} Rectangle;

// 建構式
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height);

#endif /* RECT_H */
super, x, y);/n/n    /* next, you initialize the attributes added by this subclass... */n    me->width = width;/n    me->height = height;/n}/n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "rect.h"

// 建構式
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height)
{
    /* first call superclass’ ctor */
    Shape_ctor(&me->super, x, y);

    /* next, you initialize the attributes added by this subclass... */
    me->width = width;
    me->height = height;
}

 

我們來看一下 Rectangle 的繼承關系和記憶體布局:

使C語言實作面向物件的三個要素,你掌握了嗎??

 

因為有這樣的記憶體布局,所以你可以很安全的傳一個指向 Rectangle 物件的指標到一個期望傳入 Shape 物件的指標的函式中,就是一個函式的引數是 “Shape *”,你可以傳入 “Rectangle *”,并且這是非常安全的,這樣的話,基類的所有屬性和方法都可以被繼承類繼承!

 /n/nint main() /n{/n    Rectangle r1, r2;/n/n    // 實體化物件/n    Rectangle_ctor(&r1, 0, 2, 10, 15);/n    Rectangle_ctor(&r2, -1, 3, 5, 8);/n/n    printf(/"Rect r1(x=%d,y=%d,width=%d,height=%d)//n/",/n           Shape_getX(&r1.super), Shape_getY(&r1.super),/n           r1.width, r1.height);/n    printf(/"Rect r2(x=%d,y=%d,width=%d,height=%d)//n/",/n           Shape_getX(&r2.super), Shape_getY(&r2.super),/n           r2.width, r2.height);/n/n    // 注意,這里有兩種方式,一是強轉型別,二是直接使用成員地址/n    Shape_moveBy((Shape *)&r1, -2, 3);/n    Shape_moveBy(&r2.super, 2, -1);/n/n    printf(/"Rect r1(x=%d,y=%d,width=%d,height=%d)//n/",/n           Shape_getX(&r1.super), Shape_getY(&r1.super),/n           r1.width, r1.height);/n    printf(/"Rect r2(x=%d,y=%d,width=%d,height=%d)//n/",/n           Shape_getX(&r2.super), Shape_getY(&r2.super),/n           r2.width, r2.height);/n/n    return 0;/n}/n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "rect.h"  
#include <stdio.h> 

int main() 
{
    Rectangle r1, r2;

    // 實體化物件
    Rectangle_ctor(&r1, 0, 2, 10, 15);
    Rectangle_ctor(&r2, -1, 3, 5, 8);

    printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r1.super), Shape_getY(&r1.super),
           r1.width, r1.height);
    printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r2.super), Shape_getY(&r2.super),
           r2.width, r2.height);

    // 注意,這里有兩種方式,一是強轉型別,二是直接使用成員地址
    Shape_moveBy((Shape *)&r1, -2, 3);
    Shape_moveBy(&r2.super, 2, -1);

    printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r1.super), Shape_getY(&r1.super),
           r1.width, r1.height);
    printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(&r2.super), Shape_getY(&r2.super),
           r2.width, r2.height);

    return 0;
}

 

輸出結果:

Rect r1(x=0,y=2,width=10,height=15)
Rect r2(x=-1,y=3,width=5,height=8)
Rect r1(x=-2,y=5,width=10,height=15)
Rect r2(x=1,y=2,width=5,height=8)

 

多型

 

C++ 語言實作多型就是使用虛函式,在 C 語言里面,也可以實作多型,

 

現在,我們又要增加一個圓形,并且在 Shape 要擴展功能,我們要增加 area() 和 draw() 函式,但是 Shape 相當于抽象類,不知道怎么去計算自己的面積,更不知道怎么去畫出來自己,而且,矩形和圓形的面積計算方式和幾何影像也是不一樣的,

 

下面讓我們重新宣告一下 Shape 類:

/n/nstruct ShapeVtbl;/n// Shape 的屬性/ntypedef struct {/n    struct ShapeVtbl const *vptr;/n    int16_t x; /n    int16_t y; /n} Shape;/n/n// Shape 的虛表/nstruct ShapeVtbl {/n    uint32_t (*area)(Shape const * const me);/n    void (*draw)(Shape const * const me);/n};/n/n// Shape 的操作函式,介面函式/nvoid Shape_ctor(Shape * const me, int16_t x, int16_t y);/nvoid Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);/nint16_t Shape_getX(Shape const * const me);/nint16_t Shape_getY(Shape const * const me);/n/nstatic inline uint32_t Shape_area(Shape const * const me) /n{/n    return (*me->vptr->area)(me);/n}/n/nstatic inline void Shape_draw(Shape const * const me)/n{/n    (*me->vptr->draw)(me);/n}/n/n/nShape const *largestShape(Shape const *shapes[], uint32_t nShapes);/nvoid drawAllShapes(Shape const *shapes[], uint32_t nShapes);/n/n#endif /* SHAPE_H */","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

struct ShapeVtbl;
// Shape 的屬性
typedef struct {
    struct ShapeVtbl const *vptr;
    int16_t x; 
    int16_t y; 
} Shape;

// Shape 的虛表
struct ShapeVtbl {
    uint32_t (*area)(Shape const * const me);
    void (*draw)(Shape const * const me);
};

// Shape 的操作函式,介面函式
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape const * const me);
int16_t Shape_getY(Shape const * const me);

static inline uint32_t Shape_area(Shape const * const me) 
{
    return (*me->vptr->area)(me);
}

static inline void Shape_draw(Shape const * const me)
{
    (*me->vptr->draw)(me);
}


Shape const *largestShape(Shape const *shapes[], uint32_t nShapes);
void drawAllShapes(Shape const *shapes[], uint32_t nShapes);

#endif /* SHAPE_H */

 

看下加上虛函式之后的類關系圖:

使C語言實作面向物件的三個要素,你掌握了嗎??

 

5.1 虛表和虛指標

虛表(Virtual Table)是這個類所有虛函式的函式指標的集合,

 

虛指標(Virtual Pointer)是一個指向虛表的指標,這個虛指標必須存在于每個物件實體中,會被所有子類繼承,

 

在《Inside The C++ Object Model》的第一章內容中,有這些介紹,

 

5.2 在建構式中設定vptr

在每一個物件實體中,vptr 必須被初始化指向其 vtbl,最好的初始化位置就是在類的建構式中,事實上,在建構式中,C++ 編譯器隱式地創建了一個初始化的vptr,在 C 語言里面, 我們必須顯示的初始化vptr,

 

下面就展示一下,在 Shape 的建構式里面,如何去初始化這個 vptr,

vptr->area)(me);}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">static inline uint32_t Shape_area(Shape const * const me) {    return (*me->vptr->area)(me);}

 

5.3 繼承 vtbl 和 多載 vptr

上面已經提到過,基類包含 vptr,子類會自動繼承,但是,vptr 需要被子類的虛表重新賦值,并且,這也必須發生在子類的建構式中,下面是 Rectangle 的建構式,

 /n/n// Rectangle 虛函式/nstatic uint32_t Rectangle_area_(Shape const * const me);/nstatic void Rectangle_draw_(Shape const * const me);/n/n// 建構式/nvoid Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,/n                    uint16_t width, uint16_t height)/n{/n    static struct ShapeVtbl const vtbl = /n    {/n        &Rectangle_area_,/n        &Rectangle_draw_/n    };/n    Shape_ctor(&me->super, x, y); // 呼叫基類的建構式/n    me->super.vptr = &vtbl;           // 多載 vptr/n    me->width = width;/n    me->height = height;/n}/n/n// Rectangle's 虛函式實作\nstatic uint32_t Rectangle_area_(Shape const * const me) \n{\n    Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉換\n    return (uint32_t)me_-&gt;width * (uint32_t)me_-&gt;height;\n}\n\nstatic void Rectangle_draw_(Shape const * const me) \n{\n    Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉換\n    printf(\&quot;Rectangle_draw_(x=%d,y=%d,width=%d,height=%d)\\n\&quot;,\n           Shape_getX(me), Shape_getY(me), me_-&gt;width, me_-&gt;height);\n}&quot;,&quot;classes&quot;:[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "rect.h"  
#include <stdio.h> 

// Rectangle 虛函式
static uint32_t Rectangle_area_(Shape const * const me);
static void Rectangle_draw_(Shape const * const me);

// 建構式
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
                    uint16_t width, uint16_t height)
{
    static struct ShapeVtbl const vtbl = 
    {
        &Rectangle_area_,
        &Rectangle_draw_
    };
    Shape_ctor(&me->super, x, y); // 呼叫基類的建構式
    me->super.vptr = &vtbl;           // 多載 vptr
    me->width = width;
    me->height = height;
}

// Rectangle's 虛函式實作
static uint32_t Rectangle_area_(Shape const * const me) 
{
    Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉換
    return (uint32_t)me_->width * (uint32_t)me_->height;
}

static void Rectangle_draw_(Shape const * const me) 
{
    Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉換
    printf("Rectangle_draw_(x=%d,y=%d,width=%d,height=%d)\n",
           Shape_getX(me), Shape_getY(me), me_->width, me_->height);
}

 

5.4 虛函式呼叫

有了前面虛表(Virtual Tables)和虛指標(Virtual Pointers)的基礎實作,虛擬呼叫(后期系結)就可以用下面代碼實作了,

vptr->area)(me);/n}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">uint32_t Shape_area(Shape const * const me)
{
    return (*me->vptr->area)(me);
}

這個函式可以放到.c檔案里面,但是會帶來一個缺點就是每個虛擬呼叫都有額外的呼叫開銷,為了避免這個缺點,如果編譯器支持行內函式(C99),我們可以把定義放到頭檔案里面,類似下面:

vptr->area)(me);/n}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">static inline uint32_t Shape_area(Shape const * const me) 
{
    return (*me->vptr->area)(me);
}

 

如果是老一點的編譯器(C89),我們可以用宏函式來實作,類似下面這樣:

vptr->area)((me_)))","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#define Shape_area(me_) ((*(me_)->vptr->area)((me_)))

 

看一下例子中的呼叫機制:

使C語言實作面向物件的三個要素,你掌握了嗎??

 

5.5 main.c

 /n/nint main() /n{/n    Rectangle r1, r2; /n    Circle    c1, c2; /n    Shape const *shapes[] = /n    { /n        &c1.super,/n        &r2.super,/n        &c2.super,/n        &r1.super/n    };/n    Shape const *s;/n/n    // 實體化矩形物件/n    Rectangle_ctor(&r1, 0, 2, 10, 15);/n    Rectangle_ctor(&r2, -1, 3, 5, 8);/n/n    // 實體化圓形物件/n    Circle_ctor(&c1, 1, -2, 12);/n    Circle_ctor(&c2, 1, -3, 6);/n/n    s = largestShape(shapes, sizeof(shapes)/sizeof(shapes[0]));/n    printf(/"largetsShape s(x=%d,y=%d)//n/", Shape_getX(s), Shape_getY(s));/n/n    drawAllShapes(shapes, sizeof(shapes)/sizeof(shapes[0]));/n/n    return 0;/n}","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">#include "rect.h"  
#include "circle.h" 
#include <stdio.h> 

int main() 
{
    Rectangle r1, r2; 
    Circle    c1, c2; 
    Shape const *shapes[] = 
    { 
        &c1.super,
        &r2.super,
        &c2.super,
        &r1.super
    };
    Shape const *s;

    // 實體化矩形物件
    Rectangle_ctor(&r1, 0, 2, 10, 15);
    Rectangle_ctor(&r2, -1, 3, 5, 8);

    // 實體化圓形物件
    Circle_ctor(&c1, 1, -2, 12);
    Circle_ctor(&c2, 1, -3, 6);

    s = largestShape(shapes, sizeof(shapes)/sizeof(shapes[0]));
    printf("largetsShape s(x=%d,y=%d)\n", Shape_getX(s), Shape_getY(s));

    drawAllShapes(shapes, sizeof(shapes)/sizeof(shapes[0]));

    return 0;
}

輸出結果:

largetsShape s(x=1,y=-2)
Circle_draw_(x=1,y=-2,rad=12)
Rectangle_draw_(x=-1,y=3,width=5,height=8)
Circle_draw_(x=1,y=-3,rad=6)
Rectangle_draw_(x=0,y=2,width=10,height=15)

 

使C語言實作面向物件的三個要素,你掌握了嗎??

 

總結

 

還是那句話,面向物件編程是一種方法,并不局限于某一種編程語言,用 C 語言實作封裝、單繼承,理解和實作起來比較簡單,多型反而會稍微復雜一點,如果打算廣泛的使用多型,還是推薦轉到 C++ 語言上,畢竟這層復雜性被這個語言給封裝了,你只需要簡單的使用就行了,但并不代表,C 語言實作不了多型這個特性,

 

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

標籤:C++

上一篇:記一次c3p0連接池連接mysql資料庫的報錯

下一篇:kettle運行spoon.bat彈出命令列后一直無反應

標籤雲
其他(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