小编Sim*_*les的帖子

C++重写方法未被调用

Shape.h

namespace Graphics {
    class Shape {
    public:
        virtual void Render(Point point) {};
    };
}
Run Code Online (Sandbox Code Playgroud)

Rect.h

namespace Graphics {
    class Rect : public Shape {
    public:
        Rect(float x, float y);
        Rect();
        void setSize(float x, float y);
        virtual void Render(Point point);

    private:
        float sizeX;
        float sizeY;
    };
}

struct ShapePointPair {
    Shape shape;
    Point location;
};
Run Code Online (Sandbox Code Playgroud)

像这样使用:

std::vector<Graphics::ShapePointPair> theShapes = theSurface.getList();

for(int i = 0; i < theShapes.size(); i++) {
    theShapes[i].shape.Render(theShapes[i].location);
}
Run Code Online (Sandbox Code Playgroud)

这段代码最终会调用Shape::Render而不是Rect::Render

我假设这是因为它正在施放Rect到a Shape,但我不知道如何阻止它这样做.我试图通过重写 …

c++ virtual inheritance overriding

10
推荐指数
1
解决办法
1万
查看次数

ProtoBuf-Net ProtoInclude 泛型类型子类

我在使用从泛型类继承的对象的子类的 ProtoBuf-Net 时遇到了一些问题。

我的继承树看起来像这样:

Node
    SomeNodeType
    SomeOtherType
    ResourceNode<T>
        ShipResource : ResourceNode<Ship>
        SomeResource : ResourceNode<SomeType>
Run Code Online (Sandbox Code Playgroud)

我一直在所有普通类型的基本 Node 类型上使用 ProtoInclude。

使用 protobuf-net 实现这种层次结构的最佳方法是什么?我试过只包含所有内容,但我得到的错误似乎源于 protobuf 试图将对象反序列化为它的父对象之一。

protobuf-net

5
推荐指数
1
解决办法
4530
查看次数

C++ c_str不返回整个字符串

我已经尝试了以下代码与正常的ifstreams和当前的boost:iostream我正在使用,两者都有相同的结果.

它旨在将文件从physfs加载到内存中,然后将其传递给处理程序进行处理(例如图像,音频或数据).目前,当调用c_str时,它只返回文件的一小部分.

        PhysFS::FileStream file("Resources/test.png" , PhysFS::OM_READ);

    if(file.is_open()) {

        String* theFile;

        theFile = new String((std::istreambuf_iterator<char>(file)), 
        std::istreambuf_iterator<char>());

        String::iterator it;
        for ( it=theFile->begin() ; it < theFile->end(); it++ ) {
            std::cout << *it; 
        } // Outputs the entire file

        std::cout << theFile->c_str(); // Outputs only the first few lines

    }
Run Code Online (Sandbox Code Playgroud)

迭代器循环按预期输出整个png文件,但c_str调用仅返回前几个字符(\ _ 211PNG).

我一直在尝试使用此代码的变体很长一段时间没有成功.有任何想法吗?

c++ string c-str

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

标签 统计

c++ ×2

c-str ×1

inheritance ×1

overriding ×1

protobuf-net ×1

string ×1

virtual ×1