×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

a example about readonly and writeonly variables

I am reading some article about embedded programming. there is an example about read only and write only data type. if you want to implement a read only type, you can use const as both in c and c++. however, if you want to implement a writeonly type, there is no way you can do it in C. but in C++, you can overload the type's operator=, and define this operator overloading as private function. then when you read an object with this type, compiler will report error, because you are calling a private function. this is just a very interesting example.
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / system level / kernel/ hardware/low level io and 1M+ source code programing. is c++ good?
    • Do you have any option?
      • c
        • C is better, because C++ would make your program unnecessaryly bigger and way more complicated.
    • C is the abstraction of hardware, C++ is the abstrction of real world, object.
      • good point.
    • 其实,基本上所有的操作系统和大部分的DRIVER都是用C写的.就是不明白为什么有些人只说C++好?我接触到的程序,绝大部分也是C.请问各位,成功地把C++用到了什么地方了?GUI是一个.
      • low level说的是OO好,不是C++好:),APP上C++开发效率要比C高。
      • C++的代码重用率高,用C实现多态和TEMPLATE挺难的。而且C++可以用STL
        • 重用率高 sometimes not necessary a good thing.
          • It makes sense for most cases
      • 觉的C++没有C好, 是因为你没用好.
        1. C++ Type Checking is much more tight than C, a lot of run time error can be easily detected at compile time.

        2. Even though you write program in C, you should still use C++ compiler to compile your program, it can report more warnings and errors than C compiler.

        3. some features can't be implemented using C, but can be implemented using C++.

        4. C++ doesn't necessarily down grade your program's performance. you may need more time to write the program, but after compile, the performance is actually very close to C.

        5. there are more tricks in C++ and C, if you are familar with all those tricks. you can write program in C++ almost has the same footprint as C program
        • some features can't be implemented using C, but can be implemented using C++.? please give example. I hope when you write feature, you mean something else.
          • a example about readonly and writeonly variables
            I am reading some article about embedded programming. there is an example about read only and write only data type. if you want to implement a read only type, you can use const as both in c and c++. however, if you want to implement a writeonly type, there is no way you can do it in C. but in C++, you can overload the type's operator=, and define this operator overloading as private function. then when you read an object with this type, compiler will report error, because you are calling a private function. this is just a very interesting example.
            • write only type? you mean allocate the variable in the EPROM? I can do it in any C. Or you mean you put something in RAM and don't want it modified? Do you enjoy make the source code so huge and complicated?
              Is it any good to do that? After all, no programmer is so fool to assigne a value to if you give it a name _writeOnly.

              That said, if C++ could force the array bound checking in an easy way, it would be good.

              And the pointer is always kind of headache.

              I think C++ should learn something from Java. Unfortuanetely, it is going to the more and more complex way.
              • c++ is too complicated, sometimes it is kind of out of control
              • actually write only type is very useful in embedded programming
                some hardware registers are read only, some registers are write only. usually, register are mapped to certian memory address and accessed as pointers. if you read a write only register may generate unspecified behavior.

                of course, you can use _writeOnly to decorat the name, but you are still rely on programmer to follow it. if programmer make mistake or just want to sabotage it, it is hard to find it. If language give you this kind of capability, the compiler will fail at compile time and report this is a bug.

                I couldn't find a way to implement this feature using C.
        • Seem you are good at C++. We can discuss in more detail. Ok? I will give some examples when I have time too.
          • I am actually not very good at C++, I just spend a lot of time working on C.
        • example 1: I want to write to the I/O, when I/O block, please tell me why it block. How may bytes are outputed. Please use POSIX and C++ standards. Don't use OS specific functions.
          I don't ask you to implement this fully. But let us see how C++ handle such problem.
          • 看不懂,你是指nOutputSize = write(nFileHandler.xxx)?不如写中文吧
    • 以前开发单板,用C++和RTOS,仅仅系统开销、类的管理等公共模块就增长到200KSLOC,真正驱动和功能模块仅仅数千行,需要4M内存和32位CPU,搞笑得很。其实本来8051就足够了,不过工程师都喜欢把简单东西搞复杂,增长经验外带拖延项目,呵呵
      • 把东西搞得没有必要的复杂是最根本的专业精神,看看美国的律师和会计师,如果你总是用最简单的办法解决问题,你打破的不光是自己的饭碗。
      • 偶欣赏楼上的说法。如果只图简单,只用算盘,外加比如史丰收\快速算法,那老美也不会发明那在当年非常庞大 AND 复杂 (!) 的计算机。
        • 比较比较搞笑
      • 在8051上面用汇编,以后的维护就成了nightmare.
        • 8051 has c. you are too out.
          • It's true. I'm not in that industry after I graduated.
      • 你说得很道理,可惜我是老实人,总是想着:简单就是最好的.
      • OO不一定非要C++,C也可以。在嵌入领域目前的C++ compiler不是很完美。
        • I am working on embedded programming. company coding standard requires that the code we write must pass about 10 C/C++ compilers without warnings. Simply put, C++ compiler can detect more potential problems than C.
          • CZ C++ compiler is stronger typed than C?
          • I did not mean C++ compiler is not "strong" enough, I am talking it is not smart enough, so you know it would make your code bigger than neccessary.
            in some environemnt, that is not a problem, who cares the difference between 500M and 700M if your box has 2 G memory?
            but in some condition, people do cares. in that case C++, or maybe I should say some C++ way should be forbidden, such as metaprogramming and most of templates.
            • it depends on if you are using a good C++ compiler or not.
              some good C++ compiler can eleminate storage space if it detect it is not necessary to allocate it.
              • hehe, no comments, but at least c++ compiler could your program bigger then C compiler does:)
          • 那只是编译器的功能,和语言无关。C语言源代码可以用LINT等工具检查,所有潜在错误都可以查出来。C++只不过是C的扩展,没完全遵守OO规则,而且内存管理漏洞很多。搞笑得是,有些编译器要先把C++先转化成C,然后再编译成可执行文件
            几年前评估过,就记住个大概。其实,只要理解了软件工程和OO,什么语言都一样
            • 。。。。。C语言编译的时候是生成!@#$%^&_foo()的代码,而c++是生成!@#$classname_!@#$foo()格式
    • 选择语言并不只是看编译效率,运行效率,还要看编程效率
      比如说,如果某个行业已经有一大堆C的库函数,那么选择C是顺理成章的事情。这也是为什么“基本上所有的操作系统和大部分的DRIVER都是用C写的”,历史渊源。

      同时,如果某个开发需要经常更新,需要一些面向对象的特性,那么就只能选择C++。因为总的说来面向对象的C++比较容易维护一些。

      甚至,开发小组人员熟悉哪个语言,也会影响语言的选择。如果大家一直都在用某个语言,当然在开发新的程序的时候,也会用同一个语言,缩短开发周期。
      • 对于有经验的程序员,就算是ASM,也是RAD的.只有新人才用RAD TOOLS, i.e. c++.
    • 据我浅显的了解,国内做银行电信系统集成的大的公司都用C(时髦点的项目有用java的,如crm),小公司用c++的就多点.benlin说的对,大的公司积累多,不用可惜,再说都是项目导向,客户谁管你用啥语言.加拿大纯c的用的太少,C++,java,.net才能出头.
      btw,我也是unix c,无用武之地啊.
      • 补充一点:银行电信界面有关的很多项目是用PB的,比java多多了.(也许现在改.net了吧,离开1年半不了解了.)
      • 不会吧,我以前公司做的EMAIL/BOSS等电信系统都是C/C++各一半的,你说的C++是指一定要用G++而不能用GCC编译的CODE?
        • 管见,管见,呵呵
          • 什么意思?
            • 意思是:我所说的只是自己的一管之见.