468,512 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

How to overload Subscript operator

The following code results in Segmentation fault

=========================
#include <iostream>
using namespace std;
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
abc *pa = &a[i];
pa->a = 10*i;
pa->b = 10*i;
pa->c = 10*i;
cout << "pa->a[" << i << "]: " << pa->a << endl;
cout << "pa->b[" << i << "]: " << pa->b << endl;
cout << "pa->c[" << i << "]: " << pa->c << endl;
}
return 0;
}
=========================

I know I need to overload subscript operator?
1. I am not sure how to do that. can some one suggest a way?
2. Is there anything else missing, all I want to do is read data, and
later display it?

Aug 9 '06 #1
2 3946

pasa_1 wrote:
The following code results in Segmentation fault

=========================
#include <iostream>
using namespace std;
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
abc *pa = &a[i];
pa->a = 10*i;
pa->b = 10*i;
pa->c = 10*i;
cout << "pa->a[" << i << "]: " << pa->a << endl;
cout << "pa->b[" << i << "]: " << pa->b << endl;
cout << "pa->c[" << i << "]: " << pa->c << endl;
}
return 0;
}
=========================

I know I need to overload subscript operator?
1. I am not sure how to do that. can some one suggest a way?
2. Is there anything else missing, all I want to do is read data, and
later display it?
you probably want the
abc a[i];
outside the for loop as
abc a[10];

Aug 9 '06 #2
pasa_1 schrieb:
>
I know I need to overload subscript operator?
You don't. See below.
class abc{
public:
int a;
int b;
int c;
abc() {
a = 0;
b = 0;
c = 0;
In general case, prefer initialization over assignment:

abc() : a(0), b(0), c(0)
{
}
}
};
int main()
{
for (int i = 0; i < 10; i++)
{
abc a[i];
The size of an array has to be constant. This shouldn't compile unless
your compiler has variable length Arrays as extension (gcc has).
You would want this to be outside the for-loop with size equal to the
maximum subcript you use plus 1:

abc a[10];
abc *pa = &a[i];
[snip]

Here you make a pointer to one past the last element of the array. Using
this pointer is undefined behaviour, giving you a segmentation fault.

--
Thomas
Aug 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Piotre Ugrumov | last post: by
1 post views Thread by Nimmi Srivastav | last post: by
15 posts views Thread by Steve | last post: by
5 posts views Thread by Steve | last post: by
10 posts views Thread by olson_ord | last post: by
51 posts views Thread by Pedro Graca | last post: by
5 posts views Thread by DaVinci | last post: by
6 posts views Thread by josh | last post: by
19 posts views Thread by C++Liliput | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.