[英]copying a pointer created dynamically in array of pointers
I am beginner,and i am facing problem when trying to copy one pointer to object in array of pointers.. 我是新手,在尝试将一个指针复制到指针数组中的对象时遇到问题。
class Publication
{
char title[20];
int idd;
public:
char* getT()
{
return title;
}
virtual void getData()
{
cout<<"Enter Title of Book";
cin>>title;
cout<<"Enter ID of Book";
cin>>idd;
}
virtual void putData()
{
cout<<"Title of Book : "<<title<<"\n";
cout<<"ID of Book : "<<idd;
}
};
class Book:private Publication
{
int pages;
public:
void getData()
{
cout<<"Enter Title of Book";
cin>>getT();
cout<<"Enter Pages ";
cin>>pages;
}
void putData()
{
cout<<"Title of Book : "<<getT()<<"\n";
cout<<"No of Pages : "<<pages;
}
};
Main Function 主功能
int main()
{
Publication *ptrpub[2];
Book *ptr;
for(int i=0;i<2;i++)
{
cout<<"Enter Data for Book Object";
ptr=new Book;
ptr->getData();
Now within this loop, i want to move that pointer in an array of pointers to publication ie ptrpub[],where Book class inherits from Publication Privately 现在在此循环中,我想将该指针移动到指向发布的指针数组中,即ptrpub [],其中Book类从发布私有地继承
ptrpub[i]=ptr; //'Publication' is an inaccessible base of 'Book'
Is there any way to copy them,so i may use ptrpub[i]->putdata() to show all data entered for book object.. And I must keep that relationship between Book and Publication class. 有没有办法复制它们,所以我可以使用ptrpub [i]-> putdata()来显示为书本对象输入的所有数据。而且我必须保持Book和Publication类之间的关系。
The problem is that you inherit Book
from Publication
privately: 问题是您私自继承了Publication
Book
:
class Book:private Publication
This means that only Book
's implementation is aware that it inherits from Publication
-- code external to the class does not know this, and so you cannot cast Book
to Publication
except from within members of Book
(or friend members). 这意味着只有Book
的实现知道它是从Publication
继承的-类外部的代码不知道这一点,因此,您只能将Book
成员(或朋友成员)内部的Book
Book
为Publication
。
You can resolve this by inheriting publicly: 您可以通过公开继承来解决此问题:
class Book:public Publication
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.