基于按值返回函数正确初始化对象
我基本上有以下两个类,我使用按值返回函数来创建对象。 在下面的 Bar 类中,我有两个 Foo 类成员对象。我怎样才能正确初始化,每一个 两个物体分开? 下面,我将给出有关显示的编译器错误的说明。
template < typename T >
class Foo{
public:
Foo( );
Foo( const Foo<T> & );
~Foo();
friend Foo<T> createFoo1( double, bool );
friend Foo<T> createFoo2( double, bool );
friend Foo<T> createFoo3( int );
private:
std::vector<T> m_data;
};
template < typename T >
Foo<T> createFoo1( double param1, bool param2 ){
Foo<T> myFoo;
// fill myFoo.
return (myFoo);
}
template < typename T >
class Bar{
public:
Bar( );
Bar( const Foo<T> &, const Foo<T> & );
Bar( const Bar<T> & );
~Bar( );
friend Bar<T> createBar1( double, bool );
private:
Foo<T> m_fooY;
Foo<T> m_fooX;
};
template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
Bar<T> myBar( createFoo1<T>(param1, param2), createFoo1<T>(param1, param2) ); //OK
return (myBar);
//Bar<T> myBar;
//myBar.m_fooY(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
//myBar.m_fooX(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
//return (myBar);
}
I've basically the following two classes for which I use return-by-value functions to create objects.
In the Bar class below, I've two Foo class member objects. How could I initialize correctly, each of those
two objects separately?
Below, I'm giving an ilustration about the compiler error that is displayed.
template < typename T >
class Foo{
public:
Foo( );
Foo( const Foo<T> & );
~Foo();
friend Foo<T> createFoo1( double, bool );
friend Foo<T> createFoo2( double, bool );
friend Foo<T> createFoo3( int );
private:
std::vector<T> m_data;
};
template < typename T >
Foo<T> createFoo1( double param1, bool param2 ){
Foo<T> myFoo;
// fill myFoo.
return (myFoo);
}
template < typename T >
class Bar{
public:
Bar( );
Bar( const Foo<T> &, const Foo<T> & );
Bar( const Bar<T> & );
~Bar( );
friend Bar<T> createBar1( double, bool );
private:
Foo<T> m_fooY;
Foo<T> m_fooX;
};
template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
Bar<T> myBar( createFoo1<T>(param1, param2), createFoo1<T>(param1, param2) ); //OK
return (myBar);
//Bar<T> myBar;
//myBar.m_fooY(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
//myBar.m_fooX(createFoo1<T>(param1, param2)); // <- error C2064: term does not evaluate to a function taking 1 arguments
//return (myBar);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是除通过构造函数之外设置字段 m_fooX 和 m_fooY 的方法:
Here is how you can set the fields m_fooX and m_fooY other than through the constructor: