In c++, how is it possible for child class, or friend function to access all possible template types of the parent? How do I modify the code below, so that no matter what the type T
is, the friend function and child function will not suffer from type errors? (Currently only the type of int works properly).
// PARENT CLASS:¨
template <class T>
class parent{
public:
T variable1;
friend int function1(parent, int a);
};
//CHILD CLASS:
class child : public parent<int> {
public:
void setvariable(int a){ variable1 = a; };
};
// FRIEND FUNCTION:
int function1(parent<int> k, int a) {
return k.variable1 +a;
};
So that the following would then compile without errors:
int main() {
child child1; //Child
child child2;
child1.setvariable(4);
child2.setvariable(4.4); //Type error/retyping
cout << function1(child1.variable1, 4) << endl; // Function
cout << function1(child2.variable1, 4.0) << endl; // Type error
system("pause");
return 1;
}