Page 1 of 1

variabila const

Posted: 14 Sep 2019, 21:42
by vili_c++
Va salut!
De ce o variabila declarata constanta poate fi initializata utilizand lista de instructiuni si nu poate fi initializata utilizand constructorul ?

Code: Select all

#include<iostream>
using namespace std;

class Test
{
private:
    const int t;
public:
    Test(int t):t(t)
    {}

    int getT()
    {
        return t;
    }
};

int main()
{
    Test t1(100);
    cout << "\n t1 = " << t1.getT() << endl;
    return 0;
}

Code: Select all

#include<iostream>
using namespace std;

class Test
{
private:
    const int t;
public:
    Test(int t)
    {
        this->t = t;
    }
    int getT()
    {
        return t;
    }
};

int main()
{
    Test t1(100);
    cout << "\n t1 = " << t1.getT() << endl;
    return 0;
}