operators overloading in c++ , I need to store words intead of a single letter?
I need some help.
this is my function
1 2 3 4 5 6 7
|
class teacher
{
public:
char &operator[](int i);
protected:
char* contents;
};
|
.cpp
1 2 3 4
|
char & teacher::operator[](int i)
{
return contents[i];
}
|
main
1 2 3 4
|
teacher s1;
cout<<"Enter names: "<<endl;
cin>>s1[1];
cout<<s1[1];
|
If I enter a word, it only returns the first character, I don't know why if I'm using a char*
You are dereferencing the pointer.
However, I doubt that it points to something valid
1 2 3 4 5 6
|
struct person{
std::string name;
};
person teacher;
std::cin >> teacher.name;
|
Topic archived. No new replies allowed.