S
saneman
I have made two modules:
1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector
#include "head.h"
#include<vector>
#include<iostream>
struct P {
int x;
int y;
};
int main() {
typedef std::vector<P> container;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<container> a(v);
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}
2) head.h
A header file that contains the class A.
template <typename T >
class A {
public:
T t;
A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;
}
};
In class A getValue() returns the x field of the struct defined in main.cpp.
But how does class A know about the type 'P' which is only created in main?
1) main.cpp
Contains a main function and struct P.
Includes head.h and std::vector
#include "head.h"
#include<vector>
#include<iostream>
struct P {
int x;
int y;
};
int main() {
typedef std::vector<P> container;
container v;
P p;
p.x = 22;
p.y = 33;
v.push_back(p);
A<container> a(v);
int res = a.getValue();
std::cout << res << std::endl;
return 0;
}
2) head.h
A header file that contains the class A.
template <typename T >
class A {
public:
T t;
A(T t_){
t = t_;
}
int getValue() {
int res = (*t.begin()).x;
return res;
}
};
In class A getValue() returns the x field of the struct defined in main.cpp.
But how does class A know about the type 'P' which is only created in main?