构造函数和高级异常投掷C
Constructors and Advanced Exception Throwing C++
我必须分析此涉及异常的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。并非每种情况都受益于在构造函数中抛出例外。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- 构造函数和高级异常投掷C