0

As a beginner in C++, going through a sample program, While I do understand the concept of classes, I'm not sure how the 'operator' is integrated with the classes in the code below.

So if a class is defined in the format below.

class String{
private:
    // this is the data member
    char *ptr;
public:
    // this is one constructor 
    String();
    // This is another constructor
    String(char *s);

    // Now This is the line that I dont understand. What is the    
     //purpose of the line below when declaring a class. 

    operator char*() {return ptr;}
};

What I don't understand is, how does the operator function? I believe it is linked to the String(Char *s) constructor. But I'm not able to make the link. Any help would be appreciated.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Big Head
  • 49
  • 6
  • Also related/dupe: http://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const – NathanOliver Sep 30 '16 at 12:43
  • Yes there is: http://stackoverflow.com/questions/4421706/operator-overloading/16615725#16615725 Did you miss the big, fat "Conversion Operators" part of it? – Sam Varshavchik Sep 30 '16 at 12:44
  • @SamVarshavchik Yes I did (my mistake) - But if someone comes here, he probably does not know that this is a conversion operator, so he will have to go through the bunch of answers of the duplicate, and probably stop before reaching the relevant part. I think Nathan's duplicate would be better (or at least add a direct link to the answer on conversion operator on your dupe). – Holt Sep 30 '16 at 12:49
  • @Holt Thanks. Yeah. i didn't know it was a conversion operator. Thanks for clarification. Understood now. – Big Head Sep 30 '16 at 14:39

0 Answers0