一、轉(zhuǎn)換構(gòu)造函數(shù)的學(xué)習(xí):
1、回憶數(shù)據(jù)類(lèi)型轉(zhuǎn)換:
在平時(shí)寫(xiě)代碼的時(shí)候,最怕的就是那種隱式數(shù)據(jù)類(lèi)型轉(zhuǎn)換了,一不小心,軟件就bug不斷;而顯示數(shù)據(jù)類(lèi)型(一般是程序自己去強(qiáng)制類(lèi)型轉(zhuǎn)換,這個(gè)是我們能夠明顯的識(shí)別和掌控的)。為此我們這里總結(jié)了一副隱式類(lèi)型轉(zhuǎn)換的圖:
下面我們來(lái)幾個(gè)隱式轉(zhuǎn)換的例子:
代碼版本一:
#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;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
Postive
注解:這里我們明顯發(fā)現(xiàn)(-200+100)還是大于0,這顯然不符合正常人的思維了;所以我們仔細(xì)分析一下,發(fā)現(xiàn)這里肯定是進(jìn)行了隱式轉(zhuǎn)換了,為此我們?cè)偌右粭l語(yǔ)句看看(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;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
(ui+i) = 4294967196
Postive
注解:通過(guò)打印(ui+i)的值我們發(fā)現(xiàn),i原本是int數(shù)據(jù)類(lèi)型,這里隱式轉(zhuǎn)換成無(wú)符號(hào)的數(shù)據(jù)類(lèi)型了
為了讓大家更加理解隱式的轉(zhuǎn)換,我們下面再來(lái)一個(gè)例子:
代碼版本三:
#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;
}
輸出結(jié)果:
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
注解:這里我們發(fā)現(xiàn)sizeof出來(lái)的內(nèi)存大小是4個(gè)字節(jié)大??;其實(shí)這里編譯器把short和char類(lèi)型的都轉(zhuǎn)換int類(lèi)型了,所以最終兩個(gè)int數(shù)據(jù)相加,所占的內(nèi)存大小就是int類(lèi)型了。
所以咋們平時(shí)在寫(xiě)代碼的時(shí)候,腦袋里面要有這種寫(xiě)代碼謹(jǐn)慎的思維,防止出現(xiàn)這種隱式轉(zhuǎn)換的情況出現(xiàn),養(yǎng)成寫(xiě)代碼的好習(xí)慣
2、普通類(lèi)型與類(lèi)類(lèi)型之間能否進(jìn)行類(lèi)型轉(zhuǎn)換,類(lèi)類(lèi)型之間又是否能夠類(lèi)型轉(zhuǎn)換呢?
為了說(shuō)明這些問(wèn)題,咋們通過(guò)實(shí)際的代碼測(cè)試來(lái)看看啥情況:
代碼:普通類(lèi)型轉(zhuǎn)換成類(lèi)類(lèi)型
#include <iostream>
#include <string>
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
t =6; 從 C 語(yǔ)言角度,這里將 5 強(qiáng)制類(lèi)型轉(zhuǎn)換到 Test 類(lèi)型,只不過(guò)編譯器 在這里做了隱式類(lèi)型轉(zhuǎn)換
return 0;
}
輸出結(jié)果(顯示可以編譯通過(guò))
root@txp-virtual-machine:/home/txp# g++ test.cpp
root@txp-virtual-machine:/home/txp# ./a.out
代碼類(lèi)類(lèi)型轉(zhuǎn)換為普通類(lèi)型
#include <iostream>
#include <string>
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
int i = t;
return 0;
}
輸出結(jié)果(沒(méi)有編譯通過(guò))
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;
^
代碼類(lèi)類(lèi)型與類(lèi)類(lèi)型之間的轉(zhuǎn)換:
#include <iostream>
#include <string>
class Value{
};
class Test{
public:
Test()
{
}
Test(int i)
{
}
};
int main()
{
Test t;
Value i;
t=i;
return 0;
}
輸出結(jié)果(暫時(shí)還是不行,編譯不通過(guò)):
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&’
說(shuō)明:上面的例子,我們只是簡(jiǎn)單的按照實(shí)際角度出發(fā),發(fā)現(xiàn)確實(shí)有寫(xiě)轉(zhuǎn)換行不通。那么真理到底是怎樣的?我們接著往下看
-
可編程邏輯
+關(guān)注
關(guān)注
7文章
523瀏覽量
44504 -
C++
+關(guān)注
關(guān)注
22文章
2116瀏覽量
74600
發(fā)布評(píng)論請(qǐng)先 登錄
西門(mén)子TIA Portal中函數(shù)FC和函數(shù)塊FB的相互轉(zhuǎn)換

Spire.XLS for C++組件說(shuō)明

同樣是函數(shù),在C和C++中有什么區(qū)別
ADS1299EEGFE-PDK在window上如何通過(guò)C++讀取8通道數(shù)據(jù)?
C++新手容易犯的十個(gè)編程錯(cuò)誤
C語(yǔ)言和C++中結(jié)構(gòu)體的區(qū)別
C7000優(yōu)化C/C++編譯器

OpenVINO2024 C++推理使用技巧
c++編譯后鏈接失敗的原因?如何解決?
ModusToolbox 3.2在c代碼中包含c++代碼的正確步驟是什么?
C++語(yǔ)言基礎(chǔ)知識(shí)
C++中實(shí)現(xiàn)類(lèi)似instanceof的方法

評(píng)論