Think before you code, Virtual Functions in C++
来源:互联网 发布:网络推广多少钱一年 编辑:程序博客网 时间:2024/05/21 18:37
A few days back, I was doing a job, and unintentionally, I made a mistake in the code (What mistake? That I will explain in the detailed section of the article), and when I was caught by a bug and started de-bugging it, I was amazed how a little mistake can give a programmer a whole lot of pain. Yes, I made a mistake in the virtual function area. How? Let's find out........
Using the codeSo, why do we need a virtual function? Everyone knows that. Let's say I have a base class and a few derived class as well; and all the derived classes shares a common function, and in the driver program, I do not want to make a big huge switch/if block. I want to iterate through all the derived types and want to execute the common member function. Like this:
Here, the idea is simple. Since we are using a virtual function in the base class, the “whichPhoneUserIsUsing()” method can take a generic base class argument, and the proper method from the derived class gets accessed depending upon the actual type of the object. This is the beauty of virtual functions. Note that in the method “whichPhoneUserIsUsing()”, we used a reference to the base class as the argument: “CommunicationDevices &devices”, and from the driver (main()), we are passing the derived class' object while calling this function. This is normally called as Upcasting in C++. That is, we are going from the more specific type to the more generic type. And, this casting is type-safe always. As you expected, this code will produce the following o/p:
Now, consider the following code, only a single character (believe me, just a single character) has been changed here from the previous code:
We just modified our method whichPhoneUserIsUsing() like this:
We just modified our method whichPhoneUserIsUsing() like this:
and bang.................given below is the output:
So, what gets wrong here?
Yes, you guessed it correctly, it's a famous copy-constructor problem. When the arguments are just a “CommunicationDevices” instead of a reference to it, the function says:
Hey Mr. Programmer, I am bound to create only a temporary object for this function (whichPhoneUserIsUsing()). I am no more responsible to take a reference, so I don't care what kind of actual object you are passing through; I will create a concrete “CommunicationDevices” object, and will copy only those segments from the actual object which are meaningful to me (i.e., which are part of the base class). And, will only invoke the “which” method for this temporary object. And hence, every time you call me, I will call the base class version (i.e., CommunicationDevices version) of the which() method.
This famous property is called Object Bisection, or Object Slicing. Cutting down the desired property from one object and copying it to a concrete base class object.
References:- C++ Programming Language: Bjarne Stroustrup
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
programmersmind
Member int main(){
while(!isSleeping())
{
write_code();
}
return 0;
}
Occupation: Software Developer (Senior) Company: Rebaca Technologies Location: India
- Think before you code, Virtual Functions in C++
- Think before you code, Virtual Functions in C++
- How do virtual functions work in C++?
- think more before you do!
- Functions that are executed before and after main() in C
- Search Functions in Code
- Think before you leap - 谈寻找重复元素
- Overloading Functions in C
- [Built-in Functions] - C
- Study in Coursera Before you study abroad
- Think in Code-用代码思考
- Think in Code,用代码思考
- 袁红岗:Think in Code,用代码思考
- 袁红岗:Think in Code,用代码思考
- virtual functions
- virtual functions
- The File Functions in C
- OpenCL C Built-IN Functions
- 和自己赛跑 (9月15日)
- A Prelude to Pointers
- Synchronization in Multithreaded Applications with MFC
- A serialization primer - Part 1
- A serialization primer - Part 2
- Think before you code, Virtual Functions in C++
- MFC under the hood
- Why I Chose C++
- VC中使用ADO操作数据库的方法
- C++中几个比较不常用的关键字
- Boost库的编译
- 一本看到技巧又能保持阳光心态励志书(来自苏鹏的推荐)
- C++中常用的几个小技巧
- 字符串处理技巧