Do Assignment Operator Conversion


I

Immortal Nephi

You will find interesting. I want to convert from object B to object
A and I have to use vector.

vector< A > a;
vector< B > b;
a = b;

You can’t do that because vector does not have assignment operator
implementation. What will you do?
Create class A and class B. Both class A and class B are derived
from vector. All the functions of vector are inherited into class A
and class B.

template< typename U >
class B;

template< typename T >
class A : public vector< T > {
public:
template< typename U >
A< T >& operator =( const B< U >& );
};

template< typename T >
class B : public vector< T > {
public:
};

template< typename T >
template< typename U >
A< T > &A< T >::eek:perator =( const B< U > &right ) {
A< T >::iterator iterA = begin();
B< U >::const_iterator iterB = right.begin();

while( iterA != end() ) {
*iterA = *iterB; // sample only
// convertData( iterA, iterB ); // not implemented yet
iterA++;
iterB++;
}

return *this;
}

int main() {
A< int > a;
B< int > b;

a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
a.push_back( 4 );

b.push_back( 10 );
b.push_back( 20 );
b.push_back( 30 );
b.push_back( 40 );

a = b;

return 0;
}

You already know that string is an example. It has a function to
convert from char* to string.
What if you want to convert from string to any type of object?
Think of class A is to be named pixelString and class B is to be
named string. You can do either two ways.

pixelString pixelStrName_A = “There is a cat sitting on the fence.”;

or…

pixelString pixelStrName_A;
string strName_B = “There is a cat sitting on the fence.”;
pixelStringName_A = strName_B;

Each character in the string will be converted to pixelString before
pixelString calls a function to draw 16 x 16 bitmap. You can modify
string and then bitmap will be redrawn automatically.
 
Ad

Advertisements

L

lucdanton

        You will find interesting.  I want to convert from object B to object
A and I have to use vector.

vector< A > a;
vector< B > b;
a = b;

Assuming B is convertible to A:

vector< A > a;
vector< B > b(a.begin(), a.end());

and if you really want assignment:
b.assign(a.begin(), a.end());
 
A

Alf P. Steinbach /Usenet

* lucdanton, on 27.08.2010 17:07:
Assuming B is convertible to A:

vector< A> a;
vector< B> b(a.begin(), a.end());

and if you really want assignment:
b.assign(a.begin(), a.end());

I find the generation of such a pair to be so common that I write

a.assign( CPPX_ALL_OF( b ) );

using a macro. :)

That is, I would write that if I needed to assign a vector like 'b' above to 'a'.

The call would expand to

a.assign( ::cppx::startOf( b ), ::cppx::endOf( b ) );

where b could be any type supported by these functions, including a raw array.

There's much to be said for uniform notation, I think.


Cheers,

- Alf
 
I

Immortal Nephi

* lucdanton, on 27.08.2010 17:07:





I find the generation of such a pair to be so common that I write

   a.assign( CPPX_ALL_OF( b ) );

using a macro. :)

That is, I would write that if I needed to assign a vector like 'b' above to 'a'.

The call would expand to

   a.assign( ::cppx::startOf( b ), ::cppx::endOf( b ) );

where b could be any type supported by these functions, including a raw array.

There's much to be said for uniform notation, I think.

I ask you for your opinion. Do most programmers want to write
assignment operator to support two different objects.
If you add more characters into string, then pixelString will convert
from text character to pixel character automatically through
assignment operator conversion.
You can modify string’s data before pixel character in memory buffer
will be rewritten to new pixel character automatically.
I need to know if assignment operator conversion is forth practice so
I can write my own object conversion.
 
Ad

Advertisements

A

Alf P. Steinbach /Usenet

* Immortal Nephi, on 27.08.2010 20:37:
I ask you for your opinion. Do most programmers want to write
assignment operator to support two different objects.

Well, this doesn't call for opinion: the answer is "no".

If you add more characters into string, then pixelString will convert
from text character to pixel character automatically through
assignment operator conversion.
You can modify string’s data before pixel character in memory buffer
will be rewritten to new pixel character automatically.
I need to know if assignment operator conversion is forth practice so
I can write my own object conversion.

This I don't understand, sorry.


Cheers & hth.,

- Alf
 

Top