- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法让用户输入来引用随机类实例?我的意思是基于已经创建的那些?有点像开关,但没有写出开关内的每个实例?我希望我的代码更具通用性,以便其他人可以在不更改开关或代码的其他部分的情况下向其添加内容。
我知道帖子的组织有点曲折,但我觉得这是在帖子上组织我的想法的最佳方式,所以请多多包涵。在最底部的代码中,我创建了一个指针来更改“test1”实例中的变量。我希望能够让程序能够让用户输入以选择哪个类,然后在那里进行更改。我知道这种语法不起作用,但例如:
class test {
public:
int number;
int letter;
}
test classTest; //to get user input of which class he wants to select
//to change a variable
cin >> classTest; //user inputs choice
test * classPointer; //pointer to variables
cin >> classPointer -> number;
cin >> classPointer -> letter;
//where classTest is actually not a normal instance, just a way to get user
//input to select which class instance variables he'd like to change
这是我的问题开始的背景和这个问题的来源。如果你不关心它/需要它来回答我,你可以跳过它。我正在编写一个 RPG 游戏来练习我在 C++ 中学习的内容,并且我想让我编写的代码相当通用。例如,如果我有一个类创建某些在“城镇”中通用的对象。比如 Prop 店,客栈等等。在战斗系统中你可以选择使用 Prop 。在该项目类别中,虽然应该只列出可用的项目。这没问题,我可以编写大量的 if、开关和 bool 值——但是我不想为战斗系统中的每个项目编写所有这些我想要某种可以为我完成的机制。我相信一些关于如何调用指针的技巧将解决我这里的问题。然而,尽管如此,请不要因为我的背景故事而分心。它只是为了帮助您了解我的思考过程,如果它不能解决这个问题,那就这样吧,无论如何我都需要知道这些信息以供引用。
#include <iostream>
#include <stdio.h>
class test {
public:
int number;
char letter;
void function() {
printf("Number = %d\nLetter = %c", number, letter);
}
test() {}
test(int a, char b) : number(a), letter(b) {}
};
int main()
{
test* pointer;
test test1(1, 'c');
pointer = &test1;
test1.function(); //output initial result
printf("\nEnter a new number: ");
std::cin >> pointer -> number; //change number
printf("Enter a new letter: ");
std::cin >> pointer -> letter; //change letter
test1.function(); //output changed result
return 0;
}
最佳答案
我对代码片段和密集的文本有点迷惑,但你描述的那种问题是相当常见的。
要创建基于某个键的类型的对象,您可能希望将抽象工厂模式作为您的起点 - 在您附近的所有优秀教科书和更有信誉的 wiki 中都可以找到。如果它看起来对您的目的来说过于夸张,请不要推迟 - 与所有模式一样,您可以将其削减以满足您的需要。
本质上,对于您要创建的每种类型的对象,您都需要一个知道如何制作它的工厂。制作此相关对象集合所需的所有工厂应共享相同的接口(interface)。
要选择正确的工厂来创建请求的对象类,您需要持有键映射,即工厂的字符串。该 map 取代了您可能想使用的 if-else/开关。如果您想尽量减少打字并混淆您的敌人和 friend ,可以使用 Singleton 来根据键注册工厂,但请先阅读所有健康警告:
// Base class for alldwellings in your townclass Dwelling{public: virtual ~Dwelling() {} virtual Dwelling(int number, char letter); virtual Enter() = 0; virtual Leave() = 0; // ... Whatever other common things you can do in a town dwelling */ };// A type of dwelling - as many of these as you likeclass Shop : public Dwelling{ virtual Shop(int number, char letter) : Dwelling (number, letter) { /* stuff */} virtual Enter(); virtual Leave();};// Base factory class for making a type of dwellingclass AbstractDwellingFactory{public: virtual DwellingFactory() {} virtual Dwelling * Create(int number, char letter) = 0;};// Collection and registrar of all dwelling factories - a Meyer's Singleton for convenienceclass DwellingFactory{public: static DwellingFactory & Instance() { static DwellingFactory theInstance; return theInstance; } // Create a dwelling from the given type and other parameters - consider smart pointers rather than the raw pointer shown here Dwelling * Create(std::string const & dwelling dwellingType, int number, char letter) { // Assuming we have a matching factory, make us a dwelling. // Obviously some error checking is required here in the real world return myAbstractDwellingFactoryMap[dwellingType].Create(number, letter); } void Register(std::strinf const & dwellingType, AbstractDwellingFactory const & fact) { myAbstractDwellingFactory[dwellingType] = &fact; }private: DwellingFactory() {} // The actual key to type mapping std::map myAbstractDwellingFactoryMap;};// A generic concrete factorytemplate <typename DwellingType>class ConcreteDwellingFactory : public AbstractDwellingFactory{public: ConcreteDwellingFactory(std::string const & dwellingType) { DwellingFactory::Instance().Register(dwellingType, *this); } // Factory function - note we can return a pointer to a derived type here - look up 'co variant return types' DwellingType * Create(int number, char letter) { return new DwellingType; }};// A factory instance for each dwelling type you want to supportstatic ConcreteDwellingFactory<Shop> shopFactory("Shop");void SomeFunction(){ std::string dwellingType; int number; char letter; std::cin >> dwellingType >> number >> letter; Dwelling *requestedDwelling(DwellingFactory::Instance().Create(dwellingType, number, letter)); requestedDwelling->Enter(); // et cetera}
关于c++ - 是否可以引用随机类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393275/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!