overloading the [] operator for a string class

A

andrew browning

here is the constructor:
mystring(const char str[] = "");

here are the private variables:
value_type *data; //Pointer to partially filled dynamic array

size_type used; //How much of array is being used

size_type capacity; //Current capacity of the mystring

here is the function:
const char& operator [](size_type index) const {return data[index];}

i get a segmentation error when i call it from main. can someone
please help... i'm having a time with this thing!
 
P

Phlip

andrew said:
here is the constructor:
mystring(const char str[] = "");

here are the private variables:
value_type *data; //Pointer to partially filled dynamic array

size_type used; //How much of array is being used

size_type capacity; //Current capacity of the mystring

here is the function:
const char& operator [](size_type index) const {return data[index];}

i get a segmentation error when i call it from main. can someone
please help... i'm having a time with this thing!

You should post more code, such as the contents of the constructor.

Generally, data must point to an array, such as one returned by new
char[used]. If data is garbage, that would generally crash at data[index],
but you did not post enough for us to tell.
 
V

Victor Bazarov

andrew said:
here is the constructor:
mystring(const char str[] = "");

here are the private variables:
value_type *data; //Pointer to partially filled dynamic array

size_type used; //How much of array is being used

size_type capacity; //Current capacity of the mystring

here is the function:
const char& operator [](size_type index) const {return data[index];}

i get a segmentation error when i call it from main. can someone
please help... i'm having a time with this thing!

This is covered by FAQ 5.8. Please take time to read it before your
next posting. Here it is: http://www.parashift.com/c++-faq-lite/

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top