函数声明内部或外部的类 [英] Function declaration inside or outside the class

查看:133
本文介绍了函数声明内部或外部的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个试图学习C ++的JAVA开发人员,但我不知道标准函数声明的最佳实践。

I am a JAVA developer who is trying to learn C++, but I don't really know what the best practice is for standard function declarations.

class Clazz
{
 public:
    void Fun1()
    {
        //do something
    }
}

class Clazz
{
public:
    void Fun1();
}

Clazz::Fun1(){
    // Do something
}

我有一种感觉,第二个可读性较差...

I have a feeling that the second one can be less readable...

推荐答案

C ++是面向对象的,因为它支持用于软件开发的面向对象的范例。

C++ is object oriented, in the sense that it supports the object oriented paradigm for software development.

但是,与Java不同,不强迫你类中的定义:声明一个函数的标准C ++方法是只声明一个没有任何类的函数。

However, differently from Java, doesn't force you to group function definitions in classes: the standard C++ way for declaring a function is to just declare a function, without any class.

如果你正在谈论方法声明/标准方式是将声明放在一个包含文件(通常名为 .h .hpp )中, (通常命名为 .cpp .cxx )中。我同意这确实有点烦人,需要一些重复,但它是如何设计的语言。

If instead you are talking about method declaration/definition then the standard way is to put just the declaration in an include file (normally named .h or .hpp) and the definition in a separate implementation file (normally named .cpp or .cxx). I agree this is indeed somewhat annoying and requires some duplication but it's how the language was designed.

对于快速实验和单文件项目,任何东西都会工作...但对于更大

For quick experiments and single file projects anything would work... but for bigger projects this separation is something that is practically required.

请注意,如果你知道Java,那么C ++是一种完全不同的语言...而且它是一种无法学习的语言实验。原因是它是一个相当复杂的语言,有很多不对称和显然是不合逻辑的选择,最重要的是,当你犯了一个错误,没有运行时错误天使保存你像Java ...但有而是有未定义的行为守护进程。

Note that if you know Java then C++ is a completely different language... and it's a language that cannot be learned by experimenting. The reason is that it's a rather complex language with a lot of asymmetries and apparently illogical choices and that, most important, when you make a mistake there are no "runtime error angels" to save you like in Java... but there are instead "undefined behavior daemons".

学习C ++的唯一合理的方法是通过阅读...无论你多么聪明,没有办法你可以猜到什么委员会

The only reasonable way to learn C++ is by reading... no matter how smart you are there is no way you can guess what the committee decided (actually being smart is sometimes even a problem because the correct answer is illogical and a consequence of historical heritage).

只需选择一个好书或两本书,并仔细阅读。

Just pick a good book or two and read them cover to cover.

这篇关于函数声明内部或外部的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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