如何调用另一个类的对象的模板构造函数?
例:
#include <iostream>
using namespace std;
class RGB{
private:
int _R;
int _G;
int _B;
public:
RGB(int a, int b, int c){
_R=a;
_G=b;
_B=c;
}
};
class Gray{
int _G;
public:
Gray(int x){
_G=x;
}
};
template <class T1, class T2>
class Pixel{
public:
Pixel(T1 i, T2 j, int a, int b){
foo=i;
bar=j;
x=a;
y=b;
}
void setPixel(T1 a, T2 b){
foo=a;
bar=b;
}
void getPixel();
private:
int x;
int y;
T1 foo;
T2 bar;
};
int …Run Code Online (Sandbox Code Playgroud) 这个条件在构造函数中是正确的还是有更好的方法来做到这一点?
class Foo {
public: Foo(int y) {
if (y < 0 || y > 99)
cout << "Error! Invalid input" << endl;
else
x = y;
}
private: int x;
};
Run Code Online (Sandbox Code Playgroud)