我正在为我的库设计一个C++的异常层次结构."层次结构"是从std :: runtime_error派生的4个类.我想避免异常类的切片问题,因此使复制构造函数受到保护.但显然gcc需要在抛出它们的实例时调用复制构造函数,因此抱怨受保护的复制构造函数.Visual C++ 8.0编译相同的代码.是否有任何可移植的方法来解决异常类的切片问题?标准是否说明实现是否可以/应该要求抛出要抛出的类的复制构造函数?
我试图将一些子类元素添加到一个向量中,遍历它们调用一个重写的方法,并希望它在可能的情况下调用重写的方法.但是我发现它似乎只是调用超类方法.
我学习了Java并且不确定为什么它在C++中这样做.我尝试使用超类的指针向量重写代码,并将子类的指针强制转换为超类.通过指针访问它然后工作.
理想情况下,我不希望必须将一个指针列表放入向量中,因为那时我必须手动删除每一个(我相信?)以阻止内存泄漏,因为我将使用new创建对象,因此它们会持续通过方法调用将它们添加到矢量中.
有没有更好的方法来做到这一点,还是我坚持使用指针并在不需要父类时在创建的对象上调用delete?优选地,向量将是类X的列表而不是类X的指针列表
我的结构是:
class a { vector vec of class X,
method to create and add an instance of X into vector vec,
method to create and add an instance of Y into vector vec }
class X { talk() }
class Y : public X { talk() }
Run Code Online (Sandbox Code Playgroud)
用于演示我理想的操作的代码,但仅通过调用超类方法显示其中断:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
class A {
public:
virtual void talk() { printf("A\n"); }
};
class B: public A {
public:
void talk() { …Run Code Online (Sandbox Code Playgroud) 关于在类中使用分布,我有两个相关的问题.
在C++中是否存在某种基本分布,以便将分布用作类成员而不知道它将是哪个分布?我不能使用模板(见问题2)
class Foo{
private:
// could by any distribution
std::base_distribution dist_;
};
Run Code Online (Sandbox Code Playgroud)我有另一个类Bar应该有Foo一个私有成员的向量(std::vector<Foo>).问题是如果Foo使用模板,那么就不可能有一个不同模板参数的向量,这正是我想要的.
class Bar {
private:
std::vector<Foo> foo_;
};
Run Code Online (Sandbox Code Playgroud)boost::variant也没有帮助,因为我不知道分布的类型.所以这(例如)在我的情况下是不可能的:
class Bar{
private:
boost::variant<std::normal_distribution<>, std::uniform_real_distribution<> > dists_;
};
Run Code Online (Sandbox Code Playgroud) 我有一个基类A和一个派生类B。
class A {
public:
int x;
virtual int getX() {
return x;
}
};
class B : public A {
public:
int y;
};
Run Code Online (Sandbox Code Playgroud)
虚函数只是为了使其具有多态性。接下来,我声明“”的列表,A但将B“”放入其中:
vector<A> list;
B b1,b2;
b1.y = 2;
b2.x = 10;
list.push_back(b1);
list.push_back(b2);
Run Code Online (Sandbox Code Playgroud)
现在我想遍历向量上的所有元素并访问y成员(只有成员B):
for (auto it = list.begin(); it != list.end(); ++it) {
B &cast = dynamic_cast<B&>(*it);
int value = cast.y;
std::cout << value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
此代码给出了运行时错误。知道我该如何进行投射和访问y吗?
我想知道在 doxygen 中记录以下内容的正确方法是什么。
有一个定义一些验证器的类,例如:
class Validators {
/**
* @fn A
* @brief sees if x is too large.
* @param[in] x the input to validate
* @throws runtime_error when otx is too large.
*/
static void A(int x) {
if (x > 5) {
throw std::runtime_error("x too large");
}
}
};
Run Code Online (Sandbox Code Playgroud)
在如下函数中使用此验证器:
#include "validator.h"
class MyClass {
public:
void setX(int x) {
Validators::A(x);
}
};
Run Code Online (Sandbox Code Playgroud)
我应该如何记录setX()重新抛出由 引发的runtime_error A(),或者我根本不应该记录它?
有谁知道以下编译warning: catching polymorphic type ‘class std::out_of_range’ by value [-Wcatch-value=]警告的含义以及如何纠正它?我从 Stroustrup 的 C++ 4th Edition 字面上复制了这个代码块。谢谢
#include <iostream>
#include <vector>
#include <list>
using std::vector;
using std::list;
using std::cout;
template <typename T>
class Vec : public vector<T> {
public:
using vector<T>::vector; // constructor
T& operator[](int i) { return vector<T>::at(i); }
const T& operator[](int i) const { return vector<T>::at(i); }
};
int main(int argc, char* argv[]) {
vector<int> v0 = {0, 1, 2};
Vec<int> v1 = {0, 1, 2};
cout << …Run Code Online (Sandbox Code Playgroud) 这行不会编译:
Shape shape = (i % 2) ? Circle(5) : Rectangle(5, 5);
Run Code Online (Sandbox Code Playgroud)
(我知道它没用,因为表达式返回的内容将简化为简单Shape,这不是重点).
无法弄清楚为什么它不会编译.我正在创建一个Shape名为的变量shape(我认为此时会创建一个新Shape变量),然后我将此变量赋给表达式的结果.为什么不编译?
错误:
no match for ternary operator
Run Code Online (Sandbox Code Playgroud)
什么是真正奇怪的是,有完全相同的含义较长的代码并编译和运行按预期:
Shape shape;
if (i % 2)
shape = Rectangle(5, 5);
else
shape = Circle(5);
Run Code Online (Sandbox Code Playgroud) 如果我没记错的话,在Java中,我们可以将子类传递给具有超类的函数.代码看起来像这样.
// Assume the classes were already defined, and Apple
// and Pineapple are derived from Fruit.
Fruit apple = new Apple();
Fruit pineapple = new Pineapple();
public void iHaveAPenIHaveAn(Fruit fruit) { ... } // :)
...
public static void main(String[] arg)
{
iHaveAPenIHaveAn(apple); // Uh! Apple-pen.
iHaveAPenIHaveAn(pineapple); // Uh! Pineapple-pen.
}
Run Code Online (Sandbox Code Playgroud)
然而,在C++中,我注意到,从这里,你需要使用基类的引用变量(那是适当的期限?),而不是基类的常规变量.
假设你有两个类:一个基类A和一个A派生类B.
class A { ... };
class B : A { ... };
Run Code Online (Sandbox Code Playgroud)
如果我们有一个neverGonna()接受类A参数的函数,那么为什么函数看起来像这样:
void …Run Code Online (Sandbox Code Playgroud) 当我学习使用异常时,我刚刚想到了一个问题。代码如下。
// exception constructor
#include <iostream> // std::cout
#include <exception> // std::exception
#include <cxxabi.h>
#define PRINT_TYPENAME(f) std::cout << abi::__cxa_demangle(typeid(f).name(), 0, 0, &status) << std::endl;
struct ooops : std::exception
{
const char *what() const noexcept { return "Ooops!\n"; }
};
int main()
{
ooops e;
std::exception *p = &e;
// demangle the typeid
char *realname;
int status;
PRINT_TYPENAME(p);
PRINT_TYPENAME(*p);
try
{
throw e; // throwing copy-constructs: ooops(e)
}
catch (std::exception &ex)
{
std::cout << ex.what();
}
try
{
throw *p; // …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×2
exception ×2
boost ×1
c# ×1
casting ×1
class ×1
derived ×1
distribution ×1
doxygen ×1
expression ×1
gcc ×1
inheritance ×1
java ×1
pointers ×1
polymorphism ×1
superclass ×1
variables ×1
vector ×1