捕获的std :: exception是否可以使what()为NULL?
检查e.what()是否低于开销?
//...
}
catch (const std::exception& e)
{
std::string error;
if(e.what())
error = e.what();
}
Run Code Online (Sandbox Code Playgroud) 我尝试std::ifstream
使用一个函数进行扩展,以便更轻松地读取二进制变量,令我惊讶的是,using std::ifstream::ifstream;
移动构造函数没有被继承。更糟糕的是,它被明确删除。
#include <fstream>
class BinFile: public std::ifstream
{
public:
using std::ifstream::ifstream;
//BinFile(BinFile&&) = default; // <- compilation warning: Explicitly defaulted move constructor is implicitly deleted
template<typename T>
bool read_binary(T* var, std::streamsize nmemb = 1)
{
const std::streamsize count = nmemb * sizeof *var;
read(reinterpret_cast<char*>(var), count);
return gcount() == count;
}
};
auto f()
{
std::ifstream ret("some file"); // Works!
//BinFile ret("some file"); // <- compilation error: Call to implicitly-deleted copy constructor of 'BinFile'
return ret; …
Run Code Online (Sandbox Code Playgroud) 是否可以在没有源代码的情况下从C++库扩展类?标题是否足以允许您使用继承?我正在学习C++并且正在进入理论.我会测试这个,但我不知道怎么做.
我正在创建一个从STL库继承队列的新类.该类的唯一补充是向量.此向量将具有相同的队列大小,并将存储一些整数值,这些值将对应于队列中的每个对象.
现在,我想覆盖pop()和push(),但我只是想为父类的方法添加更多功能.
恩.当在队列对象上调用pop()时,我还想从向量中弹出一个对象.当在队列对象上调用push()时,我还想在向量中插入一个新对象.
我怎么做???
#include <iostream>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
template <typename type>
class CPU_Q : public queue<type>
{
public:
vector<int> CPU_TIME;
void increaseTime()
{
for(int ndx = 0; ndx < CPU_TIME.size(); ndx++)
{
CPU_TIME[ndx]++;
}
}
void push(type insertMe)
{
//This is what I want to do
super::push(); // or queue::push(); maybe?
CPU_TIME.push_back(0);
}
void pop()
{
//Something very similar to push()
}
}
Run Code Online (Sandbox Code Playgroud)
许多人提前感谢
- 三
首先,我知道该std::string
课程具有我可能需要的所有功能.这仅用于测试目的,以了解我将来能够做些什么.
无论如何,说我有这个:
class MyString : public std::string { }
Run Code Online (Sandbox Code Playgroud)
那么我怎么会使用:
MyString varName = "whatever";
Run Code Online (Sandbox Code Playgroud)
因为肯定我会得到一个错误,因为"什么"是一个std :: string而不是MyString类的成员?
如果你理解我的意思,我该如何解决这个问题?
(顺便说一句,我知道这可能是一个非常愚蠢的问题,但我很好奇)