J
jerry.teshirogi
I have the following class and main:
//////////////////////////////////////////////////////////
#include <iostream.h>
class myVector
{
public:
double x, y, z:
void set( const double &, const double &, const double &);
friend ostream & operator<<( ostream &, const vector & );
}
////////////////////////////////////////////////////////////
void myVector::set( const double &xin, const double &yin, const double
&zin )
{
x = xin;
y = yin;
z = zin;
}
ostream & operator<<( ostream &os, const vector &v )
{
os << v.x << '\t' << v.y<< '\t' << v.z;
return os;
}
//////////////////////////////////////////////////////////
1:int main()
2:{
3: double dra[3] = {0.0, 0.1, 0.2};
4:
5: myVector v0 = {0.0, 0.2, 0.4};
6:
7: myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };
8: cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
dra[2] << endl;
9: cout << "v0 = " << v0 << endl;
10: for( int i; i < 2; i ++ )
11: cout << "v1[" << i << "] = " << v1 << endl;
12:}
The above code illustrates a portion of a large legacy simulation that
I'm looking to upgrade.
I would like to add much more functionality to myVector class. To
start off, I would like to add constructors, and make the member
variables x, y, & z private scope. But I found that if I do either
one of those things, the compiler squawks on lines 5 & 7 in the main,
and says that myVector class doesn't provide suitable functions to
implement those lines. And if I take out the constructors and set my
member variables back to public scope, the compiler stops complaining.
It's as if my adding stuff to the class is taking away an inherent
hidden functionality of the class. But I need to keep that
functionality, because the legacy simulation demands it; the sim
assigns values to myVector instances from lists of doubles, like this,
myVector v0 = {0.0, 0.2, 0.4};
My solution approach so far has been to add an overloaded assignment
operator (operator=) to work for lines 5 & 7. The problem I'm having
is, I don't know how to declare the RVALUE portion (i.e., {0.0, 0.2,
0.4} ) in the declaration of the overloaded operator=.
vector & operator=( ???????? ) ;
I'm not entirely sure if it's actually possible to declare RVALUES
like this. But I'm betting it's doable. Array of doubles, array
of floats, and array of ints can be initialized like in line # 3 in
the main. And if those guys are all C++ classes and have that
functionality, then there ought to be a way to add the same capability
in to my class, right?
But I'm stuck...
I'm open to suggestions on other approaches as well.
My desire is really to be able to add constructors, private variables,
and a host of other functions to the class, and, of course, also be
able to assign values to myVector instances from a list as in lines 5
& 7.
Thanks in advance for your help...
//////////////////////////////////////////////////////////
#include <iostream.h>
class myVector
{
public:
double x, y, z:
void set( const double &, const double &, const double &);
friend ostream & operator<<( ostream &, const vector & );
}
////////////////////////////////////////////////////////////
void myVector::set( const double &xin, const double &yin, const double
&zin )
{
x = xin;
y = yin;
z = zin;
}
ostream & operator<<( ostream &os, const vector &v )
{
os << v.x << '\t' << v.y<< '\t' << v.z;
return os;
}
//////////////////////////////////////////////////////////
1:int main()
2:{
3: double dra[3] = {0.0, 0.1, 0.2};
4:
5: myVector v0 = {0.0, 0.2, 0.4};
6:
7: myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };
8: cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
dra[2] << endl;
9: cout << "v0 = " << v0 << endl;
10: for( int i; i < 2; i ++ )
11: cout << "v1[" << i << "] = " << v1 << endl;
12:}
The above code illustrates a portion of a large legacy simulation that
I'm looking to upgrade.
I would like to add much more functionality to myVector class. To
start off, I would like to add constructors, and make the member
variables x, y, & z private scope. But I found that if I do either
one of those things, the compiler squawks on lines 5 & 7 in the main,
and says that myVector class doesn't provide suitable functions to
implement those lines. And if I take out the constructors and set my
member variables back to public scope, the compiler stops complaining.
It's as if my adding stuff to the class is taking away an inherent
hidden functionality of the class. But I need to keep that
functionality, because the legacy simulation demands it; the sim
assigns values to myVector instances from lists of doubles, like this,
myVector v0 = {0.0, 0.2, 0.4};
My solution approach so far has been to add an overloaded assignment
operator (operator=) to work for lines 5 & 7. The problem I'm having
is, I don't know how to declare the RVALUE portion (i.e., {0.0, 0.2,
0.4} ) in the declaration of the overloaded operator=.
vector & operator=( ???????? ) ;
I'm not entirely sure if it's actually possible to declare RVALUES
like this. But I'm betting it's doable. Array of doubles, array
of floats, and array of ints can be initialized like in line # 3 in
the main. And if those guys are all C++ classes and have that
functionality, then there ought to be a way to add the same capability
in to my class, right?
But I'm stuck...
I'm open to suggestions on other approaches as well.
My desire is really to be able to add constructors, private variables,
and a host of other functions to the class, and, of course, also be
able to assign values to myVector instances from a list as in lines 5
& 7.
Thanks in advance for your help...