473,322 Members | 1,504 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,322 software developers and data experts.

operator= accepting a template?

What I want to do is have an operator= accept a template variable.

I will have some classes which all will contain an instance of a different
class. I want an operator= in yet a 3rd class to accept these classes and
use the instance. This is confusing as heck, so here's kinda what I want to
do:

class COffsetMap
{
public:
int Value;
};

class TestClass
{
TestClass& operator=( /* Here is where I want to accept a template */
SomeVar )
{
SomeVar.FieldMap // This is what I need to access
}
};

class CCharFieldMap: public COffsetMap
{
public:
void SetMap() { Value = 10; }
};

class CCharacter
{
public:
CCharFieldMap FieldMap;
};

How would I set up the template, if I even can? The whole purpose in this
is to do this in code:

TestClass Instance;
CCharacter Character;
Character.FieldMap.SetMap();

Instance = Character;

My SetMap actually sets a lot of values, which are actually offsets into the
CCharacter class (variable locations). Saying Instance = Character I would
like to load a vector in TestClass with the variables in Character which I
can get to knowing the base address of Character and the offsets (Value = 10
is just a bogus example right now).

Is this possible? To have an operator=() accept a template parameter? And
if so, how woudl I set up the template?
Aug 23 '06 #1
7 1435
Jim Langston wrote:
What I want to do is have an operator= accept a template variable.

I will have some classes which all will contain an instance of a different
class. I want an operator= in yet a 3rd class to accept these classes and
use the instance. This is confusing as heck, so here's kinda what I want to
do:

class COffsetMap
{
public:
int Value;
};

class TestClass
{
TestClass& operator=( /* Here is where I want to accept a template */
SomeVar )
{
SomeVar.FieldMap // This is what I need to access
}
};
template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

[snip]
Is this possible?
Yes, if I understand you properly.
To have an operator=() accept a template parameter?
Yes, that's quite ordinary - look at how any decent smart pointer
template class is implemented.
And
if so, how woudl I set up the template?
As above.

Best regards,

Tom

Aug 23 '06 #2
Thomas Tutone <Th***********@yahoo.comwrote:
template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}
So, I was intrigued by this question and did a little experiment. I
created a class and gave it a templated assignment operator. However,
it seems the compiler-generated assignment operator is still present.
Why does it call this one instead of the templated version? I was under
the impression that it would not generate the compiler-generated version
in the presence of a user-defined one. Does that only apply to the
compiler-generated constructor?
#include <iostream>
#include <ostream>

class Empty {};
class Test {
int i;
double d;

public:
Test() : i(0), d(0.0) { }
Test(int i_, double d_) : i(i_), d(d_) { }

template <typename T>
Test& operator=(const T& t);

friend std::ostream& operator<<(std::ostream& o, const Test& t);
};

template <typename T>
Test& Test::operator=(const T& t)
{
return *this;
}

template <>
Test& Test::operator=<int>(const int& i_)
{
i = i_;
return *this;
}

template <>
Test& Test::operator=<double>(const double& d_)
{
d = d_;
return *this;
}
std::ostream& operator<<(std::ostream& o, const Test& t)
{
return o << '(' << t.i << ", " << t.d << ')';
}
int main()
{
Test t;
// outputs (0, 0) as expected
std::cout << t << '\n';

t = 3;
// outputs (3, 0) as expected
std::cout << t << '\n';

t = 3.4;
// outputs (3, 3.4) as expected
std::cout << t << '\n';

Empty e;
t = e;
// outputs (3, 3.4) as expected, since the unspecialized operator=()
// is essentially a no-op
std::cout << t << '\n';

Test t2(42, 3.14);
t = t2;
// outputs (42, 3.14)
// but I expected operator=<Test>() to be called,
// hence (3, 3.4) as before
std::cout << t << '\n';
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 23 '06 #3
Marcus Kwok wrote:
Thomas Tutone <Th***********@yahoo.comwrote:
> template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

So, I was intrigued by this question and did a little experiment. I
created a class and gave it a templated assignment operator. However,
it seems the compiler-generated assignment operator is still present.
Why does it call this one instead of the templated version? I was
under the impression that it would not generate the
compiler-generated version in the presence of a user-defined one.
Does that only apply to the compiler-generated constructor?
A non-template version always wins. Implicitly declared/defined copy
assingment op and copy-constructors are never replaced with template
ones. That's how the Standard requires it (12.8/3).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '06 #4

"Victor Bazarov" <v.********@comAcast.comwrote in message
news:ec**********@news.datemas.de...
Marcus Kwok wrote:
>Thomas Tutone <Th***********@yahoo.comwrote:
>> template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

So, I was intrigued by this question and did a little experiment. I
created a class and gave it a templated assignment operator. However,
it seems the compiler-generated assignment operator is still present.
Why does it call this one instead of the templated version? I was
under the impression that it would not generate the
compiler-generated version in the presence of a user-defined one.
Does that only apply to the compiler-generated constructor?

A non-template version always wins. Implicitly declared/defined copy
assingment op and copy-constructors are never replaced with template
ones. That's how the Standard requires it (12.8/3).
So, if I wanted a version of the assignment operator where the parameter was
the same class (e.g., const Test&, using Marcus' example for his class
Test), then I'd need to need to explicitly declare that operator in the Test
class, instead of using a template, right?

-Howard

Aug 23 '06 #5
Howard wrote:
"Victor Bazarov" <v.********@comAcast.comwrote in message
news:ec**********@news.datemas.de...
>Marcus Kwok wrote:
>>Thomas Tutone <Th***********@yahoo.comwrote:
template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

So, I was intrigued by this question and did a little experiment. I
created a class and gave it a templated assignment operator. However, it
seems the compiler-generated assignment operator is
still present. Why does it call this one instead of the templated
version? I was under the impression that it would not generate the
compiler-generated version in the presence of a user-defined one.
Does that only apply to the compiler-generated constructor?

A non-template version always wins. Implicitly declared/defined copy
assingment op and copy-constructors are never replaced with template
ones. That's how the Standard requires it (12.8/3).

So, if I wanted a version of the assignment operator where the
parameter was the same class (e.g., const Test&, using Marcus'
example for his class Test), then I'd need to need to explicitly
declare that operator in the Test class, instead of using a template,
right?
I believe so.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '06 #6

Howard wrote:
"Victor Bazarov" <v.********@comAcast.comwrote in message
news:ec**********@news.datemas.de...
Marcus Kwok wrote:
Thomas Tutone <Th***********@yahoo.comwrote:
template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

So, I was intrigued by this question and did a little experiment. I
created a class and gave it a templated assignment operator. However,
it seems the compiler-generated assignment operator is still present.
Why does it call this one instead of the templated version? I was
under the impression that it would not generate the
compiler-generated version in the presence of a user-defined one.
Does that only apply to the compiler-generated constructor?
A non-template version always wins. Implicitly declared/defined copy
assingment op and copy-constructors are never replaced with template
ones. That's how the Standard requires it (12.8/3).

So, if I wanted a version of the assignment operator where the parameter was
the same class (e.g., const Test&, using Marcus' example for his class
Test), then I'd need to need to explicitly declare that operator in the Test
class, instead of using a template, right?
You would need to explicitly define it, unless the implicitly
declared/defined version met your needs. In that latter case, there
would be little reason to explicitly define it (and no reason to
explicitly declare it without defining it as well).

Best regards,

Tom

Aug 23 '06 #7

"Thomas Tutone" <Th***********@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Jim Langston wrote:
>What I want to do is have an operator= accept a template variable.

I will have some classes which all will contain an instance of a
different
class. I want an operator= in yet a 3rd class to accept these classes
and
use the instance. This is confusing as heck, so here's kinda what I want
to
do:

class COffsetMap
{
public:
int Value;
};

class TestClass
{
TestClass& operator=( /* Here is where I want to accept a template */
SomeVar )
{
SomeVar.FieldMap // This is what I need to access
}
};

template<typename T>
TestClass& operator=(const T& t)
{
// do whatever it is you do to t here
return *this;
}

[snip]
Thank you. Works as advertised.
Aug 23 '06 #8

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

Similar topics

6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
5
by: TOMERDR | last post by:
Hi, I was requested to write a code similar to this: CArchive ar; //Not mfc CArchive..... .... void * vp = &XXX; // any object. ar << vp, sizeof(XXX);
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
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) ?
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.