C ++,构造函数后的冒号是什么意思? [英] C++, What does the colon after a constructor mean?

查看:212
本文介绍了C ++,构造函数后的冒号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项:

变量在构造函数中冒号后

单一群体的重要性:"在C ++中

C ++构造函数语法问题(noob)


我在这里有一些C ++代码:

  class demo 
{
private:
unsigned char len,* dat;

public:
demo(unsigned char le = 5,unsigned char default):len(le)
{
dat = new char [len];
for(int i = 0; i <= le; i ++)
dat [i] = default;
}

void〜demo(void)
{
delete [] * dat;
}
};

class newdemo:public demo
{
private:
int * dat1;

public:
newdemo(void):demo(0,0)
{
* dat1 = 0;
return 0;
}
};我的问题是,:len(le)是什么?


$ b c $ c>和:demo(0,0)调用?



是否与继承有关?

解决方案

有人说,这是一个初始化列表。您可以将它用于两件事:


  1. 调用基类构造函数



  2. 对于case#1,我假定你理解继承(如果不是这样,让我知道评论)。所以你只是调用你的基类的构造函数。



    对于情况#2,问题可能会问:为什么不只是在构造函数?初始化列表的重要性对于 const 成员特别明显。例如,看看这种情况,其中我想基于构造函数参数初始化 m_val

      class Demo 
    {
    Demo(int& val)
    {
    m_val = val;
    }
    private:
    const int& m_val;
    };

    根据C ++规范,这是非法的。我们不能在构造函数中更改 const 变量的值,因为它被标记为const。所以你可以使用初始化列表:

      class Demo 
    {
    Demo(int& val) m_val(val)
    {
    }
    private:
    const int& m_val;
    };

    这是唯一可以更改const成员变量的时间。



    除了使用它来初始化一个类成员的引用之外,它也是唯一的方法。 const 成员变量,它似乎一般被接受为初始化变量的方式,所以其他程序员清楚地看到你的代码。


    Possible Duplicates:
    Variables After the Colon in a Constructor
    Importance of a singlecolon “:” in C++
    C++ constructor syntax question (noob)

    I have some C++ code here:

    class demo 
    {
    private:
        unsigned char len, *dat;
    
    public:
        demo(unsigned char le = 5, unsigned char default) : len(le) 
        { 
            dat = new char[len];                                      
            for (int i = 0; i <= le; i++)                             
                dat[i] = default;
        }
    
        void ~demo(void) 
        {                                            
            delete [] *dat;                                           
        }
    };
    
    class newdemo : public demo 
    {
    private:
        int *dat1;
    
    public:
        newdemo(void) : demo(0, 0)
        {
         *dat1 = 0;                                                   
         return 0;                                                    
        }
    };
    

    My question is, what are the : len(le) and : demo(0, 0) called?

    Is it something to do with inheritance?

    解决方案

    As others have said, it's an initialisation list. You can use it for two things:

    1. Calling base class constructors
    2. Initialising member variables before the body of the constructor executes.

    For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.

    For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialise m_val based on the constructor parameter:

    class Demo
    {
        Demo(int& val) 
         {
             m_val = val;
         }
    private:
        const int& m_val;
    };
    

    By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initialisation list:

    class Demo
    {
        Demo(int& val) : m_val(val)
         {
         }
    private:
        const int& m_val;
    };
    

    That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.

    Outside of using it to initialise const member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.

    这篇关于C ++,构造函数后的冒号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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