I have only been using c++ for about a month now and I was wondering if it were possible to create your own custom functions for something like a string.
For example to find the size of a string you do stringname.size()
But what if I wanted to make a function like stringname.customfunction()
were I get to decide what customfunction()
does. I want to do this so I can create a .toLower() function that converts a whole string to lower. I'm sorry if this is a wierd question but any help would be greatly appreciated.

- 465
- 2
- 16
4 Answers
Yes you can create custom functions, but not directly as member functions of an existing class.
The simplest and therefore best approach is to just create freestanding functions.
That means that you'll call it like customfunction( s )
.

- 142,714
- 15
- 209
- 331
-
so instead of stringname.customfunction() the preferred way is customfunction(string a). Alright I think I understand now. – Nicholas Ramsay Nov 13 '15 at 08:32
You can achieve this by creating you own string class, but as I've undertood you mean calling something like "THE String".toLower()
as in python for example. This is actually imposible in c++ as far as I know.
Once again your best chance is do your own class so you can call someting like MyString("THE String").toLower()
. Or just create a function toLower that takes an string and return an string toLower("THE String")

- 40,134
- 6
- 50
- 93
You can't directly but you should be able to emulate this by inheriting from std::string
(or basic_string<char>
):
class mystring : public std::string
{
public:
void customfunction() { /* ... */ }
};
Note: However doing so is a bad idea. In addition to the link that @Cheers and hth. - Alf mentions, Herb Sutter discusses the overly wide interface of std::string and widening it even further is a bad idea.

- 1
- 1

- 16,230
- 17
- 74
- 137
-
**−1** Deriving from `std::string` in order to augment its already bloated interface is widely regarded as Bad Practice™. Doing such derivation properly is non-trivial, e.g. one needs to get the constructors right (easier now with constructor inheritance, but still), while a simple support function is trivial. I normally only downvote technically incorrect answers, but this one is really bad advice for beginners. – Cheers and hth. - Alf Nov 13 '15 at 09:31
-
SO was very slooooow here for a while, but here's a link to more info: (http://stackoverflow.com/questions/6006860/why-should-one-not-derive-from-c-std-string-class). The accepted and heavily upvoted answer is ungood – not uncommon on SO – but there are good points made. – Cheers and hth. - Alf Nov 13 '15 at 10:44
-
I didn't say it was best practice - I merely presented it as a way of doing it. Where I work we have 6 different string classes, two of which are home grown. I will update the answer to note that it is a bad idea. – graham.reeds Nov 13 '15 at 12:14
This may be a bit advanced and highly despised, but you could change the actual source files. I highly suggest YOU DO NOT do this.
You should just make a simple function void toLower(std::string str);
that would convert it for you.
(You would call this function with toLower(str);
.)

- 591
- 2
- 12
- 24