简体   繁体   English

从包含 class 指针的 Vector 访问对象

[英]Accessing objects from Vector containing class pointer

As declared above, I wish to access an object from the vector which contains an abstract class.如上所述,我希望从包含抽象 class 的向量中访问 object。 I'm fairly new.我还算新。

Account* c = new Current;
vtrAcc.push_back(c("test", 0));

Where "test" is a name and 0 is account ID.其中"test"是一个名称, 0是帐户 ID。

Writing this, throws an error:写这个,抛出一个错误:

expression preceding parentheses of apparent call must have (pointer-to-) function C/C++(109)明显调用括号前的表达式必须有(指向-)function C/C++(109)

We're required to create multiple instances of accounts, thus I use vectors, however, I fail to understand (pointers are hard) how to add parameters to initialize my constructor?我们需要创建多个帐户实例,因此我使用向量,但是,我不明白(指针很难)如何添加参数来初始化我的构造函数? Does Account* c = new Current;是否Account* c = new Current; call its' default constructor?调用它的默认构造函数?

I expect this to work for now just to understand the thought behind it.我希望这现在可以工作,只是为了了解它背后的想法。 Looking throughout StackOverflow I did find this TOPIC from 10 years ago, I understand it.纵观整个 StackOverflow,我确实找到了 10 年前的这个话题,我明白了。 But how do I adapt that with pointers?但是我如何用指针来适应呢?

try尝试

Account* c = new Current("test", 0);
vtrAcc.push_back(c);

if your vtrAcc is a pointer to account type vector<Account*>vtrAcc;如果您的 vtrAcc 是指向帐户类型vector<Account*>vtrAcc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM