//一维数组动态分配,数组长度为 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 与 #define 的区别
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
文章来源:https://www.toymoban.com/news/detail-841071.html
扫码关注公众号,查看更多精彩内容文章来源地址https://www.toymoban.com/news/detail-841071.html
到了这里,关于C++学习笔记——003的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!