开发者

Requirements for directly returning a class instance from a function?

开发者 https://www.devze.com 2023-01-05 11:09 出处:网络
I\'m got a function in C++ that wraps an output stream constructor so it can pass in a pointer to a parent class that it\'s running in, ala:

I'm got a function in C++ that wraps an output stream constructor so it can pass in a pointer to a parent class that it's running in, ala:

pf_istream PF_Plugin::pf_open_in() {
    return     pf_istream(this);
}

When I do it that way, however, I get something like this:

pf_plugin.cc:103: error: no matching function for call to ‘pf_istream::pf_istream(pf_istream)’

pf_istream.hh:36: note: candidates are: pf_istream::pf_istream(const PF_Plugin*)

pf_istream.hh:29: note: pf_istream::pf_istream(pf_istream&)

I've got a copy constructor and assignment operator defined (as can be seen above), and if I write the funct开发者_如何转开发ion like so:

pf_istream PF_Plugin::pf_open_in() {
   pf_istream to_ret(this); 
   return     to_ret;
}

It works just fine. I get similar errors if I try to assign a pf_istream instance directly from a call to the function, eg:

pf_istream inFile = pf_open_in();

What else do I need to add to make this work?


> pf_istream::pf_istream(pf_istream&)

This is your problem. It's not properly finding your copy constructor, because it's passing a temporary. Your copy constructor should take a const&, like so:

pf_istream::pf_istream(const pf_istream&) { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消