我正在编写一个以RPG元素迷宫为中心的简单2D游戏.它主要用于学习目的,用于练习类设计,图论算法,数据结构使用和使用2D图形.
该计划的简要概述:
游戏本身是基于平铺的.它当前生成并绘制迷宫到屏幕,允许玩家移动和墙壁的碰撞检测; 此外,它可以处理更大的迷宫的屏幕滚动.
无论如何,现在我正在构建一个处理在地图周围放置对象的对象.列表中的第一个是金币,然后是心和物品.我认为这是练习继承和多态的好时机,但我还没有接受过这种设计的正式培训.这是头文件:
#ifndef MAP_OBJECT_H
#define MAP_OBJECT_H
#include "Common.h"
#include "Utility Functions.h"
const int TILE_SIZE = 80;
class MapObject
{
public:
MapObject(unsigned int nClips, unsigned int r, unsigned int c, unsigned int cSize) :
sheet(0), clips(0), row(r), col(c), numClips(nClips), clipSize(cSize)
{
//For those of you who aren't familiar with sprite sheet usage, basically this
// handles which part of the sprite sheet your on, so it will draw the correct sprite to the screen
if(numClips > 0) clips …Run Code Online (Sandbox Code Playgroud) 在以下代码中:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << " A constructor \n";
sum(2,4);
}
virtual int sum(int a, int b){
cout << "Base sum \n";
return a + b;
}
};
class B : public A {
public:
B() : A() {
cout << " B constructor \n";
}
int sum(int a, int b){
cout << "Overloaded sum \n";
return (a + b) * 10;
}
};
int main(){
A* a = …Run Code Online (Sandbox Code Playgroud) 我看到当我调用类的实例方法时,C#编译器会发出callvirt调用该方法的
指令,为什么会这样呢?
这是否意味着所有实例方法都被virtual methods编译器视为什么,这是什么谜?
我想知道删除是如何工作的?在主要功能我删除了cfact object.但仍然是cfact->Hello()工程,而不是抛出错误.在我发现删除时发现的调试时,cfact释放内存.一旦factory* c2fact = newfun.Newfun("c2_fact");行执行cfact获得一些内存位置.
怎么样?请帮助我理解这个概念..
class factory{
public:
virtual void Hello() = 0;
};
class c_fact: public factory
{
public:
void Hello(){
cout << "class c_fact: public factory"<<endl;
}
};
class c2_fact: public factory
{
public:
void Hello(){
cout << "class c2_fact: public factory"<<endl;
}
};
class callFun{
public:
virtual factory* Newfun(string data)
{
if(data == "c_fact")
{return new c_fact;}
else
{return new c2_fact;}
}
};
class newFun:public callFun{ …Run Code Online (Sandbox Code Playgroud) c++ virtual-functions new-operator factory-pattern delete-operator
我在C#polymorphism中选择了这段代码形式的MSDN资源.
public class A
{
public virtual void DoWork() { }
}
public class B : A
{
public override void DoWork() { }
}
public class C : B
{
public override void DoWork()
{
// Call DoWork on B to get B's behavior:
base.DoWork();
// DoWork behavior specific to C goes here:
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
它说C类覆盖B的DoWork(),但是B类的DoWork()方法不是虚拟的.由于C继承了B的所有方法和数据,它是否也继承了A的虚方法(在B中可用,因为A是B的基类)?
如果B没有提供DoWork()的实现,那么C会直接访问A的虚方法副本来覆盖它吗?
此外,当C继承自B时,它会获得A的成员的单独副本或B的副本.我猜对于A的成员的单独副本,必须单独继承A,如
public class C : A, B
Run Code Online (Sandbox Code Playgroud)
纠正我,如果错了.
编辑:
public class A
{
public virtual void DoWork()
{
Console.WriteLine("In Class …Run Code Online (Sandbox Code Playgroud) 我期待很容易找到那个答案,但搜索周围什么都没有.
考虑以下:
#include <iostream>
class Base
{
public:
virtual void whoAmI()
{
std::cout << "\nI'm base\n" ;
}
};
class Derived : public Base
{
};
class SecondDerivative : public Derived
{
public:
virtual void whoAmI()
{
std::cout << "\nI'm Second derivative\n" ;
}
};
int main()
{
SecondDerivative SD;
Base* b = &SD;
b->whoAmI();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道在使用指针时应该调用运行时类型的虚函数.但在此示例中,直接派生类不实现虚函数,但在使用原始基类指针时会调用其派生类的函数.
使用g ++构建并运行show:
I'm Second derivative
我想知道标准中是否存在关于此类案例的令人信服的规则,还是依赖于平台?
虚拟/动态的
// declare in Child Class
constructor Create; virtual;
constructor TChildClass.Create;
begin
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
有覆盖的那个.
// declare in Child Class
constructor Create; override;
constructor TChildClass.Create;
begin
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
一无所有
// declare in Child Class
constructor Create;
constructor TChildClass.Create;
begin
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
这些是一回事吗?看起来很混乱.
有人可以解释为什么i->value()和(i + 1)->value()打印1和3不是1和4喜欢 x[0]->value() << x[1]->value()
#include <iostream>
#include <vector>
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int value() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class B
: public A
{
public:
B(int n = 0) : A(n) { }
public:
virtual int value() const { return m_n + 1; }
};
int main()
{
const A a(1); //a.m_n=1
const B b(3); …Run Code Online (Sandbox Code Playgroud) 我读到了虚拟功能,但我无法清除这个概念.在下面提到的例子中.我们创建一个基指针并首先分配基础对象,调用函数是基类,然后分配派生对象并调用其函数.既然我们已经提到过要分配哪些对象,那么编译时编译器不知道要调用哪个对象函数?我不明白为什么这个决定会延迟到运行时间.我在这里遗漏了什么.
#include <iostream>
using std::cout;
using std::endl;
// Virtual function selection
class Base
{
public:
virtual void print() const
{
cout << "Inside Base" << endl;
}
};
class Derived : public Base
{
public:
// virtual as well
void print() const
{
cout << "Inside Derived" << endl;
}
};
int main()
{
Base b;
Derived f;
Base* pb = &b; // points at a Base object
pb->print(); // call Base::print()
pb = &f; // points at Derived object …Run Code Online (Sandbox Code Playgroud) 我下面这个教程,试图了解virtual table背后的整个过程pointer和virtual functions in C++.
不确定,当我有这样的代码:
D1 d1;
Base *dPtr = &d1;
dPtr->function1();
Run Code Online (Sandbox Code Playgroud)
为什么我需要所有这些virtual table管理?为什么编译器根本不分配内存地址d1(或基数,如果没有)覆盖virtual function?
我的意思是:如果它需要D1 functon1()地址或Base functon1()地址,它可以在编译时详细说明.它当时知道.为什么以后在运行时看到会浪费时间和资源virtual tables?
我想念这一点.花哨的例子?
c++ ×7
polymorphism ×3
c# ×2
constructor ×2
delphi ×1
inheritance ×1
methods ×1
new-operator ×1
oop ×1
sdl ×1
vector ×1
vtable ×1