C++;-扩展数据类型 有没有一种方法可以在C++中扩展数据类型,就像你可以在JavaScript?

C++;-扩展数据类型 有没有一种方法可以在C++中扩展数据类型,就像你可以在JavaScript?,c++,arrays,methods,char,extend,C++,Arrays,Methods,Char,Extend,我想这是这样的: char data[]="hello there"; char& capitalize(char&) { //Capitalize the first letter. I know that there //is another way such as a for loop and subtract //whatever to change the keycode but I specifically //don't want

我想这是这样的:

char data[]="hello there";
char& capitalize(char&)
{
    //Capitalize the first letter. I know that there
    //is another way such as a for loop and subtract
    //whatever to change the keycode but I specifically 
    //don't want to do it that way. I want there
    //to be a method like appearance.
}

printf("%s", data.capitalize());

这应该以某种方式打印出来。

不,是基于对象的原型。这一概念不适用于任何情况。它们是如此不同的语言,我甚至不能给你一个反例来回答你的问题最接近的方法是使用运算符重载,例如

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

std::string operator!(const std::string& in) {
  std::string out = in;
  std::transform(out.begin(), out.end(), out.begin(), (int (*)(int)) std::toupper);
  return out;
}

int main() {
  std::string str = "hello";
  std::cout << !str << std::endl;
  return 0;
}
#包括
#包括
#包括
#包括
字符串运算符!(常量标准::字符串和输入){
std::string out=in;
std::transform(out.begin()、out.end()、out.begin()、(int(*)(int))std::toupper);
返回;
}
int main(){
std::string str=“hello”;

STD::CUT不是C++,但是你可以做一些事情,比如代码>大写(数据)< /C>。如果数据类型是结构/类,你可以(但不应该)从它继承。对于内置类型,如代码> int <代码>或<代码> char < /代码>(或数组)你真是运气不好。我认为这与基本类型没有多大关系。但是对于类,你可以制作一些java风格的东西,并使用给定的方法toString(),capitalize()实现一个“Printable”接口ecc:/或者你可以制作一个模板或静态函数来传递值。lol谢谢你的帮助,我知道这将是一个挑战。我想知道什么可能是最接近这个解决方案的方法。我想使用一个新类。这是一个很好的答案,可能是最好的解决方案,但我对给它打分持怀疑态度,因为我知道我不想误导别人,但你可能需要一个类字符串(特殊字符串)或类似的东西,在它里面我可以创建任何东西