一、轉換構造函數的學習:
1、回憶數據類型轉換:
在平時寫代碼的時候,最怕的就是那種隱式數據類型轉換了,一不小心,軟件就bug不斷;而顯示數據類型(一般是程序自己去強制類型轉換,這個是我們能夠明顯的識別和掌控的)。為此我們這里總結了一副隱式類型轉換的圖:
下面我們來幾個隱式轉換的例子:
代碼版本一:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"Postive"<<std::endl;
}
else
{
std::cout<<"Negative"<<std::endl;
}
return 0;
}
輸出結果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
Postive
注解:這里我們明顯發現(-200+100)還是大于0,這顯然不符合正常人的思維了;所以我們仔細分析一下,發現這里肯定是進行了隱式轉換了,為此我們再加一條語句看看(ui+i)的值到底是多少:
代碼版本二:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"(ui+i) = "<<ui+i<<std::endl;
std::cout<<"Postive"<<std::endl;
}
else
{
std::cout<<"Negative"<<std::endl;
}
return 0;
}
輸出結果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
(ui+i) = 4294967196
Postive
注解:通過打印(ui+i)的值我們發現,i原本是int數據類型,這里隱式轉換成無符號的數據類型了
為了讓大家更加理解隱式的轉換,我們下面再來一個例子:
代碼版本三:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"(ui+i) = "<<ui+i<<std::endl;
std::cout<<"Postive"<<std::endl;
}
else
{
std::cout<<"Negative"<<std::endl;
}
std::cout<<"sizeof(s+'b') = "<<sizeof(s+'b')<<std::endl;
return 0;
}
輸出結果:
root@txp-virtual-machine:/home/txp# g++ test.cpp
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
(ui+i) = 4294967196
Postive
sizeof(s+'b') = 4
注解:這里我們發現sizeof出來的內存大小是4個字節大小;其實這里編譯器把short和char類型的都轉換int類型了,所以最終兩個int數據相加,所占的內存大小就是int類型了。
所以咋們平時在寫代碼的時候,腦袋里面要有這種寫代碼謹慎的思維,防止出現這種隱式轉換的情況出現,養成寫代碼的好習慣
2、普通類型與類類型之間能否進行類型轉換,類類型之間又是否能夠類型轉換呢?
為了說明這些問題,咋們通過實際的代碼測試來看看啥情況:
代碼:普通類型轉換成類類型
#include <iostream>
#include <string>
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
t =6; 從 C 語言角度,這里將 5 強制類型轉換到 Test 類型,只不過編譯器 在這里做了隱式類型轉換
return 0;
}
輸出結果(顯示可以編譯通過)
root@txp-virtual-machine:/home/txp# g++ test.cpp
root@txp-virtual-machine:/home/txp# ./a.out
代碼類類型轉換為普通類型
#include <iostream>
#include <string>
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
int i = t;
return 0;
}
輸出結果(沒有編譯通過)
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:21:14: error: cannot convert ‘Test’ to ‘int’ in initialization
int i = t;
^
代碼類類型與類類型之間的轉換:
#include <iostream>
#include <string>
class Value{
};
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
Value i;
t=i;
return 0;
}
輸出結果(暫時還是不行,編譯不通過):
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:27:7: error: no match for ‘operator=’ (operand types are ‘Test’ and ‘Value’)
t=i;
^
test.cpp:27:7: note: candidate is:
test.cpp:9:7: note: Test& Test::operator=(const Test&)
class Test{
^
test.cpp:9:7: note: no known conversion for argument 1 from ‘Value’ to ‘const Test&’
說明:上面的例子,我們只是簡單的按照實際角度出發,發現確實有寫轉換行不通。那么真理到底是怎樣的?我們接著往下看
-
可編程邏輯
+關注
關注
7文章
516瀏覽量
44111 -
C++
+關注
關注
22文章
2112瀏覽量
73706
發布評論請先 登錄
相關推薦
ADS1299EEGFE-PDK在window上如何通過C++讀取8通道數據?
C++新手容易犯的十個編程錯誤
OpenCV圖像識別C++代碼
FX2 CY7C68013A如何在C++環境中使用LoadEEPROM函數?
鴻蒙OS開發實例:【Native C++】
![鴻蒙OS開發實例:【Native <b class='flag-5'>C++</b>】](https://file1.elecfans.com/web2/M00/C8/31/wKgZomYZMTCAaDv3AAY5x13C324319.jpg)
使用 MISRA C++:2023? 避免基于范圍的 for 循環中的錯誤
![使用 MISRA <b class='flag-5'>C++</b>:2023? 避免基于范圍的 for 循環中的錯誤](https://file1.elecfans.com/web2/M00/A9/66/wKgZomUl7m-AHJX6AABuJjgxs14678.png)
C語言數據類型有哪些
![<b class='flag-5'>C</b>語言數據<b class='flag-5'>類型</b>有哪些](https://file1.elecfans.com/web2/M00/C5/16/wKgZomX6UKqAb6q4AABHS7gAoYk070.png)
c語言,c++,java,python區別
C++簡史:C++是如何開始的
![<b class='flag-5'>C++</b>簡史:<b class='flag-5'>C++</b>是如何開始的](https://file1.elecfans.com/web2/M00/A9/66/wKgZomUl7m-AHJX6AABuJjgxs14678.png)
評論