//一維陣列動態分配,陣列長度為 m
int *array = new int [m];
//釋放記憶體
delete [] array;
//二維陣列
int **array;
// 假定陣列第一維長度為 m, 第二維長度為 n
// 動態分配空間
array = new int *[m];
for(int i = 0; i < m; ++i)
{
array[i] = new int [n];
}
//釋放
for(int i = 0; i < m; ++i)
{
delete [] array[i];
}
delete [] array;
#include <iostream>
#include <malloc.h>
class TEST
{
private:
int num1;
int num2;
public:
TEST()
{
num1 = 10;
num2 = 20;
}
void Print()
{
std::cout << num1 << " " << num2 << std::endl;
}
};
int main(void)
{
// 用malloc()函式在堆區分配一塊記憶體空間,然後用強制型別轉換將該塊記憶體空間
// 解釋為是一個TEST類物件,這不會呼叫TEST的預設建構函式
TEST * pObj1 = (TEST *)malloc(sizeof(TEST));
pObj1->Print();
// 用new在堆區建立一個TEST類的物件,這會呼叫TEST類的預設建構函式
TEST * pObj2 = new TEST;
pObj2->Print();
return 0;
}
/*
執行結果:
-----------------------------
-842150451 -842150451 |
10 20 |
請按任意鍵繼續. . . |
-----------------------------
我們可以看到pObj1所指的物件中,欄位num1與num2都是垃圾值
而pObj2所指的物件中,欄位num1與num2顯然是經過了構造後的值
*/
int i = 10;
float f = static_cast<float>(i); // 靜態將int型別轉換為float型別
class Base {};
class Derived : public Base {};
Base* ptr_base = new Derived;
// 將基類指標轉換為派生類指標
Derived* ptr_derived = dynamic_cast<Derived*>(ptr_base);
const int i = 10;
int& r = const_cast<int&>(i); // 常量轉換,將const int轉換為int
int i = 10;
float f = reinterpret_cast<float&>(i); // 重新解釋將int型別轉換為float型別
typedef unsigned int UINT;
void func()
{
UINT value = "abc"; // error C2440: 'initializing' : cannot convert from 'const char [4]' to 'UINT'
cout << value << endl;
}
void func1()
{
#define HW "HelloWorld";
}
void func2()
{
string str = HW;
cout << str << endl;
}
void func1()
{
typedef unsigned int UINT;
}
void func2()
{
UINT uValue = 5; //error C2065: 'UINT' : undeclared identifier
}
class A
{
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};
class B
{
UINT valueB;
//error C2146: syntax error : missing ';' before identifier 'valueB'
//error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
};
class A
{
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};
void func3()
{
A::UINT i = 1;
// error C2248: 'A::UINT' : cannot access private typedef declared in class 'A'
}
class A
{
public:
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};
void func3()
{
A::UINT i = 1;
cout << i << endl;
}
typedef int * pint;
#define PINT int *
int i1 = 1, i2 = 2;
const pint p1 = &i1; //p不可更改,p指向的內容可以更改,相當於 int * const p;
const PINT p2 = &i2; //p可以更改,p指向的內容不能更改,相當於 const int *p;或 int const *p;
pint s1, s2; //s1和s2都是int型指標
PINT s3, s4; //相當於int * s3,s4;只有一個是指標。
void TestPointer()
{
cout << "p1:" << p1 << " *p1:" << *p1 << endl;
//p1 = &i2; //error C3892: 'p1' : you cannot assign to a variable that is const
*p1 = 5;
cout << "p1:" << p1 << " *p1:" << *p1 << endl;
cout << "p2:" << p2 << " *p2:" << *p2 << endl;
//*p2 = 10; //error C3892: 'p2' : you cannot assign to a variable that is const
p2 = &i1;
cout << "p2:" << p2 << " *p2:" << *p2 << endl;
}
p1:00EFD094 *p1:1
p1:00EFD094 *p1:5
p2:00EFD098 *p2:2
p2:00EFD094 *p2:5
typedef long double FALSE;
typedef double FALSE;
typedef float FALSE;
typedef enum BAYER_PATTERN{
BAYER_RG = 0,
BAYER_BG,
BAYER_GR,
BAYER_GB
}BAYER_PATTERN;
BAYER_PATTERN color = BAYER_RG;
// 函式宣告
int func();
int main()
{
// 函式呼叫
int i = func();
}
// 函式定義
int func()
{
return 0;
}
int a=1;
double b=2.5;
a=b;
cout << a; //輸出為 2,丟失小數部分
int a = 1;
double b = 2.1;
cout << "a + b = " << a + b << endl; //輸出為a + b = 3.1
掃碼關注公眾號,檢視更多精彩內容