比如说你有:
class Plant {
public:
...public accessors, etc ...
}
Run Code Online (Sandbox Code Playgroud)
然后你有一个特定的植物类:
class Broccoli : public Plant
{
public:
Broccoli(int i);
virtual ~Broccoli();
inline BroccoliSprite* getPlantSprite() { return _plantSprite; };
private:
BroccoliSprite* _plantSprite;
};
Run Code Online (Sandbox Code Playgroud)
在某些时候,Broccoli对象存储在向量中:std::vector<Plant> vPlants;
访问我使用的向量及其元素: inline std::vector<Plant>& getPlants() { return vPlants; };
访问:
for (int f=0; f < _f.getPlants().size(); f++)
{
Plant _p1 = _f.getPlants().at(f);
cocos2d::CCSprite* s = _p1.getPlantSprite();
....
}
Run Code Online (Sandbox Code Playgroud)
我希望能够用getPlantSprite()它来显示给用户,但编译器告诉我:No member named 'getPlantSprite' in 'Plant'
这是真的,但是向量中的对象是类型Plant并且包含一个成员变量,它是我需要的精灵,getter返回它.
我不明白的是如何获得访问权限 getPlantSprite()
我想有一点是我可以改变 …
AoA,我正在制作一个国际象棋的控制台游戏,但我坚持多态,下面是类和函数定义/*旧的部分//基类
class Piece /*Parent class */
{
protected:
Position* pCoord;
std::string color;
char symbol;
public:
Piece(Position* Coord,std::string Color,char symbol);
Position GetCurrentPos();
std::string GetColor();
void SetColor(std::string color);
void Draw();
virtual bool SetPos(Position* newPos){MessageBox(NULL,L"Virtual Running",L"Error",MB_OK); return true;};
virtual ~Piece();
};
/* Inherited classes */
//Child classes
class Pawn: public Piece
{
private:
std::vector<Position>* allowPos;
public:
Pawn(Position* Coord,std::string Color,char symbol);
~Pawn();
std::vector<Position>* GetThreatendFields();
bool isValidMove(Position* newPos);
bool SetPos(Position* newPos);
};
//Child classes
class Bishops: public Piece
{
private:
std::vector<Position>* allowPos;
public:
Bishops(Position* Coord,std::string …Run Code Online (Sandbox Code Playgroud) 我在使用自动存储持续时间声明的对象使用虚函数时遇到问题.这是一个可重现的场景:
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void printClassName() {
std::cout << "A" << std::endl;
}
};
class B : public A {
public:
B() : A() {}
~B() {}
void printClassName() {
std::cout << "B" << std::endl;
}
};
class Test {
private:
A item;
public:
Test() {}
~Test() {}
void setItem(A item) {
this->item = item;
}
A getItem() {
return this->item;
}
};
int main() {
Test t;
B item;
t.setItem(item); …Run Code Online (Sandbox Code Playgroud) 我有一个基类Shape,派生类和Ellipse和Rectangle.
在一个函数中,我有一个变量:
Shape activeShape(black, black, {0,0}, {0,0}, false);
后来在那个函数中:
activeShape = updateShape(isButton, activeShape, true);
updateShape 看起来像这样:
Shape updateShape(int button, Shape active, bool leftClick)
{
switch(button)
{
case 1:
return active;
case 2:
return Line(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
break;
case 3:
return Rectangle(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
break;
case 4:
return FilledRectangle(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
break;
case 5:
return Ellipse(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
break;
case 6:
return FilledEllipse(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
break;
default:
if(leftClick)
{
active.setColor(getEnumColor(button), …Run Code Online (Sandbox Code Playgroud) 我试过这段代码:
#include <iostream>
using namespace std;
class A {
public:
A(){cout<<"A();";}
~A(){cout<<"~A();";}
};
class B : public A {
public:
B(){cout<<"B();";}
~B(){cout<<"~B();";}
};
int main() {
A a =B();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:A(); B();〜B();〜A();〜A();
为什么A的析构函数被调用了2次?
当我尝试将基类转换为派生类时,我收到错误.我想访问我放在组件向量中的派生类.
//基础和派生
class Component
{
public:
Component();
virtual ~Component();
private:
};
class Material:public Component{...};
Run Code Online (Sandbox Code Playgroud)
//在主要
int textureID = gameScene.gameObjects[0].getComponent<Material>()->texture;
Run Code Online (Sandbox Code Playgroud)
//游戏对象
#pragma once
#include <vector>
#include "Component.h"
class GameObject:public Component
{
public:
GameObject();
GameObject(int otherAssetID);
~GameObject();
int assetID;
std::vector<Component> components;
void addComponent(Component otherComponent);
void deleteComponent(Component otherComponent);
template <class T>
T* getComponent() {
for (int i = 0; i < components.size(); i++)
{
if (dynamic_cast<T*>(components[i]) != nullptr)
{
T *test = dynamic_cast<T*>(components[i]);
return test;
}
}
return nullptr;
}
private:
};
Run Code Online (Sandbox Code Playgroud) 我有来自第三方库的两个结构,所以我不能改变它们的布局.但是我有两个我自己编写的结构符合它们的签名,所以我可以为它们添加函数(再次,但不是它们的布局,因此也没有虚函数).结构被称为Foo1和Foo2.两者都有相同的成员调用_b,但是在不同的偏移量.
struct FooBase { };
struct Foo1 : FooBase
{
int _a = 2;
int _b = 1;
};
struct Foo2 : FooBase
{
int _b = 2;
};
struct Wrap
{
Wrap(const FooBase& x) : _x(x) { }
FooBase& _x;
int GetValue() { return /* MISSING */; }
};
Run Code Online (Sandbox Code Playgroud)
我有一个名为的包装类Wrap.我正在寻找一种方法来返回_bfoo类中不使用虚函数的值,因为我不能再改变它们的大小了.
Foo1 f1 = Foo1();
Wrap x = f1;
int a = x.GetValue(); // should return 1
Foo2 …Run Code Online (Sandbox Code Playgroud) 我有一个基类和一个子类:
class Base{
public:
Base(){}
// ...
};
class Sub: public Base{
int param;
public:
Sub(){}
// ...
};
Run Code Online (Sandbox Code Playgroud)
我还有一个需要Base向量的函数,如下所示:
void doSomeThing(vector<Base> vectorOfBases){
// ...
}
Run Code Online (Sandbox Code Playgroud)
我需要这样调用该函数:
vector<Sub> myVectorOfSubs;
doSomeThing(myVectorOfSubs);
Run Code Online (Sandbox Code Playgroud)
编译器告诉我:
没有从向量 < Sub > 到向量 < Base > 的适当转换
那么如何将子类向量传递给需要基类向量的函数呢?
让成员函数返回类型成为您要返回的实际对象的超类的正确方法是什么?
Clang tidy 警告我返回subClassA或subClassB丢弃我对myMethod. 我也没有得到任何输出 - 大概是因为我不小心丢弃了覆盖的myMethod.
#include <iostream>
using namespace std;
class SuperClass {
public :
SuperClass() {
cout << "I'm the superclass" << endl;
};
virtual std::string myMethod();
};
class SubClassA : public SuperClass {
public:
SubClassA() : SuperClass() {
cout << "I'm subclass A" << endl;
}
std::string myMethod() override {
return "A";
}
};
class SubClassB : public SuperClass {
public:
SubClassB() : SuperClass() { …Run Code Online (Sandbox Code Playgroud) 我知道有虚函数,但没有虚变量。没有添加它可能是因为大部分时间不需要它,但我最近遇到了一个问题:
class A
{
public:
int bar;
};
class B : public A
{
public:
float foo;
};
class C : public A
{
public:
double foo;
};
int main()
{
std::vector<A*> v;
v.push_back(new B());
v.push_back(new C());
std::cout << v[1]->bar << v[0]->foo; //error because it will not find 'foo' in A
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以阻止这个错误吗?
c++ ×10
polymorphism ×4
c++11 ×2
inheritance ×2
class ×1
constructor ×1
destructor ×1
dynamic-cast ×1
game-engine ×1
oop ×1
stdvector ×1
vector ×1