相关疑难解决方法(0)

在C++中输入Casting Issue

从基类到派生类型转换我遇到了问题(我正在施法,因为我确定该对象是那种确切的类型).这是我的代码(简化):

class MyCollection 
{
public:
 Element* Get(int i) {
  return elements[i]; 
 }
 void Add(Element* element) {
   //finding i
   elements[i] = element;
 }
private:
 Element* elements[100]; 
}

class Element {
 public:
  int ID; 
}

class SpecialElement : public Element 
{
public:
 SpecialElement(char* name) { 
   this-> Name = name; 
 }
 char* GetName() { return Name; }
private:
 char* Name; 
}
Run Code Online (Sandbox Code Playgroud)

现在,当我加入SpecialElement的MyCollection的对象,当我把断点添加的时刻,投我的Add方法的参数在立即窗口和调用GetName方法还给我名字的对象,但是当我做这样的事情:

void main() {
 MyCollection coll = new MyCollection();
 coll.Add(new SpecialElement("MyName"));
 SpecialElement* foundElement = (SpecialElement*)coll->Get(0); 
 foundElement->GetName(); //Error
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会这样?是不是ObjectElement类型的创建对象?

c++ casting

-2
推荐指数
2
解决办法
139
查看次数

如何使用共享基类为派生类重载operator <<?

我试图operator<<在几个子类中重载.我有一个名为Question的超类,它有一个枚举值类型和一个字符串问题.该类的子类是TextQuestion,ChoiceQuestion,BoolQuestion和ScaleQuestion.TextQuestion没有其他数据字段.ChoiceQuestion有一个字符串向量,用于存储多项选择的可能性.BoolQuestion没有其他数据字段.ScaleQuestion有两个int值,low_high_,用于比例.

class Question {
public:
    enum Type{TEXT, CHOICE, BOOL, SCALE};
    Question():
        type_(), question_() {}
    Question(Type type, std::string question):
        type_(type), question_(question) {}
    friend std::ostream& operator<<(std::ostream& out, const Question& q);
    virtual void print(std::ostream& out) const;
    virtual ~Question();
    private:
    Type type_;
    std::string question_;
};

class TextQuestion: public Question {
public:
    TextQuestion():
        Question() {}
    TextQuestion(Type type, std::string question):
        Question(type, question) {}
    void print(std::ostream& out) …
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance operator-overloading subclass

-2
推荐指数
1
解决办法
2061
查看次数

从函数的结果中分配std :: shared_ptr的一个衬里是什么?

有一个更惯用的方法来做到这一点:

Potato SpecialPotato();
std::shared_ptr<Potato> givePotato()
{
    std::shared_ptr<Potato> ret;
    *ret = SpecialPotato();
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
70
查看次数

智能指针有真正的用例吗?

我正在阅读 C++ 中的智能指针,我很惊讶超过 99% 的给定示例实际上是相当糟糕的示例,因为在这些情况下可以避免动态分配。我同意仅在 STL 容器不起作用的上下文中使用智能指针。例如,在动态数组 ( std::vector) 中,性能很重要,因此最好拥有经过良好测试的代码而不使用任何智能指针。

这里我认为是一个不好的例子,因为在这种情况下unique_ptr不是解决方案,但堆栈分配将是正确的方法。

MyObject* ptr = new MyObject(); 
ptr->DoSomething(); 
delete ptr; 
Run Code Online (Sandbox Code Playgroud)

那么什么是 C++ 中智能指针的好例子或用途呢?

换种说法是什么设计模式需要将指针的所有权转移到另一个对象?

c++ pointers smart-pointers

-2
推荐指数
1
解决办法
427
查看次数

为什么没有调用子类的虚函数

我有一个基类和派生类:

plugin.h

#ifndef PLUGIN_H
#define PLUGIN_H

// plugin states
#define PLUGIN_IDLE 0

class Plugin {
public:
  Plugin();
  ~Plugin();

  virtual void loop();
};

#endif
Run Code Online (Sandbox Code Playgroud)

plugin.cpp

#include <Arduino.h>
#include "plugin.h"

Plugin::Plugin() {
}

Plugin::~Plugin(){
}

void Plugin::loop(){
  Serial.println("Plugin::loop()");
}
Run Code Online (Sandbox Code Playgroud)

派生类

class OneWirePlugin : public Plugin {
public:
  OneWirePlugin(byte pin);
  void loop();
};

OneWirePlugin::OneWirePlugin(byte pin) {
}

void OneWirePlugin::loop() {
  Serial.println("OneWirePlugin::loop()");
}
Run Code Online (Sandbox Code Playgroud)

我期待调用派生实例的loop()方法将执行OneWirePlugin::loop().

但是,这只发生在我在派生类上下文中调用它时:

Plugin p = Plugin();
Plugin o = OneWirePlugin(ONEWIRE_PIN);
OneWirePlugin q = OneWirePlugin(ONEWIRE_PIN);
p.loop(); …
Run Code Online (Sandbox Code Playgroud)

c++ arduino

-4
推荐指数
1
解决办法
1411
查看次数

c ++虚函数总是必须是const?

我只是想知道所有虚函数是否必须是const?

我遇到了一些问题,因为当我打算将它们打印出来时,该区域总是为我的方块返回0.如果有人能够启发我,我将不胜感激.

shapetwod.h

class ShapeTwoD
{
    protected:
        string name, warpSpace;
        bool containsWarpSpace;
    public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);

        //accessors/set function
        void setName(string);

        //mutator/get function
        string getName();



        //methods
        virtual double computeArea();
        virtual void view();
};
Run Code Online (Sandbox Code Playgroud)

shapetwod.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
}

ShapeTwoD::ShapeTwoD(string ShapeName)
{
    name = ShapeName;
}

void ShapeTwoD::setName(string shapeName)
{
    name=shapeName;
}

string ShapeTwoD::getName()
{
    return name;
}

double ShapeTwoD::computeArea()
{
    return 0;
}

void ShapeTwoD::view()
{
    cout << "Area is: " << endl;
}
Run Code Online (Sandbox Code Playgroud)

square.h

class Square:public …
Run Code Online (Sandbox Code Playgroud)

c++ virtual const

-6
推荐指数
1
解决办法
338
查看次数