Hi folks.
This is a bit OT with respect to DSP, but I know there are
many skilled programmers here so maybe somebody can
help me out.
I am implementing these classes that basically look like
class a {
:
};
class b{
private:
a *p;
int i;
public:
:
};
Basically, class b is an array of objects of class a,
with some extra functionality attached.
I would like to be able to access an instance of class
b as if it really was an array of a's:
a A; // An instance of class a
b B; // An "augmented array of as"
a C[4]; // A "true" array of as
With the above declarations, both
A = C[1];
C[0] = A;
are legal.
I want to be able to do similar things as
A = B[1];
B[0] = A;
where what one actually does is
A = *(B.p+1);
*(B.p+1) = A;
In order to achieve this, the operators "=" and "[]"
must be overloaded, which is where my efforts grind
to a halt... It does not help my efforts that I am
way out to sea with no prospect of getting back
to my C++ books for another three weeks...
Needless to say, any help is most welcome!
Rune
OT: C++ overloading operators
Started by ●September 23, 2006
Reply by ●September 23, 20062006-09-23
Rune Allnor wrote:> Basically, class b is an array of objects of class a, > with some extra functionality attached. > > I would like to be able to access an instance of class > b as if it really was an array of a's: > > a A; // An instance of class a > b B; // An "augmented array of as" > a C[4]; // A "true" array of as > > With the above declarations, both > > A = C[1]; > C[0] = A; > > are legal. > > I want to be able to do similar things as > > A = B[1]; > B[0] = A; > > where what one actually does is > > A = *(B.p+1); > *(B.p+1) = A; > > In order to achieve this, the operators "=" and "[]" > must be overloaded, which is where my efforts grind > to a halt... It does not help my efforts that I am > way out to sea with no prospect of getting back > to my C++ books for another three weeks...You may get most of your expressions working if you do enough investigations, but in fact it is much easier to use STL classes which are part of the C++ standard. The expression *(B.p+1) is uncommon. Usually this is written *(B.begin()+1). The class vector<a> will most likely meet your demands. (Use #include <vector> or #include <vector.h> on older compilers.) Since the STL classes are templates, it is usually sufficient to get the STL header files if your compiler does not support it out of the box. SGI has a free implementation and a good reference to it at www.sgi.com/tech/stl. Marcel
Reply by ●September 23, 20062006-09-23
Rune Allnor wrote: #include <cassert> #include <cstddef>> class a {int member; public: a& operator=(const a& other) { member = other.member; return *this; }> }; > > class b{ > private: > a *p; > int i; > public:a& operator[](std::size_t n) { assert(n < i); // if that's what i means return p[n]; } const a& operator[](std::size_t n) const { assert(n < i); return p[n]; }> };The b::operator[] const overload is necessary so you can use subscript expressions in functions that take const b&. There's a chance that you don't need to write a::operator= yourself -- the autogenerated operator simply assigns all base subobjects and members. However, if you need exception safety then use the copy/swap idiom; see http://groups.google.com/groups?q=c%2B%2B+assignment+%22copy+swap%22 Martin -- There are three kinds of people -- those who can count and those who can't.
Reply by ●September 23, 20062006-09-23
Rune Allnor wrote:> It does not help my efforts that I am way out to sea with no > prospect of getting back to my C++ books for another three weeks...When the prophet cannot go to the scripture, let the scripture come to the prophet ;) http://kungens.kemi.fi/eckel/ Thinking in C++ vol. 1 treats operator overloading. Martin -- The difference between genius and stupidity is that genius has limits.
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune
Reply by ●September 23, 20062006-09-23
Martin Eisenberg wrote:> Rune Allnor wrote: > > > It does not help my efforts that I am way out to sea with no > > prospect of getting back to my C++ books for another three weeks... > > When the prophet cannot go to the scripture, let the scripture come > to the prophet ;)Thanks for the quick response. With your help along with the fresh weather (50 kts wind and ~8 m waves right now), the necessary details were shaken loose from the darker corners of my mind... Rune






