Присвоение значения члену класса ConstC++

Программы на C++. Форум разработчиков
Anonymous
Присвоение значения члену класса Const

Сообщение Anonymous »

Есть ли в коде ниже? < /p>

Код: Выделить всё

#include 

int* p_n;

class A
{
public:

A(int val) : n(val)
{
// At this point the compiler does not know if the object is const or not.
p_n = &n;
}

int n;
};

int main()
{
// A::n is const, because A is const, right?
const A a(1);

// but we change a const value here
*p_n = 2;

std::cout 

Подробнее здесь: [url]https://stackoverflow.com/questions/79421784/assigning-a-value-to-a-const-class-member[/url]

Вернуться в «C++»