Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

the magic embedded object

22 views
Skip to first unread message

ruben safir

unread,
Nov 29, 2016, 4:39:47 AM11/29/16
to
Why is it that when I change the origianl vector the object changes but not the reverse.


#include <iostream>
#include <vector>

namespace vect{

/*
* =====================================================================================
* Class: Lvalue
* Description: testing vector access as an lvalue
* =====================================================================================
*/

template < class T >
class Lvalue
{
public:

// ==================== LIFECYCLE =======================================
Lvalue (){}; /* constructor */
Lvalue (std::vector<T> in): _ofthings{in}{}; /* constructor */
Lvalue ( const Lvalue &other ); /* copy constructor */
~Lvalue (){}; /* destructor */

/* ==================== ACCESSORS ======================================= */
std::vector<T>& ofthings(){
return _ofthings;
};

void ofthings(std::vector<T> const vec){
_ofthings = vec;
};
/* ==================== MUTATORS ======================================= */

/* ==================== OPERATORS ======================================= */

const Lvalue& operator = ( const Lvalue &other ); // assignment operator
T& operator [] (int index)
{
return (ofthings()[index]);
};
friend std::ostream &operator << (std::ostream &os, const Lvalue in)
{
for(int i = 0; i < in._ofthings.size(); i++ ){
std::cout << i << "\t";
}
return os;
};

/* ==================== DATA MEMBERS ======================================= */
protected:
std::vector<T> _ofthings;

private:

}; /* ----- end of template class Lvalue ----- */

};


int main ( int argc, char *argv[] )
{

std::cout << "TEST INPUT TO VECTOR" << std::endl;
std::vector<int> test(100, 0);
for(int i = 0; i<10; i++ ){
test[i] = i;
std::cout << i << "::" <<test[i] << "\t";
}
std::cout << std::endl;
std::cout << "TEST INPUT TO OBJECT" << std::endl;
vect::Lvalue<int> test2 {test};
for(int j = 0, i=10; j < 10; i--, j++){
test[j] = i;
std::cout << "What is J" << j << std::endl;
std::cout << "i: " << i << " j: " << j << " test[j]: " << test[j] << std::endl;
std::cout << "i: " << i << " j: " << j << " test2[j]: " << test[j] << std::endl;
}
std::cout << std::endl;
std::cout << "TEST INPUT TO OBJECT bidirectional" << std::endl;
for(int i = 0, j=10; i < 10; i++, j++){
test2[i] = j;
std::cout << "What is i ==>" << i << std::endl;
std::cout << "i: " << i << " j: " << j << " test2[i]: " << test2[i] << std::endl;
std::cout << "i: " << i << " j: " << j << " test[i]: " << test[i] << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
for(int j = 0, i=10; j < 10; i--, j++){
test[j] = i;
std::cout << "What is J" << j << std::endl;
std::cout << "i: " << i << " j: " << j << " test[j]: " << test[j] << std::endl;
std::cout << "i: " << i << " j: " << j << " test2[j]: " << test[j] << std::endl;
}
std::cout << "__DONE__" << std::endl;



return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

TEST INPUT TO VECTOR
0::0 1::1 2::2 3::3 4::4 5::5 6::6 7::7 8::8 9::9
TEST INPUT TO OBJECT
What is J0
i: 10 j: 0 test[j]: 10
i: 10 j: 0 test2[j]: 10
What is J1
i: 9 j: 1 test[j]: 9
i: 9 j: 1 test2[j]: 9
What is J2
i: 8 j: 2 test[j]: 8
i: 8 j: 2 test2[j]: 8
What is J3
i: 7 j: 3 test[j]: 7
i: 7 j: 3 test2[j]: 7
What is J4
i: 6 j: 4 test[j]: 6
i: 6 j: 4 test2[j]: 6
What is J5
i: 5 j: 5 test[j]: 5
i: 5 j: 5 test2[j]: 5
What is J6
i: 4 j: 6 test[j]: 4
i: 4 j: 6 test2[j]: 4
What is J7
i: 3 j: 7 test[j]: 3
i: 3 j: 7 test2[j]: 3
What is J8
i: 2 j: 8 test[j]: 2
i: 2 j: 8 test2[j]: 2
What is J9
i: 1 j: 9 test[j]: 1
i: 1 j: 9 test2[j]: 1

TEST INPUT TO OBJECT bidirectional
What is i ==>0
i: 0 j: 10 test2[i]: 10
i: 0 j: 10 test[i]: 10
What is i ==>1
i: 1 j: 11 test2[i]: 11
i: 1 j: 11 test[i]: 9
What is i ==>2
i: 2 j: 12 test2[i]: 12
i: 2 j: 12 test[i]: 8
What is i ==>3
i: 3 j: 13 test2[i]: 13
i: 3 j: 13 test[i]: 7
What is i ==>4
i: 4 j: 14 test2[i]: 14
i: 4 j: 14 test[i]: 6
What is i ==>5
i: 5 j: 15 test2[i]: 15
i: 5 j: 15 test[i]: 5
What is i ==>6
i: 6 j: 16 test2[i]: 16
i: 6 j: 16 test[i]: 4
What is i ==>7
i: 7 j: 17 test2[i]: 17
i: 7 j: 17 test[i]: 3
What is i ==>8
i: 8 j: 18 test2[i]: 18
i: 8 j: 18 test[i]: 2
What is i ==>9
i: 9 j: 19 test2[i]: 19
i: 9 j: 19 test[i]: 1


What is J0
i: 10 j: 0 test[j]: 10
i: 10 j: 0 test2[j]: 10
What is J1
i: 9 j: 1 test[j]: 9
i: 9 j: 1 test2[j]: 9
What is J2
i: 8 j: 2 test[j]: 8
i: 8 j: 2 test2[j]: 8
What is J3
i: 7 j: 3 test[j]: 7
i: 7 j: 3 test2[j]: 7
What is J4
i: 6 j: 4 test[j]: 6
i: 6 j: 4 test2[j]: 6
What is J5
i: 5 j: 5 test[j]: 5
i: 5 j: 5 test2[j]: 5
What is J6
i: 4 j: 6 test[j]: 4
i: 4 j: 6 test2[j]: 4
What is J7
i: 3 j: 7 test[j]: 3
i: 3 j: 7 test2[j]: 3
What is J8
i: 2 j: 8 test[j]: 2
i: 2 j: 8 test2[j]: 2
What is J9
i: 1 j: 9 test[j]: 1
i: 1 j: 9 test2[j]: 1
__DONE__

Ben Bacarisse

unread,
Nov 29, 2016, 5:45:33 AM11/29/16
to
ruben safir <ru...@mrbrklyn.com> writes:

> Why is it that when I change the origianl vector the object changes
> but not the reverse.

Neither is happening. You've missed a "2" from the two ouput operations.

<snip>
--
Ben.

ruben safir

unread,
Nov 29, 2016, 9:30:04 AM11/29/16
to
On 11/29/2016 05:45 AM, Ben Bacarisse wrote:
> Neither is happening. You've missed a "2" from the two ouput operations.


correct, thanks for looking
0 new messages