c++ 构造函数问题
PHP中文网
PHP中文网 2017-04-17 13:41:42
0
6
406

有这样一个构造函数,工作正常,用途就是把参数都放到成员里,

是否有更简洁的写法?

函数内容空着看起来好奇怪。

foo::foo(mytype1 a, mytype2 b, mytypec c) :
m_a(a),
m_b(b),
m_c(c)
{
}
PHP中文网
PHP中文网

认证0级讲师

reply all(6)
左手右手慢动作

There is no concise way to write it. .
Usually I write like this

foo::foo(t1 a, t2 b, t3 c)
    :m_a(a)
    ,m_b(b)
    ,m_c(c)
{}

Unlike perl6, you can use named parameters directly~~

洪涛

As far as I know, no.

Ty80

Note that the constructor body is not used for initialization. In fact, the initialization of data members is completed before the function body.

If some data members do not have a default constructor, you do not need to initialize the list. Assigning values ​​in the function body will definitely not work.

The same applies to destructors. The call to the destructor corresponding to the data member is also completed after the function body.

Of course, writing this way is a bit painful, but you can make the compiler support a certain way of writing. But how to make it compatible with old code? Is it possible that the new way of writing may conflict with other code forms? Will the new writing method cause great difficulties for the compiler? After all, it’s hard enough to implement the front-end of a C++ compiler these days!

巴扎黑

Except for the following four situations:

  1. Initialize a reference member.

  2. Initialize a const number.

  3. When calling the constructor of a base class and it has a set of parameters.

  4. When calling the constructor of a member class and it has a set of parameters. (From In-depth exploration of the C++ object model)

The effect of using list initialization and assignment is not much different. So if you are not used to the way the constructor initializes the list, you can initialize it in the following way:

foo::foo(mytype1 a, mytype2 b, mytypec c)
{
    m_a = a;
    m_b = b;
    m_c = c;
}

Of course, pay attention to the first four situations.

PHPzhong

There is no better way to write it, this is the style~

Many times we need this writing method, even for small classes.

class Father
{
private:
    class _Son
    {
    public:
        int i1, i2, i3, i4;
    }
public:
    auto func()
    {
        _Son s;
        s.i1 = ......;
    }
};

If you really don’t want to bother, and this class is private, then you can use it as a C struct (such as _Son). There is no need for a constructor. You’d better mark it as struct. , is also a matter of style; otherwise, you lose elegance, style and encapsulation ~ the consequence is that you have to manually assign a value to an object of this class or write an ugly init() method every time you need an object of this type.

Ty80

This method uses an initialization list to initialize members. If members are assigned one by one in the structure, the assignment process is to first call the default constructor (default) and then perform the assignment. This is not as efficient as using the initialization list.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template