在这种情况下,通过构造函数或显式函数进行隐式转换的更好的方法是什么?

你好,你怎么样

我正在创建自己的类,String仅将C ++用于学习目的。

我停留在应该做出决定的地方。让我解释一下这件事。

我上课有两种选择。我只在下面发布相关的代码段,因为我不想分散您的注意力。如果为了帮助我,您需要更多信息,我们将很乐意提供。

选项1

class String {
    size_t _length;
    char* _stringHead;
public:
    String(const std::string&);
    String(const char*);
    String(const char);
};
String operator+(String, const String);

const bool operator==(const String, const String);
const bool operator!=(const String, const String);
const bool operator<(const String, const String);
const bool operator<=(const String, const String);
const bool operator>(const String, const String);
const bool operator>=(const String, const String);

选项2

class String {
    size_t _length;
    char* _stringHead;
public:
    //irrelevant part of code in Option 2
    String(const std::string&);
    String(const char*);
    String(const char);
    //irrelevant part of code in Option 2
};
String operator+(String, const String&);

const bool operator==(const String&, const String&);
const bool operator!=(const String&, const String&);
const bool operator<(const String&, const String&);
const bool operator<=(const String&, const String&);
const bool operator>(const String&, const String&);
const bool operator>=(const String&, const String&);

//for std::string
String operator+(String, const std::string&);

const bool operator==(const String&, const std::string&);
const bool operator!=(const String&, const std::string&);
const bool operator<(const String&, const std::string&);
const bool operator<=(const String&, const std::string&);
const bool operator>(const String&, const std::string&);
const bool operator>=(const String&, const std::string&);

String operator+(const std::string&, String);

const bool operator==(const std::string&, const String&);
const bool operator!=(const std::string&, const String&);
const bool operator<(const std::string&, const String&);
const bool operator<=(const std::string&, const String&);
const bool operator>(const std::string&, const String&);
const bool operator>=(const std::string&, const String&);
//for std::string

//the same goes for char* and char
...
//the same goes for char* and char

因此,从选项1选项2规范中可以看出,这里的决定是关于是否使用隐式类型转换(这是在构造函数的帮助下完成的)还是为我希望将String类型用作每种类型的每个实用程序分别键入工作。

就我现在所知,使用第一种方法的好处是易于实现和维护。虽然第二种方法可能会产生更好的性能结果。

我想得到建设性的论据,哪种方法更好,在哪种情况下以及您将使用哪种方法。我认为,我最感兴趣的是第二种方法的性能收益是否合理。

dri

当将参数类型的实例传递给需要类类型的方法时,将使用隐式构造函数创建类类型的实例。这种隐式转换是通过调用类的构造函数完成的。

例如,运行与您的代码相似的代码:

#include <iostream>

class String {
 public:
  String(const std::string& s) {
    std::cout << "called" << std::endl;
  };
};

std::ostream& operator<< (std::ostream& stream, const String& s) {
  return stream;
}

void hello(String s) {
  std::cout << "Hello " << s; // Outputs "called" before "Hello ".
}

int main() {
  std::string s = "world";
  hello(s); // Uses the implicit conversion constructor.
}

因为String每次都必须创建该类的新实例,所以可以预期性能会稍有下降但是,在我看来,这不足以超过其好处:隐式转换可以大大简化类设计器的工作,并使使用类更加容易。

但是,请记住,在某些情况下,如果自动发生转换,团队成员更可能会感到惊讶,而不是因为存在转换而对此感到惊讶。

这是一个例子:

#include <iostream>

class String {
 public:
  String(int size) {};
};

std::ostream& operator<< (std::ostream& stream, const String& s) {
    return stream;
}

void hello(String s) {
  std::cout << "Hello " << s; // Prints "Hello " as no error occurs.
}

int main() {
  hello(10); // It still calls the implicit conversion constructor.
}

在上面的代码中,explicit关键字产生的错误消息可以为某人节省一些调试时间。

在某些情况下,隐式转换有意义:

  1. 该类足够便宜,以至于您不需要关心它是否是隐式构造的。
  2. 一些类在概念上与它们的参数相似,例如std::string反映了与const char*它可以隐式转换的概念相同的概念,因此隐式转换很有意义,如此处的情况。
  3. 如果禁用了隐式转换,则某些类将变得更加令人讨厌。考虑std::string每次要传递字符串文字时,都必须显式构造一个

在某些情况下,隐式转换的意义不大:

  1. 施工是昂贵的。
  2. 从概念上讲,类与它们的参数非常不同。考虑带有String和的示例int
  3. 施工可能会产生有害的副作用。例如,一个AnsiString类不应从隐式构造UnicodeString,因为Unicode到ANSI的转换可能会丢失信息。

因此,在您的特殊情况下,我的建议是使用转换,因为它很有意义,因为您的类非常相似,std::string并且可以最大程度地减少代码重复,但是在将来,请使用带有思想的隐式转换

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在这种情况下,“隐式匿名类参数”是什么意思?

为什么在这种情况下隐式转换不起作用?

在这种情况下如何避免显式递归?

C ++如何在不进行隐式转换的情况下模拟函数?

显式构造函数仍在进行转换

为什么对隐式和显式删除的move构造函数进行不同的处理?

在传递给参数时,为什么不能在给定合适的构造函数的情况下隐式构造对象?

如何在不显式调用显式构造函数的情况下初始化映射?

通过方法/构造函数参数隐式与隐式使用

在这种情况下,函数式编程效率较低吗?

为什么在这种情况下使用工厂构造函数

为什么在这种情况下没有调用最合适的构造函数?

在这种情况下,AVG()函数的正确用法是什么?

在这种情况下,使用IF或SWITCH或“动态函数名称”的最佳实践是什么

在没有声明符的情况下显式调用构造函数

对于默认构造函数,未定义隐式超级构造函数Num()。必须定义一个显式构造函数,这背后的逻辑是什么

C ++ 17:显式转换函数与显式构造函数+隐式转换-规则是否已更改?

为什么这不隐式转换为转换构造函数?

隐式构造函数调用还是什么?

C ++隐式默认构造函数的意义是什么?

为什么在这种情况下不能使用隐式?

为什么在没有显式或隐式定义的情况下可以使用“ >> =”?

隐式与显式删除副本构造函数

隐式与显式默认构造函数调用

C ++ 11:默认构造函数:隐式还是显式?

C ++隐式和显式继承构造函数调用

隐式定义与显式声明的构造函数

std :: bind的返回类型隐式转换为两个不同的显式构造函数

模棱两可的重载,隐式转换和显式构造函数