void *malloc(size_t size); // stdlib.h void free(void *pmem); // stdlib.h void * memcpy ( void * destination, const void * source, size_t num );
2016年10月16日 星期日
C / C++ Memory Op
Last Update: 2016/10/16 20:53+08
2016年9月19日 星期一
2016年9月7日 星期三
Interface & Abstract
Last Update: 2016/09/07 21:42+08
Assume
that with interface you mean a C++ class with only pure virtual methods (i.e. without any code),
instead with abstract class you mean a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable.
Assume
that with interface you mean a C++ class with only pure virtual methods (i.e. without any code),
instead with abstract class you mean a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable.
class MyInterface
{
public:
// Empty virtual destructor for proper cleanup
virtual ~MyInterface() {}
virtual void Method1() = 0;
virtual void Method2() = 0;
};
class MyAbstractClass
{
public:
virtual ~MyAbstractClass();
virtual void Method1();
virtual void Method2();
void Method3();
virtual void Method4() = 0; // make MyAbstractClass not instantiable
};
2015年10月20日 星期二
C++ & C# simple DllImport example
Last Update: 2015/10/20 23:14+08
Type: Note
//todo 再補完測試各類宣告和呼叫方式
Type: Note
Intro
DllImport 使用包含C++宣告方式//todo 再補完測試各類宣告和呼叫方式
2015年1月6日 星期二
C++ - Operator with reference(&)
Last Update: 2015/01/07 10:56+08
Type: Note
這次這篇主要是要探討 operator 與 reference(&) 的關係
之前提到 C++ &(and) 與 const 的關係
其中有一個 用 reference(&) 的方式 作為 function return, 有些人提到這是個 evil 作法
無論如何, 它也是個有用的東西, 只是要注意使用
Type: Note
Intro
最近都在玩 pointer(*) 和 reference(&)這次這篇主要是要探討 operator 與 reference(&) 的關係
之前提到 C++ &(and) 與 const 的關係
其中有一個 用 reference(&) 的方式 作為 function return, 有些人提到這是個 evil 作法
無論如何, 它也是個有用的東西, 只是要注意使用
2014年12月20日 星期六
2014年12月13日 星期六
C++ vector - Pointer or Not
Last Update: 2014/12/13 20:13+08
Type: Note
我覺得是要看個人使用習慣
雖然有人說用 new 出來的, 也就是用 Pointer 比較好
但其實只要瞭解它的運作, 別搞亂就可以了
需要注意的是 vector 在 超出它當前的容納量時, 會重新 宣告/要求 分配memory
Type: Note
Intro
當我們宣告一個 vector 時, 要用 Pointer 還是 不要用 比較好?我覺得是要看個人使用習慣
雖然有人說用 new 出來的, 也就是用 Pointer 比較好
但其實只要瞭解它的運作, 別搞亂就可以了
需要注意的是 vector 在 超出它當前的容納量時, 會重新 宣告/要求 分配memory
2014年12月7日 星期日
C++ member declare has incomplete type
Last Update: 2014/12/08 13:42+08
Type: Note
class 宣告member時
若該member的型態並非指標
則該member的型態必需事先就定義完整
如下例, compile是會失敗的
但他卻還不知道 Personality class 的 size, 所以無從計算
如果改成指標型態 就沒有問題, 因為指標大小是固定的
Java 和 C# 沒有這問題, 因為 class 都是以指標型態儲存
----------------------------------
實例上來看, 我們會像下面這樣宣告 形成不完全的型態
"#define PERSONALITY_H_" 後, 接著引入 project.h
此時的 Personality class 尚未宣告 or 尚未完成宣告
project.h 引入 Human.h
Human class 宣告, 但無法計算size, 因為 Personality class 未完成
Type: Note
class 宣告member時
若該member的型態並非指標
則該member的型態必需事先就定義完整
如下例, compile是會失敗的
class Personality;
class Human{
Personality p;
}
class Personality{
string name;
}
因為它必須先計算 Human class 的 memory 大小但他卻還不知道 Personality class 的 size, 所以無從計算
如果改成指標型態 就沒有問題, 因為指標大小是固定的
class Personality;
class Human{
Personality *p;
}
class Personality{
string name;
}
在使用 include 時要注意這點Java 和 C# 沒有這問題, 因為 class 都是以指標型態儲存
----------------------------------
實例上來看, 我們會像下面這樣宣告 形成不完全的型態
//Human.h
#ifndef HUMAN_H_
#define HUMAN_H_
#include "project.h"
class Human{
Personality p;
}
#endif
//Personality.h
#ifndef PERSONALITY_H_
#define PERSONALITY_H_
#include "project.h"
class Personality{
string name;
}
#endif
//project.h #include "Human.h" #include "Personality.h"這就會導致上述提到的不完全型態
"#define PERSONALITY_H_" 後, 接著引入 project.h
此時的 Personality class 尚未宣告 or 尚未完成宣告
project.h 引入 Human.h
Human class 宣告, 但無法計算size, 因為 Personality class 未完成
2014年11月7日 星期五
Eclipse - CDT for C/C++
Last Update: 2014/11/09 14:23+08
Type: Note
Eclipse 安裝 CDT(C/C++ Development Tools)
Help
-> Instal New Software...
-> 選Eclipse版本 ex. Mars - http://download.eclipse.org/releases/mars
-> 勾選 Programming Language-> C/C++ Development Tools 和 C/C++ Development Tools SDK
Type: Note
Intro
Eclipse 安裝 CDT(C/C++ Development Tools)
Help
-> Instal New Software...
-> 選Eclipse版本 ex. Mars - http://download.eclipse.org/releases/mars
-> 勾選 Programming Language-> C/C++ Development Tools 和 C/C++ Development Tools SDK
2014年10月9日 星期四
C++ Split
Last Update: 2014/10/09 16:35+08
Type: Note
用 stringstream by 特定字元切割 ex: ':'
Type: Note
用 stringstream by 特定字元切割 ex: ':'
std::ifstream infile("file.txt");
if (!infile.is_open())
return;
std::string line;
while (std::getline(infile, line)) {
std::stringstream ss(line);
std::string key, val;
std::getline(ss, key, ':');
std::getline(ss, val, ':');
用 istringstream by token (空白) 切割
while (std::getline(infile, line)) {
std::string key, val;
istringstream iss(line);
std::vector<std::string> tokens;
copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
back_inserter<vector<std::string> >(tokens));
if (tokens.size() != 2)
continue;
key = tokens[0];
val = tokens[1];
C++ Interface Destructor
Last Update: 2014/10/09 16:32+08
Type: Note
即使是 interface 也需要有解構子
Type: Note
即使是 interface 也需要有解構子
class IAnimal
{
virtual void walk()=0;
virtual ~IAnimal(){}
};
如果你這樣用IAnimal* animal = new Lion(); delete animal;這個 interface 是不知道 Lion 的解構子
2014年5月27日 星期二
C / C++ format string
Last Update: 2014/05/27 19:00+08
Type: Note
C sprintf - 使用時間當做範例
C++ ostringstream
Type: Note
C sprintf - 使用時間當做範例
#include <time.h> time_t timep; struct tm *p; time(&timep); p=gmtime(&timep); sprintf(fn, "t%02d%02d%02d", p->tm_hour, p->tm_min, p->tm_sec);
C++ ostringstream
#include <string>
#include <sstream>
int no = 12;
std::ostringstream oss;
oss << "data_" << no << ".text";
std::string str = oss.str();
printf("%s\n", str.c_str());
2014年4月19日 星期六
2014年4月8日 星期二
gcc/g++ in Windows - MinGW & Cygwin
Last Update: 2014/04/09 09:35+08
Type: Note
官網(http://www.mingw.org/)下載 MinGW installer
Install
選擇要安裝的資料夾(避免空白和符號)
在選擇 package 時, 將 [Basic Setup] 全部 mark 起來
> Apply Changes
Set Environment Variables
將/bin 加入 環境變數中的 PATH
Eclipse > Project > 右鍵 Properties
> C/C++ General
> Paths and Symbols
> Include
> 在 GNU C Or GNU C++ 加入 <install dir>/include
> 在 Library Paths 加入 <install dir>/lib
官網(http://cygwin.com/install.html)下載 Cygwin installer
Install
> 照指示安裝
過程中有 Local Package Directory, 會把安裝檔載在這, 下次安裝就不用再次下載
> Select Packages 時, 搜尋 "gcc-g++"
安裝 Devel 裡的
cygwin32-gcc-g++: GCC for Cygwin 32 bit toolchain (C++)
gcc-g++: GNU Compiler Collection (C++)
[無效]cmake: Cross-platform makefile generation system
make: The GNU version of the 'make' utility
Set Windows Environment Variables
新增 CYGWIN_HOME=<install dir>
PATH加入 %CYGWIN_HOME%/bin
Type: Note
---MinGW---
Download官網(http://www.mingw.org/)下載 MinGW installer
Install
選擇要安裝的資料夾(避免空白和符號)
在選擇 package 時, 將 [Basic Setup] 全部 mark 起來
> Apply Changes
Set Environment Variables
將
Eclipse > Project > 右鍵 Properties
> C/C++ General
> Paths and Symbols
> Include
> 在 GNU C Or GNU C++ 加入 <install dir>/include
> 在 Library Paths 加入 <install dir>/lib
---Cygwin---
Download官網(http://cygwin.com/install.html)下載 Cygwin installer
Install
> 照指示安裝
過程中有 Local Package Directory, 會把安裝檔載在這, 下次安裝就不用再次下載
> Select Packages 時, 搜尋 "gcc-g++"
安裝 Devel 裡的
cygwin32-gcc-g++: GCC for Cygwin 32 bit toolchain (C++)
gcc-g++: GNU Compiler Collection (C++)
[無效]cmake: Cross-platform makefile generation system
make: The GNU version of the 'make' utility
Set Windows Environment Variables
新增 CYGWIN_HOME=<install dir>
PATH加入 %CYGWIN_HOME%/bin
2013年10月20日 星期日
C++ memory & stack-allocated for local variable
Last Update: 2013/10/20 12:22+08
Type: note
主要是要測試 class 在 function return 時 的問題
但用 int 比較方便解釋, 所以這邊用 int 作測 試
Type: note
Intro
Local Variable 的 memory 位置 和 return value主要是要測試 class 在 function return 時 的問題
但用 int 比較方便解釋, 所以這邊用 int 作測 試
int get1(){ int rs = 1; printf("%d : %d\n", &rs, rs); return rs;}
int* get2(){ int rs = 2; printf("%d : %d\n", &rs, rs); return &rs;}
int& get3(){ int rs = 3; printf("%d : %d\n", &rs, rs); return rs;}
int& get4(){ int *rs = new int(4); printf("%d : %d\n", rs, *rs); return *rs;}
2013年10月19日 星期六
C++ - use of reference(&) or pointer(*) in function parameters
Last Update: 2014/12/20 14:19+08
Type: Note
如何傳遞參數
Type: Note
Intro
如何傳遞參數
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
class Vec2D {
public:
int x, y;
};
2013年5月24日 星期五
C++ Map 操作
Last Update: 2013/05/24 17:43+08
Type: Note
前置
宣告
Type: Note
Content
前置
#include "stdafx.h" #include <string> #include <map> using namespace std;
宣告
int _tmain(int argc, _TCHAR* argv[])
{
map<string, int> maps;
Insert
map<string,int>::iterator it = maps.begin();
maps.insert(it, pair<string,int>("A",26));
maps.insert(it, pair<string,int>("B",27));
maps.insert(it, pair<string,int>("C",28));
maps.insert(it, pair<string,int>("D",29));
maps["E"] = 30;
For Loop
for (it=maps.begin(); it != maps.end(); ++it){
printf("%s = %i\n", it->first.c_str(), it->second);
}
Get
printf("E = %i\n", maps["E"]);
printf("F = %i\n", maps["F"]);//預設為0
Remove
maps.erase("E");
Find
it = maps.find("E");
if(it == maps.end())
printf("not found.\n");
else
printf("%s = %i\n", it->first.c_str(), it->second);
The end...
getchar();
return 0;
}
2013年5月10日 星期五
C++ Macro Note
Last Update: 2013/05/11 21:33+08
Type: Note
該變數可以是 class, function, ...
## 是將2個變數粘在一起, pri##ntf = printf
__FILE__ , __FUNCTION__, __LINE__, ... 是 Compiler 預先就定義好的變數
debug時, 可以print出來, 方便識別
Type: Note
Intro
C++ Macro 筆記
#define GetObjName(x) #x
#define DebugPrint(msg) printf("%s ; %s ; %s ; %d\n", #msg, __FILE__, __FUNCTION__, __LINE__)
#define Contact(x, y) x##y
int _tmain(int argc, _TCHAR* argv[])
{
printf("line: %d \n", __LINE__);
printf("file: %s \n", __FILE__);
printf("function: %s \n", __FUNCTION__);
//printf("function: %s \n", __func__);
printf("time: %s \n", __TIME__);
printf("date: %s \n", __DATE__);
//printf("STDC: %d \n", __STDC__);
//printf("STDC_HOSTED: %d \n", __STDC_HOSTED__);
//printf("VERSION: %s \n", __VERSION__);
printf("TIMESTAMP: %s \n", __TIMESTAMP__);
char* s = GetObjName(MyClass);
printf("%s \n", s);
Contact(pri, ntf) ("exec printf \n");
DebugPrint("Debug");
getchar();
return 0;
}
Macro中的# 代表取得variable名稱該變數可以是 class, function, ...
## 是將2個變數粘在一起, pri##ntf = printf
__FILE__ , __FUNCTION__, __LINE__, ... 是 Compiler 預先就定義好的變數
debug時, 可以print出來, 方便識別
訂閱:
文章 (Atom)