Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with Copy Constructor and overloaded assignment operator with templates

Reply
Thread Tools

Problem with Copy Constructor and overloaded assignment operator with templates

 
 
saxman
Guest
Posts: n/a
 
      06-12-2007
Hi everyone,

I'm writing a class that inherits from std::vector in order to add
some additional functionality. I'm having a compiler problem, however,
when I have a function that returns this class. I've drilled down and
been able to create a very simple example of the problem. Here's my
code:

<code>
#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

template <typename T>
class B : public std::vector<T>
{
public:
B(){ cout << "In B ctor" << endl; }

B(B<T> &b) { cout << "In B copy ctor" << endl; }

B<T>& operator=(const B<T> &b) { cout << "In B assign." << endl;
return *this; }

~B(){ cout << "In B destructor" << endl; }

};

B<int> test()
{
B<int> returnVal;
return returnVal;
}

int main()
{
B<int> b;
b = test();
return 0;
}
</code>

I get the following error when compiling:
test.cpp: In function `int main()':
test.cpp:31: error: no matching function for call to
`B<int>::B(B<int>)'
test.cpp:14: note: candidates are: B<T>::B(B<T>&) [with T = int]

Does anyone know of a solution?

Some additional information: any of the following code in 'main'
compiles fine:
<code>
B<int> b;
B<int> b2 = b;
return 0;
</code>

or

<code>
B<int> b;
B<int> b2;
b2 = b;
return 0;
</code>

or

<code>
B<int> b;
B<int> b2(b);
return 0;
</code>

Any help would be greatly appreciated.

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      06-12-2007
On 12 Juni, 16:08, saxman <erll...@gmail.com> wrote:
> Hi everyone,
>
> I'm writing a class that inherits from std::vector in order to add
> some additional functionality. I'm having a compiler problem, however,
> when I have a function that returns this class. I've drilled down and
> been able to create a very simple example of the problem. Here's my
> code:
>
> <code>
> #include <vector>
> #include <iostream>
>
> using std::vector;
> using std::cout;
> using std::endl;
>
> template <typename T>
> class B : public std::vector<T>
> {
> public:
> B(){ cout << "In B ctor" << endl; }
>
> B(B<T> &b) { cout << "In B copy ctor" << endl; }
>
> B<T>& operator=(const B<T> &b) { cout << "In B assign." << endl;
> return *this; }
>
> ~B(){ cout << "In B destructor" << endl; }
>
> };
>
> B<int> test()
> {
> B<int> returnVal;
> return returnVal;
>
> }
>
> int main()
> {
> B<int> b;
> b = test();


The problem is that the compiler converts this to a copy-constructor
call like this 'B<int>(test())', however since your copy-constructor
takes a reference as a parameter this can not compile (since a
reference can not bind to the temporary returned by test()). To solve
this simply change the copy-constructor to 'B(const B<T> &b)'. In
general the parameters to copy-constructors should be const.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
saxman
Guest
Posts: n/a
 
      06-12-2007
On Jun 12, 10:16 am, Erik Wikström <eri...@student.chalmers.se> wrote:
> On 12 Juni, 16:08, saxman <erll...@gmail.com> wrote:
>
>
>
> > Hi everyone,

>
> > I'm writing a class that inherits from std::vector in order to add
> > some additional functionality. I'm having a compiler problem, however,
> > when I have a function that returns this class. I've drilled down and
> > been able to create a very simple example of the problem. Here's my
> > code:

>
> > <code>
> > #include <vector>
> > #include <iostream>

>
> > using std::vector;
> > using std::cout;
> > using std::endl;

>
> > template <typename T>
> > class B : public std::vector<T>
> > {
> > public:
> > B(){ cout << "In B ctor" << endl; }

>
> > B(B<T> &b) { cout << "In B copy ctor" << endl; }

>
> > B<T>& operator=(const B<T> &b) { cout << "In B assign." << endl;
> > return *this; }

>
> > ~B(){ cout << "In B destructor" << endl; }

>
> > };

>
> > B<int> test()
> > {
> > B<int> returnVal;
> > return returnVal;

>
> > }

>
> > int main()
> > {
> > B<int> b;
> > b = test();

>
> The problem is that the compiler converts this to a copy-constructor
> call like this 'B<int>(test())', however since your copy-constructor
> takes a reference as a parameter this can not compile (since a
> reference can not bind to the temporary returned by test()). To solve
> this simply change the copy-constructor to 'B(const B<T> &b)'. In
> general the parameters to copy-constructors should be const.
>
> --
> Erik Wikström


Thanks for the help and the explanation, that worked perfectly

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy constructor and assignment operator harry C++ 6 08-15-2009 01:40 PM
overloaded assignment operator and copy constructor. sam_cit@yahoo.co.in C++ 8 08-29-2007 04:13 PM
Copy constructor and assignment operator inheritance Henrik Goldman C++ 2 12-26-2006 05:07 PM
Does assignment operator have to behave exactly like copy constructor bluekite2000@gmail.com C++ 12 06-16-2005 09:44 PM
Templates, copy constructor and operator<< franky.backeljauw@ua.ac.be C++ 4 07-04-2003 03:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57