Если у меня есть иерархия такого типа:
#include <iostream>
using namespace std;
template<class T>
class typeB;
template<class T>
class typeA
{
// Private data
T* x_;
int size_;
public:
// Constructors
typeA()
: x_(0), size_(0)
{
};
typeA(int size)
: x_(0), size_(size)
{
}
// Friend classes.
friend class typeB<T>;
};
template<class T>
class typeB
: public typeA<T>
{
public:
// Constructors
typeB ()
{
};
typeB (int size)
: typeA<T>(size)
{
//this->x_ = new T[size];
x_ = new T[size];
}
};
int main()
{
typeB<int> b(4);
return 0;
}
, почему мне нужно указать "this-> x_ = new T [size]" в конструкторе typeB (int size) вместо "x_= новый T [размер] ", чтобы получить этот код для компиляции?
Что мне говорит компилятор, так это то, что он не может разрешить тип x _:
main.cpp: In constructor ‘typeB<T>::typeB(int)’:
main.cpp:42: error: ‘x_’ was not declared in this scope
Если typeB является другом типа A, он должен иметь открытый доступ к атрибутам typeA.Если я попробую это с не шаблонными классами, это сработает:
#include <iostream>
using namespace std;
class typeB;
class typeA
{
// Private data
int* x_;
int size_;
public:
// Constructors
typeA()
: x_(0), size_(0)
{
};
typeA(int size)
: x_(0), size_(size)
{
}
// Friend classes.
friend class typeB;
};
class typeB
: public typeA
{
public:
// Constructors
typeB ()
{
};
typeB (int size)
: typeA(size)
{
x_ = new int[size];
}
};
int main()
{
typeB b(4);
return 0;
}
typeA и typeB являются своего рода контейнерами списка: как вы думаете, что будет мотивом для такого рода отношений (публичное наследование + друзьяvs наличие x_ и size_ в качестве защищенных атрибутов, если требуется прямой доступ)?