Setting up a Constant in a Class

I want to make a const char in a class.
I mean that if I create two objects of a class, A & B;
If I set the value for A.Char & B.Char, it will be a constant value.

Is there a way to do so?
Sure.

1
2
3
4
5
6
7
8
struct Foo
{
  Foo(const char* str) : str(str) {}
  const char* const str;
};

Foo A("foo");
Foo B("bar");
I don't get what's happening here. Could you elaborate?
The class has a constant member of type const char*, which is initialized using the initializer list in the constructor. Due to it being constant, you cannot change it later, so A.str="bar"; won't work. Which seems to be what you wanted.
Thanks.It did the work!!
Topic archived. No new replies allowed.