LearnHouse

[C/C++]虛擬函式(Virtual function)與早期連結(Early Binding)、晚期連結(Late Binding)?

最近在寫模擬商店發出訊息的量測程式,使用JAVA來寫multi-thread Socket產生多條連線

JAVA可以說大學修完課後四年就沒在碰,那時也只會寫一般的數學運算並將結果印出來

所以可想而知,我是用C/C++的觀念在寫JAVA = ="

寫過C的人,應該都會覺得要站在巨人的肩膀上,才能寫出好程式

意思就是說,不透過別人現成寫好的source code來改的話

別說要寫好一個multi-thread socket,光單一條socket要建立連線就有得搞了 = ="

但這篇我主要是要講C++物件導向裡一個重要的觀念

老樣子,先隨便舉個之前的例子

class  Car
{
public:
void CarInfo()
{
printf("-------------------------\n");
printf("一般普通房車 - 新款資訊\n");
printf("-------------------------\n");
}
};

class Honda : public Car
{
public:
void CarInfo()
{
printf("-------------------------\n");
printf("Honda房車 - 新款資訊\n");
printf("-------------------------\n");
}
};

Honda繼承了Car,而且又有相同的函式CarInfo()

所以父類別的CarInfo()會被override

那麼在main裡面輸入下面的程式碼,會印出什麼結果呢?

Car *mycar;
mycar = new Honda();
mycar->CarInfo();

將將將!! 答案是印出

-------------------------
一般普通房車 - 新款資訊
-------------------------

為什麼會是這樣的結果呢??

因為編譯器預設是早期連結(Early Binding),它是一個在編譯時期就需決定的方式

那要怎麼才能使用晚期連結呢(Late Binding),它是一個在執行時期才會得知所要調用的物件

其實很簡單,只要加入virtual虛擬函式宣告即可,如下

class  Car
{
public:
virtual void CarInfo()
{
printf("-------------------------\n");
printf("一般普通房車 - 新款資訊\n");
printf("-------------------------\n");
}
};

這樣就能印出

-------------------------
Honda房車 - 新款資訊
-------------------------

這是一種「編譯時期」就需決定的方式,這是「早期繫 結」(Early binding)
posted by admin in 程式&軟體 and have Comments (9)

9 Responses to “[C/C++]虛擬函式(Virtual function)與早期連結(Early Binding)、晚期連結(Late Binding)?”

  1. I do not even know how I ended up here, but I thought this post
    was great. I do not know who you are but definitely you are going to a famous blogger if you aren't already ;) Cheers!

  2. Undeniably believe that which you stated. Your favorite reason appeared to be on the web
    the easiest thing to be aware of. I say to you, I definitely
    get annoyed while people think about worries that they just don't know about.
    You managed to hit the nail upon the top as well as defined out
    the whole thing without having side-effects , people
    can take a signal. Will likely be back to get more.

    Thanks

  3. this content 說:

    Hello, Neat post. There is an issue along with
    your website in web explorer, might check this?
    IE nonetheless is the market leader and a good part of other folks will omit your great writing because of this problem.

  4. Hi there, just wanted to say, I enjoyed this post.
    It was helpful. Keep on posting!

  5. Hi mates, good post and nice urging commented at this
    place, I am truly enjoying by these.

  6. naturally like your web site however you need to test the spelling on quite
    a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to tell the
    truth however I'll certainly come again again.

  7. What's up, yes this article is really good and I have learned lot
    of things from it about blogging. thanks.

  8. see more 說:

    It's an remarkable piece of writing in support
    of all the internet visitors; they will take advantage from it I am
    sure.

  9. I'm not sure why but this site is loading extremely slow for me.
    Is anyone else having this issue or is it a problem on my end?
    I'll check back later and see if the problem still exists.

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment