我很难让派生类访问基类中定义的函数。基类头文件名为called .h,它是:
class Particle {
protected:
vector<double> x_,y_,z_;
// lots of other protected members that are not relevant to the question
public:
// constructors and other functions not relevant to question are omitted
virtu
根据C++标准,在构造X的所有基之前直接调用X的成员函数( in )会导致未定义的行为(§11.9.3初始化基和成员class.base.init/16)。它们提供了以下例子:
class A {
public:
A(int);
};
class B : public A {
int j;
public:
int f();
B() : A(f()), // undefined behavior: calls member function but base A not yet initialized
j(f()) // well-defined: bas
在下面的示例中,为什么我不能调用car.getFuelConsumption
class Road {
public:
double length(); // km
int speed(); // km per hour ;
};
class Car {
protected:
virtual double getFuelConsumption(int speed_in_km) = 0;
};
class Tank: public Car{
public:
double getFuelConsumption(int speed_in_km) {
我想知道是否可以将类作为它的基类型。下面我做了一个小例子,描述了我指的情况。
我使用了示例中遗漏的Vector2类,但它只是由x和y类型的float值组成。
我试图使名为FindEntitiesWithPosition的方法与类Person和Vechicle一起工作,但我得到了错误E0312 no suitable user-defiend conversion from "std::vector<Person, std::allocator<Person>>" to "const std::vector<Entity, std::allo
我的任务是从二叉树派生的二进制搜索树,在我的驱动程序中,这就是我创建BST对象的方式。但我有点困惑,因为我知道必须有一个构造函数,但在我的赋值中没有哪个地方真正调用了派生类的构造函数。
int main() {
int x = 0;
int n = 0;
int len = 0;
int total = 0;
int seed = 0;
bool y;
cin >> n;
vector<int> v;
binSTree<int> t;
我很难使用这些“vector<int> v;”和“bin
我有一个类似于的情况。我已经在一个不太复杂的环境中复制了代码,以演示我所困惑的东西。
我想知道C是如何执行(而不是访问) B1::method和B2::method的,然后它又执行继承的方法。
我认为这是正常工作的唯一方式,因为父类是将函数调用传播到子类的一个类,所以它直接访问Bx的vtable,而不是通过C。
在任何一种情况下,它是安全的,还是未定义的行为,什么陷阱等;什么可能是更好的方法来做到这一点。
#include <iostream>
#include <vector>
class A {
static std::vector<A*> l
一般而言,模板参数可以是抽象类,如下面的程序所示。但似乎排序中的比较函数器不能是抽象的。至少以下代码不能在VC++ 11和Oracle Studio12上编译。
#include <vector>
#include <algorithm>
class Functor
{
public:
virtual bool operator()(int a, int b) const = 0;
};
class MyFunctor: public Functor
{
public:
virtual bool operator()(int a, int b)
我的程序不会调用基类的虚拟析构函数。我在上找到了这个方法,所以我不知道出了什么问题。能请人帮忙吗。
#include <iostream>
#include <vector>
using namespace std;
class Number{
private:
int *arr;
int n;
public:
Number(int *a, int i):
arr(a),n(i){}
virtual vector<int> operator()()=0
我对C++编程非常陌生,所以我想编写一些简单的代码来适应语法。我暂时故意忽略了指点和参考。
在编码过程中,我正在练习继承,我想创建一个类手,它代表一只牌手。基类有一个名为update()的函数,用于在构造时初始化“总计”和“符号”属性。此外,还可以使用add()函数向手添加卡片,该函数向手添加卡片,并触发update()以适当地更新属性。
#include<vector>
class Hand
{
public:
std::vector<int> cards;
int total;
std::string notation; // Abbrev
乍一看,using用于导入特定的函数(例如using std::cout到作用域)。但是这个using实际上将基类的所有构造函数导入到派生类中。这个using声明的场景背后到底是什么?
template< typename T >
class Vec : public std::vector< T >
{
public:
using std::vector<T>::vector; // ?
//...
};
我目前正在做一个C++项目,在这个项目中我有一个抽象接口,稍后会实现该接口。该接口还有一个实现的方法,我的实现没有覆盖它。我的问题是,当使用我的实现时,编译器(MSVC)看不到接口方法。是什么导致了这一问题,我如何解决它?
代码来了。
#include <string>
#include <vector>
using std::string;
class A
{
public:
string name;
};
class interface
{
public:
virtual int num_foo() = 0;
virtual A
我正在做一个任务,我需要使用模板对向量进行排序,这样我就可以将不同的数据类型传递给我的类,并且不使用std::sort对它们进行排序。所以到目前为止,我非常困惑,我真的不知道如何使用模板来接受来自main()的输入。PS。这是为了做家庭作业,这就是为什么我不使用数组和排序函数。
到目前为止,这是我的代码。
using namespace std;
template <class T>
class SortableVector
{
private:
T a =0;
public:
SortableVector(); // constructor
我试图理解这个pdf: (第61-62页)中类的类定义。下面是代码中我有问题的部分。以下是头文件:
#ifndef _TERM_STRUCTURE_CLASS_INTERPOLATED_
#define _TERM_STRUCTURE_CLASS_INTERPOLATED_
#include "term_structure_class.h"
#include <vector>
using namespace std;
class term_structure_class_interpolated : public term_structure_class {
p
我在强制对我自己的对象进行数据类型更改时遇到了问题。我有一个基类,比如A,还有两个从A派生的类,分别称为B和C。我将对象B和C传递给一个函数,该函数检查它是哪种类型的对象(B或C)。下面是一些示例代码和我的问题的问题:
enum ClassType {"B", "C"};
class A {
protected:
m_Type;
public:
ClassType Type() { return m_Type}
...
...
};
class B : public A {
otherMemberFun
是否可以使用指向基类的指针访问派生类的成员?
// Example program
#include <iostream>
#include <vector>
#include <memory>
#include <string>
class A {
public:
std::string x = "this is the wrong x\n";
};
template <class T>
class B : public A {
public:
T x;
};
i
我不知道该怎么描述我的问题。我想知道是否可以在基类中编写函数,这对于派生类来说是一样的,但是传递不同的参数。也许在我的代码中看到比描述它更容易:
class Employee
{
public:
std::string name, profession;
std::string current_task = "NONE";
int id, age;
// checks if the input by user a new task belongs to duties list
// for the class; if it does t