前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pointer and the C++ Philosophy

Pointer and the C++ Philosophy

作者头像
青木
发布2018-05-28 15:40:05
3980
发布2018-05-28 15:40:05
举报

Object-oriengted programming differs from traditional procedural programming in that OOP emphasizes making decisions during runtime instead of during compile time. Runtime means while a program is running,and compile time means when the compiler is putting a program together. A runtime decision is like,when on vacation,choosing what sights to see depending on the weather and your mood at the moment,whereas a compile-time decision is more like adhering to a preset schedule,regardless of the conditions. Runtime decision provide the flexibility to adjust to current circumstances.For examle,consider allocating memory for an array.The traditional way is to declare an array.To declare an array in C++,you hava to commit yourself to a particular array size.Thus,the array size is set when the program is compiled;it is a compile-time decision. Perhaps you think an array of 20 elements is sufficient 80% of the time but that occasionall the program will need to handle 200 elements.To be safe,you use an array with 200 elements.This results in your program wasting memory most of the time it's used.OOP tries to make a program more flexible by delaying such decisions until runtime.That way,after the program is running,you can tell it you need only 20 elements one time or that you need 205 elements another time. In short,with OOP you would like to make the array size a runtime decision.To make this approach possible,the lanage has to allow you create an array——or the equivalent——while the program runs.The C++ method,as you soon see,involves using the keyword new to request the correct amount of memory and using pointers to keep track of where the newly allocated memory is found. Making runtime decision is not unique to OOP.But C++ makes writing the code a bit more straightforward than does C.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档