始终初始化成员变量怎么解决

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;



class CBase1
{
public:
    CBase1() {}
    CBase1( int a)
        :a(a)
    {
        cout << "base1 structure..." << endl;
    }
    ~CBase1()
    {
        cout << "base1 destructure..." << endl;
    }
    void print()
    {
        cout << "a=" << a << endl;
    }
protected:
    int a;
};
class CBase2
{
public:
    CBase2() {}
    CBase2(int b)
        :b(b)
    {
        cout << "base2 structure..." << endl;
    }
    ~CBase2()
    {
        cout << "base2 destructure..." << endl;
    }
    void print()
    {
        cout << "b=" << b << endl;
    }
protected:
    int b;
};
class CDerive : public CBase1, public CBase2 
{
public:
    
    CDerive() 
    {
        cout << "derive structure..." << endl;
    }
    ~CDerive()
    {
        cout << "derive destructure..." << endl;
    }
    void print()
    {
        CBase1::print();
        CBase2::print();
        b1.print();
        b2.print();
        cout << "c=" << c << endl;
    }
private:
    CBase1 b1;
    CBase2 b2;
    int c;
};

int main()
{
    CDerive d;
    d.print();
}



运行结果及报错内容

警告 C26495 未初始化变量 CBase1::a。始终初始化成员变量(type.6)。
未初始化变量 CBase2::b。始终初始化成员变量(type.6)。

警告 C26495 未初始化变量 CDerive::c。始终初始化成员变量(type.6)。

我的解答思路和尝试过的方法
我想要达到的结果

首先,这个警告是值得重视的,如果你调用了不带参数构造函数CBase1(),请问你的成员变量的初始值是多少,因为你没有赋值过,它可能是随机值。所以,关键看你希望它是多少,例如,你希望调用CBase1()时,成员变量a的值是0,你可以这样写:

  CBase1() :a(0)
{
}

同理,CBase2的无参构造函数。
在你的代码中,CDerive确实会调用父类的无参构造函数。

程序没什么大问题,只是很多编译器会给你警告,未初始化。
是因为你的默认构造中并没有初始化成员,你可以在默认构造里也加上输出看看调用的过程。

#include<iostream>
using namespace std;
 

class CBase1
{
public:
    CBase1() {a=0;cout << "base1 structure1..." << endl;}
    CBase1( int a)
        :a(a)
    {
        cout << "base1 structure2..." << endl;
    }
    ~CBase1()
    {
        cout << "base1 destructure..." << endl;
    }
    void print()
    {
        cout << "a=" << a << endl;
    }
protected:
    int a;
};
class CBase2
{
public:
    CBase2() {b=0; cout << "base2 structure1..." << endl;}
    CBase2(int b)
        :b(b)
    {
        cout << "base2 structure2..." << endl;
    }
    ~CBase2()
    {
        cout << "base2 destructure..." << endl;
    }
    void print()
    {
        cout << "b=" << b << endl;
    }
protected:
    int b;
};
class CDerive : public CBase1, public CBase2 
{
public:
    
    CDerive() 
    {
        c = 0;
        cout << "derive structure..." << endl;
    }
    ~CDerive()
    {
        cout << "derive destructure..." << endl;
    }
    void print()
    {
        CBase1::print();
        CBase2::print();
        b1.print();
        b2.print();
        cout << "c=" << c << endl;
    }
private:
    CBase1 b1;
    CBase2 b2;
    int c;
};
 
int main()
{
    CDerive d;
    d.print();
}

你用的编译器是什么编译器?
我的环境是win10,vs2019编译没有警告
针对你的警告,我显示创建了CDerive(int a, int b)构造函数,然后在初始化列表里调用CBase1(int a),CBase2(int b)构造函数,这样基类的每一个变量都初始化了
如果你能hold住所有代码,也没啥大关系,没初始化的变量,系统会默认给个随机值,最好还是都初始化一个值,要不有许多隐藏问题

#include<iostream>
using namespace std;



class CBase1
{
public:
    CBase1() {}
    CBase1(int a)
        :a(a)
    {
        cout << "base1 structure..." << endl;
    }
    ~CBase1()
    {
        cout << "base1 destructure..." << endl;
    }
    void print()
    {
        cout << "a=" << a << endl;
    }
protected:
    int a;
};
class CBase2
{
public:
    CBase2() {}
    CBase2(int b)
        :b(b)
    {
        cout << "base2 structure..." << endl;
    }
    ~CBase2()
    {
        cout << "base2 destructure..." << endl;
    }
    void print()
    {
        cout << "b=" << b << endl;
    }
protected:
    int b;
};
class CDerive : public CBase1, public CBase2
{
public:

    CDerive(int a, int b):CBase1(a), CBase2(b)
    {
        cout << "derive structure..." << endl;
        c = 666;
    }
    ~CDerive()
    {
        cout << "derive destructure..." << endl;
    }
    void print()
    {
        CBase1::print();
        CBase2::print();
        b1.print();
        b2.print();
        cout << "c=" << c << endl;
    }
private:
    CBase1 b1;
    CBase2 b2;
    int c;
};


int main()
{
    int a = 1, b = 2;
    CDerive d(a, b);
    d.print();
}