C++ C++;-取消引用作为数组元素的指针?
我有一个Deck类的对象,它包含一个指向另一个类PlayingCard对象的指针的动态分配数组。我正在尝试重载C++ C++;-取消引用作为数组元素的指针?,c++,arrays,class,pointers,dereference,C++,Arrays,Class,Pointers,Dereference,我有一个Deck类的对象,它包含一个指向另一个类PlayingCard对象的指针的动态分配数组。我正在尝试重载*pCards[I],(*pCards[I])和*(pCards[I])都在解除对对象的引用。在程序的另一部分中,可能在Deck的实现中出现了一些其他错误 ostream& operator<< (ostream& out, const Deck& d) { PlayingCard** pCards = d.getPlayingCards();
*pCards[I]
,(*pCards[I])
和*(pCards[I])
都在解除对对象的引用。在程序的另一部分中,可能在Deck
的实现中出现了一些其他错误
ostream& operator<< (ostream& out, const Deck& d)
{
PlayingCard** pCards = d.getPlayingCards();
for(int i = 0; i < d.getTotalCards(); ++i)
out << (*(pCards[i])) << endl;
return out;
}
哎呀,请阅读您的评论:
// the << operator is also overloaded in the PlayingCard class to take PlayingCard objects as arguments.
// the overload in the PlayingCard class definitely works.
//问题可能出在getPlayingCards中……您是否考虑过使用std::vector或std::set之类的iterable容器。至少这样,您就可以有一个getter指向集合的开始迭代器,然后进行迭代——至少这样您就不必担心指针了(当然,这会对PlayingCard的复制构造函数的可用性做出某些假设)。。。更好的是,您还可以使用访问者设计模式来遍历PlayingCard对象的集合,将遍历逻辑保留在Deck对象中……这是一个非常好的问题,我不确定。我有电话和接线员
ostream& operator<< (ostream& out, const Deck& d)
{
PlayingCard** pCards = d.getPlayingCards();
for(int i = 0; i < d.getTotalCards(); ++i)
out << (*(pCards[i])) << endl;
return out;
}
ostream& operator<< (ostream& out, const PlayingCard& d);
// the << operator is also overloaded in the PlayingCard class to take PlayingCard objects as arguments.
// the overload in the PlayingCard class definitely works.