考虑以下模板类:
template <typename T> class Function {
public:
virtual float eval( const T &x, const T &y ) = 0;
};
Run Code Online (Sandbox Code Playgroud)
由于'eval'函数不应该修改两个输入'x'和'y'的值,我把它们设为'const'.然后我创建从Function派生的以下类
class Foo1 : public Function <float*> {
public:
Foo1() : Function <float*> () {}
virtual float eval( const float* &x, const float* &y ) { ... }
};
Run Code Online (Sandbox Code Playgroud)
当我用g ++编译时,我收到以下警告:
hidden overloaded virtual function 'Function<float *>::eval' declared here: type mismatch at 1st parameter
('float *const &' vs 'const float *&')
virtual float eval( const T &x, const T …Run Code Online (Sandbox Code Playgroud) 考虑以下模板类
template <typename T>
class A {
public:
virtual T const& getContent() = 0;
};
Run Code Online (Sandbox Code Playgroud)
我使用'int*'作为类型'T'派生这个类,如下所示
class A1 : public A<int*> {
private:
int a;
public:
int* const& getContent() { return &a; }
};
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:'返回对本地临时对象的引用'.
问题:
在返回引用之前,编译器是否从'&a'隐式实例化了'int*const'类型的本地临时对象?
我知道Aa确实存在,那么我可以忽略这个警告吗?使用此代码会产生任何不良副作用吗?
处理这种情况的正确方法是什么?我是否需要使用成员变量'int*a'.这将是麻烦的.