结构体里面也有构造函数的吧啊???

2025-05-12 15:50:44
推荐回答(1个)
回答(1):

有的,其实可以说结构体和类一样,只不过结构体中定义的变量或者方法的默认访问属性是public的,而类是private的。

struct tagSample
{
    tagSample()
    {
        a=b=0;
    }
    
    int Add()
    {
        return a+b;
    }
    
    int Add(int na,int nb)
    {
        a=na;b=nb;
        return Add();
    }
    
    int a;
    int b;
};