1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Returning Class from function

Discussion in 'C++' started by GameZelda, Jul 5, 2010.

  1. GameZelda

    GameZelda New Member

    Jul 5, 2010
    Likes Received:
    Trophy Points:
    This should be a really simple question, but I haven't used C++ in a while and I don't remember how to handle this :(

    #include <iostream>
    #include <memory>
    class test
        std::auto_ptr<int> a;
            test(int *ptr) : a(ptr) { }
    test make_test()
        test x(new int(4));
        return x;
    int main()
        test x = make_test();
        return 0;
    Compiler output:
    In function 'int main()':|
    20|error: no matching function for call to 'test::test(test)'|
    9|note: candidates are: test::test(int*)|
    5|note:                 test::test(test&)|
    ||=== Build finished: 1 errors, 0 warnings ===|
    What's the right way to do this?
    I guess the class "test" is not copyable because of the auto_ptr<int>, but even then I don't know what to do.
    One thing may be returning an auto_ptr<Test> from make_test, but is there any way to avoid this memory allocation?

Share This Page