【问题标题】:c++ code equivalent since c#? [closed]自 c# 以来的 c++ 代码等效? [关闭]
【发布时间】:2011-08-18 18:41:19
【问题描述】:

您如何在 C++ 中编写此代码?

    public bool returnboolean(bool x) 
    { return x; } 
    public double sum(double one, double two) 
    { return one+two; } 
    public string yourname(string name) 
    { return "hello "+yourname; } 

我去哪里写代码?在 *.h?还是在 *.cpp 中?

这两个班级的目标是什么?因为后来我添加了一个类,它们创建了 2 个类。

如何创建实例?

例如在c#中:

person p1 = new person();

c++ 中 this 的等价物是什么??

【问题讨论】:

  • 这里仍然很难说清楚要问什么。
  • 天哪 @angel - 你真的需要先阅读 C++ 的基本入门书,并就你不理解的内容提出问题,而不是期望这里的人为你写一个基本的入门书。
  • 当你说“那两个类”时,你指的是什么? .h 和 .cpp?它们是文件,而不是类。
  • 我不觉得这个网站应该是你的“教程”。如果您有非常具体的编程问题,请提出。问“请帮我转换下面的代码,因为我不懂C++”有点过分了。

标签: c# c++ c visual-c++


【解决方案1】:
#include <string>

public:
bool returnboolean(bool x)
    {
        return x;
    }
    double sum(double one, double two)
    {
        return one+two;
    }
    std::string yourname(std::string name)
    {
        return "hello " + name;
    }

【讨论】:

  • @Seth:是的,你可以 - 看这里ideone.com/hzq2a#
  • 是的,这就是我删除评论的原因。为什么这行得通?好像您正在向char const[7] 添加一个字符串。
  • @Seth:因为 + 运算符被重载以同时获取 std::string + std::string 和 char* + std::string。
  • Ohhhhhhhhhh 我明白了,因为您必须在运算符重载中至少有一个用户定义的类型,但不一定要两个,并且用户定义的类型不必在左边。亲爱的,非常感谢。 +1
  • @Seth:看这里:cplusplus.com/reference/string/operator+string operator+ (const char* lhs, const string&amp; rhs);
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 2012-05-30
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多