[CryptoPP userguide]BufferedTransformation示例

本文介绍如何解决DumpMessage.cpp中的编译错误,并提供了一段修正后的代码示例。通过对字符串源对象构造方式的调整,成功避免了临时变量引用无效的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. //DumpMessage.cpp
  2. #include "../cryptlib/cryptlib.h"
  3. #include "../cryptlib/filters.h"

  4. #include <iostream>

  5. void DumpMessage(CryptoPP::BufferedTransformation& bt){
  6.   using namespace std;

  7.   static char const* szHexDigits = "0123456789abcdef";
  8.   byte b;
  9.   while(bt.AnyRetrievable()){
  10.     if(!bt.Get(b))
  11.     throw "Error: AnyRetrievable() returned true,""so this shouldn't happen";
  12.     //it is much easier to implement this using HexEncoder;
  13.     //however, let's not get into that just yet. this below code
  14.     //could do some special kind of processing that is not
  15.     //supported by off-the-shelf Filter class.

  16.     cout << szHexDigits[(b >> 4) & 0x0f]
  17.         << szHexDigits[b & 0x0f]
  18.         << ' ';
  19.   }
  20. }

  21. int main(){
  22.   using namespace CryptoPP;

  23.   char const* szMessage = "How do you do?";
  24.   StringSource ss(szMessage, true);
  25.   DumpMessage(ss);

  26.   //if we constructed stringsource with 'false' instead of true, 
  27.   // stringsource wouldn't call its PumpAll() method on construction,
  28.   //and no data would be extractable from the stringsource object
  29.   //until someone called the object's Pump() or PumpAll() method

  30.   return 0;
  31. }

userguide中的原代码无法编译通过,会报一个invalid initialization of non-const reference of type ... from a temporary of type的错误。

所以将其修改为30行的代码,将StringSource对象用构造函数构造成一个具有生命的变量(am I right),就可以解决问题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值