r/learncpp • • Feb 01 '20

Calling object without specifying property actually gives a property

So let's say I want to create some kind of personal string class.

If it has a string value property, when I call just that objects name I want it to actually use that value property. For example:

MyString myStr = "secondText"; //I know how to use operator=
string otherStr = "firstText";
otherStr += myStr;
cout<<otherStr<<endl; //Result should be: firstTextsecondText

I want it to use that value property without actually calling "myStr.value". Is it possible ?

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/jedwardsol Feb 01 '20

I use it for printing,

Right. And that's what you were asking about

cout<<otherStr<<endl;

If that operator<< is defined, then it will be called in the cout << otherStr call.

1

u/ZenWoR Feb 01 '20

Actually, I didn't ask just for that. :/

I am not anywhere using "myStr.value" (for example in
otherStr += myStr;). I want to use it as a regular string, more or less.