从C#学习C ++ [英] Learning c++ from c#

查看:90
本文介绍了从C#学习C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下c#类文件的c ++等价物是什么,分为头文件和cpp文件:

What is the c++ quivalant of the following c# class file, separated into header file and cpp file:

class hello
{
     private string _fname, _lname;
     public hello(string fname, string lname)
     {
           setName(fname, lname);
     }
     public setName(string fname, string lname)
     {
          _fname = fname;
          _lname = lname;
     }
     public getName()
     { 
          return _fname + " " + _lname;
     }
}

推荐答案

一个不错的第一个选择是:

A good first stab would be:

// Class definition in name.h

#include <string>

class name
{
    public:
        name( const std::string &first, const std::string &last );
        std::string full_name() const;

    private:
        std::string first_name_, last_name_;
};

// Class implementation in name.cpp

name::name( const std::string &first, const std::string &last ) :
    first_name_( first ), last_name_( last )
{
}

std::string name::full_name() const
{
    return first_name_ + '' '' + last_name_;
}



这里要考虑两件事...

-为什么我要摆脱C#实现中使用的setter方法? (另一种思考方式是为什么值类应该是常量?)
-为什么功能不是虚拟的? (我认为这与C#和Java中的final等效,但是可能会出错,请不要使用C#)

两者都有很好的理由,而查看您的C#类使我认为它们可能不是C#惯用的.

干杯,

Ash



Two things to consider here...

- why did I get rid of the setter method you had in your C# implementation? (another way to think about it is why should value classes be constant?)

- why are the functions non-virtual? (I think that''s the equivalent of final in C# and Java, might be wrong though, don''t use C#)

There are good reasons for both of them, which looking at your C# class makes me think they might not be idiomatic in C#.

Cheers,

Ash


首先,上面的c#代码将永远不会编译,因为未指定setName和getName方法的返回类型. C#代码的修改版本
First of all above c# code will never compile, because you are not specified the return type of setName and getName method.
modified version of C# code
class hello
{
    private string _fname, _lname;
    public hello(string fname, string lname)
    {
        setName(fname, lname);
    }
    public void setName(string fname, string lname)
    {
        _fname = fname;
        _lname = lname;
    }
    public string getName()
    {
        return _fname + " " + _lname;
    }
}





//hello.h - This class provides declaration of class Hello
#include <string>
class Hello
{
public:
    Hello(void);//default constructor
    Hello( const std::string& strFirstName,  const std::string& strLastName);
    void SetName( const std::string& strFirstName,  const std::string& strLastName);
    std::string GetName();
private:
    std::string m_strFirstName;
    std::string m_strLastName;
};





//hello.cpp - This class provides implementation of class Hello
#include "Hello.h"
Hello::Hello(void)
{
}
Hello::Hello( const std::string& strFirstName,  const std::string& strLastName):
                m_strFirstName(strFirstName), m_strLastName( strLastName )
{

}
void Hello::SetName( const std::string& strFirstName,  const std::string& strLastName)
{
    m_strFirstName = strFirstName;
    m_strLastName = strLastName;
}
std::string Hello::GetName()
{
    return m_strFirstName + " " + m_strLastName;
}


这篇关于从C#学习C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆