Re: Simple List Access by Index
On 6 Dez., 03:34, serkan.ka...@gmail.com wrote:
I've created these simple Node and List classes, but I want to be able
to do something like this: L[0] = 5; L[1] = 10; L[2] = 20; etc.... How
would I need to overload my operator= in the List class? I'm
relatively new to C++.
#include <iostream>
using namespace std;
class Node
{
friend ostream& operator<<(ostream& out, const Node& N)
{
cout << N.data << " --> ";
}
Two fixes are necessary: First, you probably want to use the
function parameter 'out' instead of cout (otherwise, what would
be the reason to provide this parameter?), and second you need
to return something here, most reasonably you want to return out.
int data;
Node* next;
public:
Node() {}
Note that this constructor leaves the members with uninitialized
values - probably not something you really want...
Node(int d, Node* n)
{
data=d; next=n;
}
~Node() {}
void setData(int d) { data=d; }
int getData() { return data; }
void printData() { cout << data << endl; }
int operator=(const Node& N) { return N.data; }
};
I recommend to obey const-correctness: Functions
like getData and printData should be declared as
const members functions. The signature of operator=
is quite unusual, but feasible.
class List
{
Node N;
Node* top;
int num_;
public:
List(int num): num_(num) //create list
{
top = new Node[num];
}
~List() //destroy list
{
delete[] top;
}
Node& operator[](int i) {return top[i]; }
void operator=(Node& N)
{
//code to be placed here...
}
void print()
{
List L(num_);
for(int i=0; i<num_; i++)
{
cout << L[i];
}
}
};
int main()
{
List L(10); //list of ten nodes
//assign values to nodes here...
L.print(); //prints the list
cout << endl;
system("pause");
return 0;
}
You don't need void operator=(Node& N), because
your Node& operator[] does already provide the
functionality you ask for. Just add e.g. the line
L[2] = Node();
to your main function code (after creation of L)
and you will see that it compiles. What happens
here is the following: The expression L[2] returns
a reference to Node (look at the return type, which
is Node&). This lvalue (let's call it x) is now the
left hand side argument of the actual assignment
expression as if one would have written:
x = Node();
So, just remove void operator=(Node& N), it just
makes the interface of your class inscrutable.
One further tip regarding const-correctness again:
Since your list sometimes might only be used for
read-only operations you should add one further
operator[] overload:
class List
{
....
public:
Node& operator[](int i) {return top[i]; }
const Node& operator[](int i) const {return top[i]; }
....
};
The second overload allows you to simply read
elements in a function like this [I assume that you
also will provide an accessor size() to get the
number of all elements in the list]:
void print_all(const List& li) {
for (int i = 0, sz = li.size(); i < sz; ++i) {
std::cout << li[i];
}
}
Currently this code is not well-formed (ignore the
missing size member for a second), because the non-const
operator[] cannot be invoked for a const List&.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]