基于按值返回函数正确初始化对象

发布于 2024-12-18 03:01:19 字数 1487 浏览 2 评论 0原文

我基本上有以下两个类,我使用按值返回函数来创建对象。 在下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-12-25 03:01:19

以下是除通过构造函数之外设置字段 m_fooX 和 m_fooY 的方法:

template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
  Bar<T> myBar;
  myBar.m_fooY = createFoo1<T>(param1, param2);
  myBar.m_fooX = createFoo1<T>(param1, param2);
  return myBar;
}

Here is how you can set the fields m_fooX and m_fooY other than through the constructor:

template < typename T >
Bar<T> createBar1( double param1, bool param2 ){
  Bar<T> myBar;
  myBar.m_fooY = createFoo1<T>(param1, param2);
  myBar.m_fooX = createFoo1<T>(param1, param2);
  return myBar;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文