🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.leda
Article #110

Using d_array as a private data member within a user-defined class.

#110
From: J B Gavin
Date: Sun, 01 Oct 1995 18:27
102 lines
3026 bytes
(This is my second attempt at posting this query.
 Apologies if you get two copies)

Hi,

I want to put a d_array data member into a user-defined class.

The problem is that a function which accepts
a object of this class, will only accept a NON-const object.
This is because the d_array class (p 64 of my manual) only
has a NON-const version of the member function operator[].

Questions
---------
1/2)
Is there some way to define a non-const version of the
function operator[] for d_array?
2/2)
Alternatively, is there some way of accessing the data 
in a d_array without having to use the function operator[]?

I include a sample program to clarify what I mean.
The program compiles but only if I leave out `const'
on several lines (see below).

Thank you for you help.

Regards,

John.

================================

#include <LEDA/stream.h>
#include <LEDA/d_array.h>

	class
myClass
{
private:
  d_array<int, int> myDict_;

public:

	// want `const myClass& c' but d_array won't allow `const c.myDict_[i]'
	friend ostream& operator<<(ostream& O, const myClass& c) 
	{	int i;
		forall_defined(i, c.myDict_) 
		  O << i << ' ' << c.myDict_[i] << endl;// LINE 17 oper[] non-const
		return O;
	}

	// want `const myClass& c' but d_array won't allow `const c.myDict_[i]'
   void myCopy(const myClass& c) // const myClass& c wanted
	{	int i;
		forall_defined(i, c.myDict_) 
		  myDict_[i] = c.myDict_[i]; // LINE 25 operator[] non-const
	}

	myClass(const myClass& c) // const myClass& c wanted
	{	myCopy(c); }
	myClass& operator=(const myClass& c) // const myClass& c wanted
	{ 	myCopy(c);
		return *this;
	}
	void add(const int i = 3, const int j = 5){ myDict_[i] = j; return;}
	myClass(const int i = 1, const int j = 2){ myDict_[i] = j; }
	~myClass(){ myDict_.clear(); }

}; // myClass


main()
{	myClass myObj(20,20);
	myObj.add(1,2);
	myObj.add(2,4);
	cout << myObj;

	return 0;
}
/* COMPILATION ERRORS

// ERROR FOR LINE 17
/home/jbg/cc/leda/31/incl/LEDA/d_array.h: In function 
  `class ostream & operator <<(class ostream &, const class myClass &)':
  /home/jbg/cc/leda/31/incl/LEDA/d_array.h:77: 
  non-const member function `d_array<int,int>::operator [](int)'
  oper2.cc:17: called for const object at this point in file

// ERROR FOR LINE 25
/home/jbg/cc/leda/31/incl/LEDA/d_array.h: In method 
  `void myClass::myCopy(const class myClass &)':
  /home/jbg/cc/leda/31/incl/LEDA/d_array.h:77: 
  non-const member function `d_array<int,int>::operator [](int)'
  oper2.cc:25: called for const object at this point in file
*/

/* OUTPUT FROM PROGRAM WHEN CONST IS REMOVED
1 2
2 4
20 20
*/
Technical Details
Message-ID: <DFs8Lv.3DK.B.mary@bath.ac.uk>
Path: arc1-ca.pugleaf.net!bath.ac.uk!mapjbg

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.c++.leda
Path: arc1-ca.pugleaf.net!bath.ac.uk!mapjbg
From: mapjbg@bath.ac.uk (J B Gavin)
Subject: Using d_array as a private data member within a user-defined class.
Message-ID: <DFs8Lv.3DK.B.mary@bath.ac.uk>
Organization: School of Mathematical Sciences, University of Bath, UK
Date: Sun, 1 Oct 1995 18:27:30 GMT