473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

copy assignment for different length

Hi All,

How do I change the copy assignment header as shown below to allow for
different array length?

bool_vector& operator= (const bool_vector& t)

Thanks,
Hans.

template<int len>
class bool_vector {
private:
bool *v;
int i;
int sz;
public:
bool_vector() {
v=new bool[sz=len];
for (i=0; i<sz; i++) v[i]=0;
};
~bool_vector() {
delete [] v;
};
bool_vector& operator= (const bool_vector& t) {
cout << "rhs=" << sz << " lhz=" << t.sz;
if (this != &t) {
delete[] v;
v=new bool[sz]; // sz=t.sz
for (i=0;i<sz;i++) if (i<t.sz) v[i]=t.v[i]; else v[i]=0;
};
return *this;
}
};

int main()
{
bool_vector<4a,b;
bool_vector<5c;
a=b;
//c=b; // fails
}
Jul 8 '06 #1
3 1128
Hans posted:
Hi All,

How do I change the copy assignment header as shown below to allow for
different array length?

bool_vector& operator= (const bool_vector& t)

template<int i>
bool_vector<i&operator=(const bool_vector<i&t);
--

Frederick Gotham
Jul 8 '06 #2
Hans wrote:
Hi All,

How do I change the copy assignment header as shown below to allow for
different array length?
It's not a copy assignment operator because bool_vector<4>
is a DIFFERENT TYPE then bool_vector<5>. It's not clear
why you even use the size as a typed arg to the vector as
it seems nothing in your class really needs to be a template.
You don't even treat the length as constant.

I don't know what you want to do with differing sized arrays
but to allow assignemnt from one to another you'll have to
template an operator= on the additional type:

template <class Rbool_vector& operator=(const bool_vector<R>&() {
...
By the way, your class needs a copy constructor.
Jul 8 '06 #3

"Ron Natalie" <ro*@spamcop.netwrote in message
news:44***********************@news.newshosting.co m...
Hans wrote:
>Hi All,

How do I change the copy assignment header as shown below to allow for
different array length?
It's not a copy assignment operator because bool_vector<4>
is a DIFFERENT TYPE then bool_vector<5>. It's not clear
why you even use the size as a typed arg to the vector as
it seems nothing in your class really needs to be a template.
Because I am a beginner :-)

I understand my error, the length should be handled in the constructor and
not with a template. I changed the code and now the assignment c=b works
fine.

Thanks,
Hans
Jul 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
20
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object...
5
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.