我偶然发现了一个在我看来非常有趣的问题。我不是要解决这个问题,只是建议如何继续我的代码,我想知道派生类是否继承了基类的 friend 。
问题是:
Class matrix, friend of class array is the base class for the class diagonal_matrix. The derived class must contain a parameterized constructor through which to highlight the transmission of parameters towards the constructor from the base class, destructor and a method to check if the matrix is square and diagonal ( all elements other than the ones from the principal diagonal are equal to zero). Illustrate the concept of virtual function (pure if it's more natural in implementation).
这是我到目前为止写的(今天关于如何继续不是很有创意):
#include <iostream>
using namespace std;
class arry
{
private:
int *vec;
int n;
public:
int i,nrelem;
arry(){};
~arry();
void readArray(int);
};
arry::~arry()
{
n=0;
delete [] vec;
}
void arry::readArray(int elem)
{
n=elem;
vec=new int[n];
for(i=1;i<=elem;i++)
{
cout<<"vec["<<i<<"]=";
cin>>vec[i];
}
}
class matrix
{
public:
friend class arry;
matrix(int , int);
~matrix();
};
int main()
{
return 0;
}
最佳答案
friend of class array is the base class for the class diagonal_matrix
类 array
没有 friend 。 array
类是 matrix
类的 friend ,因为它是 matrix
声明的
class matrix
{
public:
friend class arry; // this is my friend
matrix(int , int);
~matrix();
};
你要求
advices on how to continue with my code
从在 array
类中声明友元开始。
and I'd like to know if derived classes inherit the friends of the base class
如果基类将 class A
声明为友元,则它不是派生类的友元,除非该派生类也将 class A
声明为友元。
C++ 标准 n3337 § 11.3/10 friend
Friendship is neither inherited nor transitive.
关于c++ - 类(class)传承与友元项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920931/