C ++编程风格 [英] C++ programming style

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

问题描述

我是一个老的(但不是太老)Java程序员,决定学习C ++。但我已经看到了很多C ++编程风格,很好,只是死了丑陋!

I'm an old (but not too old) Java programmer, that decided to learn C++. But I have seen that much of C++ programming style, is... well, just damn ugly!

所有的东西,把类定义在头文件,方法在不同的源文件中 - 调用函数无处不在,而不是使用在类内方法。所有这一切似乎...错了!

All that stuff of putting the class definition in a header file, and the methods in a different source file- Calling functions out of nowhere, instead of using methods inside classes. All that just seems... wrong!

最后,有没有任何理由让我继续这个屠杀到OOP,以及任何东西,良好和正义编程,或者我可以忽略老式的C ++约定,并使用我好的Java编程风格?

So finally, is there any reason for me to continue with this massacre to the OOP, and anything that is good and righteous in programming, or can I just ignore that old-fashioned C++ conventions, and use my good Java programing style?

顺便说一句,我在学习C ++,因为我想

By the way I'm learning C++, because I want to do game programing.

这是一个例子:

在C ++网站中,我发现了一个Windows实现: / p>

In an C++ website I found a Windows implementation:

class WinClass
{
    public:

        WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst);
        void Register ()
        {
            ::RegisterClass (&_class);
        }

    private:

        WNDCLASS _class;
};

该类位于头文件和构造函数中:

That class is located in a header file and the constructor:

WinClass::WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst)
{
    _class.style = 0;
    _class.lpfnWndProc = wndProc;  // Window Procedure: mandatory
    _class.cbClsExtra = 0;
    _class.cbWndExtra = 0;
    _class.hInstance = hInst;           // Owner of the class: mandatory
    _class.hIcon = 0;
    _class.hCursor = ::LoadCursor (0, IDC_ARROW); // Optional
    _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Optional
    _class.lpszMenuName = 0;
    _class.lpszClassName = className;   // Mandatory
}

位于.cpp源文件。

我可以做的是:

class WinClass
{
    public:
        WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst)
        {
            _class.style = 0;
            _class.lpfnWndProc = wndProc;  // Window Procedure: mandatory
            _class.cbClsExtra = 0;
            _class.cbWndExtra = 0;
            _class.hInstance = hInst;           // Owner of the class: mandatory
            _class.hIcon = 0;
            _class.hCursor = ::LoadCursor (0, IDC_ARROW); // Optional
            _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Optional
            _class.lpszMenuName = 0;
            _class.lpszClassName = className;   // Mandatory
        }

        void Register ()
        {
            ::RegisterClass (&_class);
        }

    private:
        WNDCLASS _class;
};

现在构造函数在其类中。

And now the constructor is inside its class.

推荐答案

除了别人在这里说的话,还有更重要的问题:

In addition to what others have said here, there are even more important problems:

到更长的编译时间和更大的
目标文件大小。

1) Large translation units lead to longer compile times and larger object file sizes.

2)循环依赖!这是大的。它几乎
总是通过拆分头和源:

2) Circular dependencies! And this is the big one. And it can almost always be fixed by splitting up headers and source:

// Vehicle.h
class Wheel {
    private:
        Car& m_parent;
    public:
        Wheel( Car& p ) : m_parent( p ) {
            std::cout << "Car has " << m_parent.numWheels() << " wheels." << std::endl;
        }
};

class Car {
    private:
        std::vector< Wheel > m_wheels;
    public:
        Car() {
            for( int i=0; i<4; ++i )
                m_wheels.push_back( Wheel( *this ) );
        }

        int numWheels() {
            return m_wheels.size();
        }
}

无论以什么顺序,总是缺少对方的定义,甚至使用向前的声明它不会工作,因为在函数体中使用关于每个类的符号的细节。

No matter what order you put these in, one will always be lacking the definition of the other, even using forward declarations it won't work, since in the function bodies are using specifics about each class's symbol.

但如果你将它们分成适当的.h和.cpp文件,并使用向前声明,它将满足编译器:

But if you split them up into proper .h and .cpp files and use forward declarations it will satisfy the compiler:

//Wheel.h
//-------
class Car;

class Wheel {
private:
    Car& m_parent;
public:
    Wheel( Car& p );
};

//Wheel.cpp
//---------
#include "Wheel.h"
#include "Car.h"

Wheel::Wheel( Car& p ) : m_parent( p ) {
        std::cout << "Car has " << m_parent.numWheels() << " wheels." << std::endl;
}

//Car.h
//-----
class Wheel;

class Car {
private:
    std::vector< Wheel > m_wheels;
public:
    Car();
    int numWheels();
}

//Car.cpp
//-------
#include "Car.h"
#include "Wheel.h"

Car::Car() {
        for( int i=0; i<4; ++i )
            m_wheels.push_back( Wheel( *this ) );
}

int Car::numWheels() {
        return m_wheels.size();
}

现在,实际上需要知道关于第二个类的细节的代码可以包括

Now the code that actually has to know specifics about the second class can just include the header file which doesn't need to know specifics about the first class.

头文件只提供声明,而源文件提供 >定义。或者另一种说法:标题告诉你有什么(什么符号有效使用)和source告诉编译器符号实际做什么。在C ++中,你不需要任何有效的符号来开始使用它。

Headers just provide the declarations while source files provide the definitions. Or another way to say it: Headers tell you what is there (what symbols are valid to use) and source tells the compiler what the symbols actually do. In C++ you don't need anything more than a valid symbol to begin using whatever it is.

相信C ++有这个成语的原因,因为如果你'你会为自己下了很多头痛。我知道:/

Trust that C++ has a reason for this idiom, because if you don't you will make a lot of headaches for yourself down the line. I know :/

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

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