variabila const

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
vili_c++
Junior
Junior
Posts: 22
Joined: 11 Feb 2014, 23:39
Judet: Dolj

variabila const

Post by vili_c++ » 14 Sep 2019, 21:42

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;
}



Post Reply