我正在尝试简化我的代码,让它更好、更容易地工作:
这意味着深入研究向量和unique_ptr
,我已经读到了很多关于它们的好东西。然而,它们对我来说是全新的。我在这两个方面都读了几页,但有很多东西需要我去理解。
我目前正在做的是以传统的方式创建抽象类的对象:
VirtualBaseClass* foo1= new DerviedClass1;
VirtualBaseClass* foo2= new DerviedClass2;
VirtualBaseClass* foo3= new DerviedClass3;
但由于我有3个--而且很可能会有更多--我想让它们之间的切换变得更容易,因为我将比较每个程序运行的对象的任意组合。
目前,为了切换,我只需重命名要为其实例化对象的DerviedClass,这样就不必使用foo3重命名每个foo1。
VirtualBaseClass* Generic1 = new DerviedClass3;
VirtualBaseClass* Generic2 = new DerviedClass1;
但最终,我希望用户告诉程序要比较哪两个对象。因此,一个好的起点似乎是让它成为VirtualBaseClass的一个数组,但从研究来看,删除这些数组似乎很痛苦,所以人们建议使用智能指针和向量。
所以我试着把两者都用上。对于唯一的指针,我这样做
unique_ptr<vBaseClass*> foo1(DerviedClass1);
unique_ptr<vBaseClass*> foo2(DerviedClass2);
unique_ptr<vBaseClass*> geneic1 = move(foo1);
然而,从我读到的内容来看,我应该做的是
unique_ptr<vBaseClass*> foo1(new DerviedClass1);
但是new给出了类型投机符的错误,但由于它在没有它的情况下工作,所以我认为它没有什么问题。使用move(foo1)时,我得到了一个错误,例如重载函数匹配,以及在编译时出现的一大堆其他错误,例如
unique_ptr<vBaseClass*> champ1 = move(foo1);
错误控制器:‘C3867::foo1’:函数调用缺少参数列表;使用'&Controller::foo1‘创建指向成员错误的指针C2780:'_OutTy *std::move(_InIt,_InIt,_OutTy (&)_OutSize)’:需要提供3个参数-1
所有这些都在我的Controller.h文件btw中完成。我非常需要指导。我不知道我所做的是不是必要的,我需要对此使用向量吗?我又该如何开始呢?有没有更好的方法来做这件事?我怎样才能让用户告诉程序使用哪个对象呢?对于数组,它将为foo1输入0,或者为foo2输入1,但使用向量?有没有更好的方法?
我的acutal代码
#pragma once
#include "stdafx.h"
#include "Skarner.h"
#include "MasterYi.h"
#include "Riven.h"
using namespace std;
class Controller
{
public:
Controller();
~Controller();
double PCFreq;
__int64 CounterStart;
int CounterCheck;
ofstream out;
Champion* skarner = new Skarner;//old way of doing it
//Champion* yi = new MasterYi;//old way of doing it
//Champion* riven = new Riven;//old way of doing it
//Champion** champions = new Champion*[200];
//Champion[0] = new Skarner();
//unique_ptr<Champion> skarner(Skarner);
unique_ptr<Champion> yi(new MasterYi);// doesn't work new error
unique_ptr<Champion*> riven(Riven); //works with or without *
unique_ptr<Champion*> champ1 = move(riven)//error with move
vector<unique_ptr<Champion>> pChampions;//vector of pointers to champion
//unique_ptr<Champion> champ2;
//Champion *champ1 = dynamic_cast<Champion*>(yi);
//Champion *champ2 = dynamic_cast<Champion*>(skarner);//not sure what the signficance of this is
//Leaving some methods out
};
哇,显然你不能只在cpp文件中使用头文件中的"new“。但是,我仍然不确定如何很好地利用它,因为我已经在controller.cpp中声明了它。我真的想把它作为一个成员变量/实例变量。
试图做到这一点。在controller.h er.h中
shared_ptr<Champion> yi;
shared_ptr<Champion> riven;
shared_ptr<Champion> skarner;
shared_ptr<Champion> champ1;
shared_ptr<Champion> champ2;
并在.cpp中定义它们
Controller::Controller()
{
PCFreq = 0.0;
CounterStart = 0;
out.open("finalStats.txt");
CounterCheck = 0;
yi = shared_ptr<Champion> (new MasterYi);
riven = shared_ptr<Champion>(new Riven);
skarner = shared_ptr<Champion>(new Skarner);
champ1 = move(yi);
champ2 = move(riven);
}
上面的代码现在似乎可以工作了,但我看不到任何直接的好处。
发布于 2014-05-06 23:14:32
解释
你有一个*
to much:
unique_ptr<vBaseClass> foo1(new DerivedClass1);
应该通过分配一个具有动态存储持续时间的新DerivedClass1
并将指向它的指针存储在foo1
中来完成此任务。
提醒一下,只需大声阅读类型:foo1
的类型为“指向vBaseClass
的唯一指针”。
对于评论中的人群
下面显示了原始指针和唯一指针之间的用法差异:
{
int* a = new int(42);
unique_ptr<int> b(new int(42));
std::cout << *a << ", " << *b << "\n";
delete a;
}
没有更多的不同。任何进一步的问题都与一个不同的问题有关,如果没有进一步的信息,很难准确定位这个问题。
此外,unique_ptr<Champion*> riven(Riven);
是一个名为riven
的函数的函数声明,它返回一个unique_ptr<Champion*>
并接受Riven
类型的单个参数。这没有错误的原因是因为它根本不会做您认为它所做的事情。
最后,绝对没有什么能让头文件有任何特别之处。实际上,C++在解析之前执行文本替换,因此实际的解析器甚至不再知道代码来自哪里!
业力示范
代码:
struct champ { virtual std::string whoami() = 0; };
struct karma : champ { std::string whoami() override { return "karma"; } };
int main() {
champ* a = new karma;
std::unique_ptr<champ> b(new karma);
std::cout << a->whoami() << ", " << b->whoami() << "\n";
}
结果:
karma, karma
发布于 2014-05-07 00:00:49
对于编译器来说,unique_ptr<Champion> yi(new MasterYi);// doesn't work new error
看起来像是一个函数声明,而new
在该上下文中是无效的。
unique_ptr<Champion*> riven(Riven); //works with or without *
看起来也像一个函数声明,不管有没有*
都是有效的。
你不能把一个函数move
成一个unique_ptr
。
我很难理解你的问题,但也许你的意思是这样的:
unique_ptr<Champion> yi = new MasterYi;
unique_ptr<Champion> riven = new Riven;
std::vector<std::unique_ptr<Champion> > pChampions = { new Skarner };
https://stackoverflow.com/questions/23498160
复制相似问题