构造函数和高级异常投掷C

Constructors and Advanced Exception Throwing C++

本文关键字:异常 高级 构造函数      更新时间:2023-10-16

我必须分析此涉及异常的C 代码,但我不使用分析该代码应该执行的操作。我不明白这几件事。此外,我没有一些缓冲数据结构的经验或在C 中投掷例外的高级详细信息。

数据结构的最大大小是多少?

  • 也就是说,如果用size_ = 100制作seq_buffer,多少可以存储元素?
  • 我的直觉告诉我,如果您创建具有100尺寸的东西,最大尺寸将是100,但我无法确定事实。

构造函数可以在C ?

中抛出异常
  • seq_buffer的构造函数可以抛出异常吗?
  • 假设这是真的,为什么它可以在不阐明的情况下起作用说明尝试,捕捉还是投掷?我认为这是获得异常的唯一方法

我已经尝试搜索这两个问题,但我确实迷路了。感谢您抽出宝贵的时间阅读。我尝试搜索这两个问题,但我确实迷失了方向。感谢您抽出宝贵的时间阅读。

seq_buffer类的构造函数的代码如下:

#ifndef SEQBUFFER_H
#define SEQBUFFER_H
#include <memory>
#include <experimental/optional>
#include "optional.h"
// Exceptions classes
struct full_buffer {};
struct empty_buffer {};
// Sequential buffer for values of type T
template <typename T>
class seq_buffer {
public:
  // Constructs a buffer for n elements
  seq_buffer(int n) :
    size_{n},
    buf_{new item_type[size_]}
  {
  }
  // Default destructor
  ~seq_buffer() = default;
  // Size of buffer
  int size() const noexcept { 
    return size_; 
  }
  // Is buffer empty?
  bool empty() const noexcept { 
    return next_read_ == next_write_; 
  }
  // Is buffer full?
  bool full() const noexcept {
    const int next = next_position(next_write_);
    return next == next_read_;
  }
  // Put element x into buffer with marker last.
  // An empty element signals end of buffer.
  void put(const optional<T> & x);
  // Gets a pair with next element and last indication.
  // Pair is accessed through members first and second
  optional<T> get();
private:
  // Compute next position after p following circular order
  int next_position(int p) const noexcept {
    return p + ((p+1>=size_)?(1-size_):1);
  }
private:
  // Size of buffer
  const int size_;
  using item_type = optional<T>;
  // Unique pointer to buffer of size_ elements.
  std::unique_ptr<item_type[]> buf_;
  // Next position to read
  int next_read_ = 0;
  // Next position to write
  int next_write_ = 0;
};
template <typename T>
void seq_buffer<T>::put(const optional<T> & x)
{
  const int next = next_position(next_write_);
  if (next == next_read_) throw full_buffer{};
  if (!x) {
    buf_[next_write_] = {};
  }
  else {
    buf_[next_write_] = *x;
  }
  next_write_ = next;
}
template <typename T>
optional<T> seq_buffer<T>::get()
{
  if (empty()) throw empty_buffer{};
  auto res = buf_[next_read_];
  next_read_ = next_position(next_read_);
  return res;
}
#endif

是的,我们可以从构造函数上抛出异常。这是处理构造函数失败或类初始化错误的最佳方法。请访问此代码示例

class bar
{
public:
  bar()
  {
    std::cout << "bar() called" << std::endl;
  }
  ~bar()
  {
    std::cout << "~bar() called" << std::endl;
  }
};
class foo
{
public:
  foo()
    : b(new bar())
  {
    std::cout << "foo() called" << std::endl;
    throw "throw something";
  }
  ~foo()
  {
    delete b;
    std::cout << "~foo() called" << std::endl;
  }
private:
  bar *b;
};

int main(void)
{
  try {
    std::cout << "heap: new foo" << std::endl;
    foo *f = new foo();
  } catch (const char *e) {
    std::cout << "heap exception: " << e << std::endl;
  }
  try {
    std::cout << "stack: foo" << std::endl;
    foo f;
  } catch (const char *e) {
    std::cout << "stack exception: " << e << std::endl;
  }
  return 0;
}

在这里,您会从构造函数本身中抛出异常。但是在某些情况下,您可以在构造函数中分配内存(HEAP)。在这种情况下,在构造函数中抛出异常并不有用,因为这会导致内存泄漏。因为如果类未能初始化,那么就不会有destructor供该类命令,因为已经存在异常(假设在灾难中,使用免费的自由释放了分配的内存)。case。并非每种情况都受益于在构造函数中抛出例外。