operator= accepting a template?

J

Jim Langston

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?
 
T

Thomas Tutone

Jim said:
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
 
M

Marcus Kwok

Thomas Tutone said:
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::eek:stream& operator<<(std::eek:stream& o, const Test& t);
};

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

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

template <>
Test& Test::eek:perator=<double>(const double& d_)
{
d = d_;
return *this;
}


std::eek:stream& operator<<(std::eek:stream& 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';
}
 
V

Victor Bazarov

Marcus said:
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
 
H

Howard

Victor Bazarov said:
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
 
V

Victor Bazarov

Howard said:
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
 
T

Thomas Tutone

Howard said:
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
 
J

Jim Langston

Thomas Tutone said:
Jim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top