码迷,mamicode.com
首页 > 其他好文 > 详细

std::unique_ptr的用法

时间:2018-01-30 22:49:01      阅读:564      评论:0      收藏:0      [点我收藏+]

标签:空指针   config   operator   ice   private   send   event   cout   自动   

  std::ofstream("demo.txt") << x; // 准备要读的文件
  {
      std::unique_ptr<std::FILE, decltype(&std::fclose)> fp(std::fopen("demo.txt", "r"),
                                                            &std::fclose);
      if(fp) // fopen 可以打开失败;该情况下 fp 保有空指针
        std::cout << (char)std::fgetc(fp.get()) << \n;
  } // fclose() 调用于此,但仅若 FILE* 不是空指针
    // (即 fopen 成功)

自动调用fclose

struct CallHelper {
  CallHelper() {
    webrtc::AudioState::Config audio_state_config;
    audio_state_config.audio_mixer =
        new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
    audio_state_config.audio_processing =
        new rtc::RefCountedObject<webrtc::test::MockAudioProcessing>();
    audio_state_config.audio_device_module =
        new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
    webrtc::Call::Config config(&event_log_);
    config.audio_state = webrtc::AudioState::Create(audio_state_config);
    call_.reset(webrtc::Call::Create(config));
  }

  webrtc::Call* operator->() { return call_.get(); } // 注意此处

 private:
  webrtc::RtcEventLogNullImpl event_log_;
  std::unique_ptr<webrtc::Call> call_;
};
TEST(CallTest, CreateDestroy_AudioSendStream) {
  CallHelper call;
  AudioSendStream::Config config(nullptr);
  config.rtp.ssrc = 42;
  AudioSendStream* stream = call->CreateAudioSendStream(config); // 注意此处
  EXPECT_NE(stream, nullptr);
  call->DestroyAudioSendStream(stream);
}

 

std::unique_ptr的用法

标签:空指针   config   operator   ice   private   send   event   cout   自动   

原文地址:https://www.cnblogs.com/cyh706510441/p/8387266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!