peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Lớp lưu trữ trong C++ - Freetuts
STARTING
CONTROL STATEMENT
FUNCTION
ARRAY & POINTER
OOP
STL
ITERATORS
OTHER FEATURES
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lớp lưu trữ trong C++

Các bạn thân mến! Trong bài học hôm nay chúng ta sẽ cùng tìm hiểu về lớp lưu trữ (storage class) trong C++. Vậy lớp lưu trữ trong C++ là gì? Chúng ta cùng tìm hiểu trong nội dung sau đây nhé!

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Lớp lưu trữ trong C++

Một lớp lưu trữ (storage class) trong C++ xác định phạm vi và thời gian tồn tại của các biến hoặc các hàm trong một hương trình. 

Trong C++ hổ trợ cho chúng ta các loại lớp lưu trữ như sau:

  • auto
  • register
  • static
  • extern
  • mutable

Chúng ta cùng tìm hiểu từng loại lớp lưu trữ một nhé.

Bài viết này được đăng tại [free tuts .net]

2. Lớp lưu trữ Auto

Auto storage class là lớp lưu trữ mặc định cho tất cả các biến cục bộ. Nếu bạn nào chưa biết về biến cục bộ thì xem lại bài phạm vi biến trong C++ nhé.

Cú pháp

Cú pháp của biến auto trong C++ như sau:

Cú pháp
datatype name_variable [= value];
or
auto name_variable [= value];

Ví dụ

Chúng ta cùng xem ví dụ đơn giản về biến auto trong C++ như sau:

Ví dụ 1: Biến auto

Ví dụ
#include <iostream>

using namespace std;

void In() {
    int a = 5;
    auto b = 15;
    auto c = 1.5;
    cout << "Bien auto a = " << a << endl;
    cout << "Bien auto b = " << b << endl;
    cout << "Bien auto c = " << c << endl;
}

int main()
{
    In();
    return 0;
}

Kết quả sau khi thực thi đoạn code trên:

auto ex JPG

Ví dụ trên định nghĩa các biến có cùng một lớp lưu trữ đó là lớp lưu trữ auto, như vậy lớp lưu trữ auto chỉ có thể được sử dụng trong các hàm, tức là các biến cục bộ.

Vì phạm vi sử dụng của biến ở lớp lưu trữ auto chỉ có phạm vi bên trong hàm.

Ví dụ 2:  Hàm auto

Chúng ta cùng xem tiếp một ví dụ về hàm auto trong C++ như sau:

Ví dụ
#include <iostream>

using namespace std;

auto Cong(int x, int y) {
    cout << x << " + " << y << " = " << x + y << endl;
}
auto Tru(double x, double y) {
    cout << x << " - " << y << " = " << x - y << endl;
}
auto In() {
    cout << "Freetuts xin chao moi nguoi" << endl;
}

int main()
{
    In();
    Cong(2, 3);
    Tru(5, 3);
    return 0;
}

Và kết quả sau khi thực thi chương trình trên:

auto function ex JPG

3. Lớp lưu trữ Register

Biến thanh ghi được cấp phát bộ nhớ trong một thanh ghi thay vì RAM. Vì vậy biến thanh ghi chỉ có kích thước tối đa bằng kích thước thanh ghi và không thể áp dụng toán tử & cho biến thanh ghi vì nó không có vị trí bộ nhớ.

Cú pháp

Cú pháp của biến thanh ghi trong C++ như sau:

Cú pháp
register datatype name_variable; 

Ví dụ

Chúng ta cùng xem một ví dụ đơn giản về biến thanh ghi trong C++ như sau:

Ví dụ
#include <iostream>

using namespace std;

int main()
{   
    register int i = 100;
    cout << "Gia tri bien i = " << i*5 << endl;
    return 0;
}

Và kết quả sau khi thực thi chương trình trên:

register ex JPG

Lưu ý: Biến ở lớp lưu trữ thanh ghi sẽ được truy cập nhanh hơn các biến ở lớp lưu trữ khác. Tuy nhiên không khuyến khích các bạn lạm dụng việc khai báo biến ở lớp lưu trữ thanh ghi, chỉ nên sử dụng khi cần có truy cập nhanh của một số biến đặc biệt nào đó thôi.

4. Lớp lưu trữ Static

Biến static trong C++ chỉ khởi tạo duy nhất một lần và tồn tại đến khi kết thúc chương trình.

Cú pháp

Cú pháp của biến static trong C++ như sau:

Cú pháp
static type variable;

Ví dụ

Chúng ta cùng xem một ví dụ đơn giản về biến static trong C++ như sau:

Ví dụ
#include <iostream>
using namespace std;

void Dem() {
   static int i = 5; 
   i++;
   cout << "   Bien i = " << i << endl ;
}

void DemNoStatic() {
   int i = 5; 
   i++;
   cout << "   Bien i = " << i << endl ;
}

main() {
   int count = 5;
   int count2 = 5;
   cout << "Vi du bien static " << endl;
   while(count--) {
      Dem();
   }
   cout << "Vi du bien khong static " << endl;
   while(count2--) {
      DemNoStatic();
   }
   return 0;
}

Và kết quả sau khi thực thi đoạn code trên như sau:

static ex JPG

5. Lớp lưu trữ External

Biến extern trong C++ được hiển thị cho tất cả các chương trình. Nó được sử dụng nếu hai hoặc nhiều file chia sẻ cùng một biến hoặc function.

Cú pháp

Cú pháp của biến extern trong C++ như sau:

Cú pháp
extern datatype name_variable;  

Ví dụ

Chúng ta cùng xem ví dụ về sử dụng biến extern trong C++ như sau:

File1:

Ví dụ
#include <iostream>
int tong;
extern void Tong();
 
main() {
   tong= 10;
   Tong();
}

File2:

Ví dụ
#include <iostream>
using namespace std;
extern int tong;

void Tong() {
   cout << "Tong la:  " <<  tong << endl;
}

Và kết quả sau khi biên dịch và thực thi 2 file trên:

extern ex JPG

6. Lớp lưu trữ Mutable

mutable storage class trong C++ rất khó để giải thích trong bài này, vì nó liên quan đến lớp đối tượng, mình sẽ nói trong các bài sau này.

Tuy nhiên, các bạn cứ tham khảo, nếu không thì cũng không sao nhé, có thể quay lại đọc khi đã hiểu lớp đối tượng trong C++ là gì.

mutable được áp dụng cho lớp đối tượng. Nó cho phép một thành viên của một đối tượng ghi đè const thành viên

Cú pháp

Cú pháp của biến mutable trong C++ như sau:

Cú pháp
mutable datatype name_variable;

Ví dụ

Chúng ta cùng lấy ví dụ đơn giản về mutable trong C++ như sau:

Ví dụ
#include<iostream>
using namespace std;

class ExMutable
{
    mutable int a;
    int b;
    public:
        ExMutable(int x,int y)
        {
            a = x;
            b = y;
        }
        void Change() const
        {
            a = a + 1;
        }
        void Print() const
        {
            cout << "  Gia tri a = " << a << endl;
            cout << "  Gia tri b = " << b << endl;
        }
};

int main()
{
    const ExMutable x(6,5);
    cout << "Gia tri truoc khi thay doi " << endl;
    x.Print();
    x.Change();
    cout << "Gia tri sau khi thay doi " << endl;
    x.Print();
    return 0;
}

Và kết quả sau khi thực thi chương trình trên:

mutable ex JPG

Ví dụ trên chúng ta khai báo một lớp ExMutable, có 2 dữ liệu thành viên đó là a và b, và chúng ta khai báo mutable cho biến a. Đối tượng const x của lớp ExMutable được tạo ra và nó được khởi tạo giá trị như trong hàm xây dựng ExMutable(x,y).

Lúc này a có giá trị là 6, b có giá trị là 5. Vì b là dữ liệu thành viên bình thường, nên không có thể thay đổi giá trị của b khi khỏi tạo giá trị, tuy nhiên thành viên là là mutable nên có thể thay đổi giá trị khi gọi phương thức Change(). Phương thức Print() đế in giá trị của a và b ra màn hình.

7. Kết luận

Như vậy là chúng ta đã tìm hiểu xong các loại lớp lưu trữ trong C++. Mỗi loại lớp lưu trữ có cách sử dụng khác nhau, tùy theo yêu cầu của bài toán mà các bạn lựa chọn lớp lưu trữ nào cho phù hợp nhé.

Mình sẽ kết thúc bài học này ở đây, cám ơn các bạn đã đọc bài viết.  

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

Top