473,402 Members | 2,055 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,402 software developers and data experts.

How do you do assignment operator within a template ?

How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};

Oct 3 '06 #1
8 1675
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};
What shall your assignment operator do? If you have a vector with 10 strings
and you assign a new string to your vector class shall it replace all the 10
strings?

Boris
Oct 3 '06 #2
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }
};
Not sure what it is you're asking. What do you need your operator
to do? Why are you assigning from an object to T to a vector? Do you
intend to assign all of the elements to that value? Then you need
a loop.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #3
im*****@hotmail.co.uk wrote:
How do you do assignment operator within a template ?

#include <vector>

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>(s) { }
T& operator[](int i) { return this -at(i); }
const T& operator[](int i) const { return this -at(i); }
T& operator=(const T& rhs) { return this -// and then what, if
that is right atall ? }

somthing like this should work.
T& operator=(const T& rhs) {
{
// this->thing = rhs.thing;
... etc
return *this;
}
};
Oct 3 '06 #4
oh sorry I did not think of that case, no I wanted to see when elements
are being assigned to

T& operator=[](const &i, const T& rhs) {
{
this->thing[i] = rhs.thing;
std::cout << "its here << " i << " " << rhs.thing;
return *this;
}

Oct 3 '06 #5
Greg wrote:
oh sorry I did not think of that case, no I wanted to see when
elements are being assigned to

T& operator=[](const &i, const T& rhs) {
{
this->thing[i] = rhs.thing;
std::cout << "its here << " i << " " << rhs.thing;
return *this;
}
No, you can't do that. Indexing operator has only one argument.

If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #6
No, you can't do that. Indexing operator has only one argument.
>
If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);
Will the assignment call this "set" e.g.

std::vector<intx;
x[0] = blah;

or would i have to replace "=" by set function ?
x.set(0, blah);

Oct 3 '06 #7
Greg wrote:
>No, you can't do that. Indexing operator has only one argument.

If you want to see when your elements are assigned to, do not provide
the indexing operator but instead have a 'set' type function:

void set(int i, const T& val);

Will the assignment call this "set" e.g.

std::vector<intx;
x[0] = blah;

or would i have to replace "=" by set function ?
x.set(0, blah);
The latter.

If you need to track assignment, you would have to implement a proxy
class. See my recent post to comp.lang.c++.moderated for an example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #8
The latter.
>
If you need to track assignment, you would have to implement a proxy
class. See my recent post to comp.lang.c++.moderated for an example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
OK thanks, I don't think this is the problem, I just realised a
potential flaw in my design something very different, I will post anew.

Oct 3 '06 #9

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

Similar topics

8
by: Clifton M. Bean | last post by:
First, I defined three classes (listed below): =========== // 1st class =========== class PointCl { public: PointCl & operator= (const PointCl & rgh ); //define as usual assingment operator
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
3
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain...
4
by: PengYu.UT | last post by:
The following shows the source code and the error message(from g++-3.3). I wrote two assignment operator. One is for the same type case, the other one is for different type case. I'm wondering why...
3
by: Hans | last post by:
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.
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
1
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.