PIXNET Logo登入

夢多了,就會是現實

跳到主文

分享一路上做過的點點滴滴, 記錄種種曾經遇到難關與解決方法 受前人幫忙之餘, 也希望自己能夠盡一點心力幫助他人

部落格全站分類:心情日記

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 1月 28 週日 201801:43
  • [C++] 評估程式碼執行時間

#include <stdio.h>
#include <future>
using namespace std;
int main()
{
double START, END;
START = clock();
// [Your code here..]
END = clock();
char szTime[100];
sprintf_s(szTime, 100, "Execute time : %lf - %lf = %lf \n",END, START, END - START);
printf_s(szTime);
system("PAUSE");
}
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(436)

  • 個人分類:C++
▲top
  • 1月 07 週日 201817:19
  • [C++] static_cast 用法說明 (基礎篇)


語法(Syntax) :


static_cast < new_type > ( expression )

Returns a value of type new_type.


 


1.  可用在很多種情況 但是無法去除(constness or volatility. ),最常見的是implicit conversition
例如將float 轉型成 int
 
int main()
{
// initializing conversion
int n = static_cast<int>(3.14);
std::cout << "n = " << n << '\n';
   
    // 一般來說,我們通常都會直接偷懶,寫成這樣
    // int n = (int)3.14;
std::vector<int> v = static_cast<std::vector<int>>(10);
std::cout << "v.size() = " << v.size() << '\n';
system("PAUSE");
}

 
輸出:
n = 3 
v.size() = 10

 
 2.  Downcast: 向下轉型
這樣的需求在C++開發當中常常需要用到,但是必須要特別注意,若使用static_cast則於runtime期間並不會自行做檢查,必須仰賴開法者自行處理(參考static polymorphism),所以是可能發生致命錯誤哦,一般都會建議使用比較安全的 dynamic_cast 作法來進行downcast 。
#include <iostream>
struct CShape {
int m = 0;
void hello() const {
std::cout << "Hello world, this is CShape!\n";
}
};
struct CCircle : CShape {
void hello() const {
std::cout << "Hello world, this is CCircle!\n";
}
};
int main()
{
// static downcast
CShape shape;
CShape& rshape = shape; // upcast via implicit conversion
rshape.hello();
CCircle& another_Circle = static_cast<CCircle&>(rshape); // downcast
another_Circle.hello();
system("PAUSE");
}
輸出:
Hello world, this is CShape!
Hello world, this is CCircle!

 
 
3.  轉換 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
struct B {
int m = 0;
void hello() const {
std::cout << "Hello world, this is B!\n";
}
};
struct D : B {
void hello() const {
std::cout << "Hello world, this is D!\n";
}
};
int main()
{
//array - to - pointer followed by upcast
D a[10];
B* dp = static_cast<B*>(a);
system("PAUSE");
}

 
 
4. 轉換 scoped enum 為 int 或是 float
enum class E { ONE = 1, TWO, THREE };
enum EU { ONE = 1, TWO, THREE };
int main()
{
E e = E::ONE;
int one = static_cast<int>(e);
std::cout << one << '\n';
// int to enum, enum to another enum
E e2 = static_cast<E>(one);
EU eu = static_cast<EU>(e2);
system("PAUSE");
}
 
 
Ref : http://en.cppreference.com/w/cpp/language/static_cast
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(19,519)

  • 個人分類:C++
▲top
  • 11月 25 週三 201517:10
  • C/C++ : signed int 和 int 宣告差別在哪裡?

最近發現一些範例程式碼在定義整數 int 時寫成這樣
typedef signed int INT32;
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(3,680)

  • 個人分類:C++
▲top
1

熱門文章

  • (60,616)Python 基礎系列 map() 用法解說
  • (31,915)C# 陣列排序
  • (19,519)[C++] static_cast 用法說明 (基礎篇)
  • (12,222)Python 傳值(pass by value) vs 傳址(pass by address) vs 傳物件(pass by object)?
  • (10,881)C# DataTable 合併兩張資料表
  • (9,026)WPF C# DispatcherTimer 用法
  • (5,133)如何使用 C# 取得 Arduino Yun 的輸出資料
  • (3,680)C/C++ : signed int 和 int 宣告差別在哪裡?
  • (629)WPF - 使用DrawingBrush繪製色彩相間的地板/棋盤格
  • (308)SQL 重點語法快速上手(一)

文章分類

  • Android (1)
  • Python (4)
  • Visual studio 技巧 (1)
  • C++ (3)
  • C# 小筆 (5)
  • 心情小筆 (0)
  • SQL 零基礎上手 (5)
  • 未分類文章 (1)

最新文章

  • Android 讓物件動起來 : animatorSet
  • [Python] IOError: [Errno 24] Too many open files:
  • [Python] ** 雙星號(double star/asterisk) vs *單星號(star/asterisk) 用法
  • Python 傳值(pass by value) vs 傳址(pass by address) vs 傳物件(pass by object)?
  • [C++] 評估程式碼執行時間
  • [C++] static_cast 用法說明 (基礎篇)
  • Python 基礎系列 map() 用法解說
  • C# 陣列排序
  • 如何使用 C# 取得 Arduino Yun 的輸出資料
  • 在visual studio2013 / 2015 使用XNA

動態訂閱

文章搜尋

參觀人氣

  • 本日人氣:
  • 累積人氣: