c++ - 将 vector<class> myvector 传递给构造函数并访问它

标签 c++ oop

抱歉,如果已经发布了关于此的类似问题,但我不知道如何正确表达这个问题。如何使用 cout << 访问“William”和“Shakespeare”,这些 cout << 被传递到 Author 对象 shakespeare,然后被传递到作者 vector ,然后又被传递到 Book 对象 play?

class Author{} //has private variables first_name and last_name and getter 
               //functions in public.

class Book{
private:
    vector<Author> author_mem;
public:
    Book(const vector<Author>& author_fn):author_mem(author_fn){}
    vector<Author> get_author_list() const {return author_mem;}
}

int main(){
    const Author shakespeare("William","Shakespeare");
    const vector<Author> authors = {shakespeare};
    const Book play(authors);
    //initiial idea was to have something like 
    //cout << play.get_author_list.at(0)

}

最佳答案

我不知道您如何为您的 Author 类命名访问器,但假设它是 GetFirstNameGetLastName

这应该有效:

cout << play.get_author_list().at(0).GetFirstName();
cout << play.get_author_list().at(0).GetLastName();

关于c++ - 将 vector<class> myvector 传递给构造函数并访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30609145/

相关文章:

C++ 字典 trie 实现

c++ - 为什么 g++ 会生成多个(弱)相似符号?

c++ - 在取消引用的类中取消指向仿函数的指针

php - MySQLi OOP 绑定(bind)参数错误

java - 为什么我无法在 Java 中实例化扩展类的实例?

c++ - 多线程黑客

c++ - 尝试在方法中包装 glGenVertexArrays 调用时出现段错误

c++ - 子字符串输出不符合预期?

c++ - 基于c++底层复合模式的图形编辑器开源项目示例

design-patterns - 这是哪种设计模式?