Andre Hofmeister
Andre Hofmeister

Reputation: 3416

Initialized const in a class

Is it possible to initialize in a class a global const in a class method? I would like to use a method in my class to set the const.

My idea was:

/* a.h */
class A {
private:
    const string cs;

public:
    A();
    ~A();

    bool cs(const string &host, ...)
};

/* a.cpp */
A::A(){
}

A::~A(){
}

bool cs(const string &host, ...) {
    /* check some values */
    cs = "Set Vaule";   //Doesnt work, get an compiler error
}

Is It possible to set a global const in a method?

Upvotes: 0

Views: 137

Answers (5)

Walter
Walter

Reputation: 45414

As all the other answers have asserted, you cannot change the value of a const class member after initialization. However, some people think they are very clever and use the const_cast<>

class A {
  const int x;
public:
  A(int _x) : x(_x) {}
  void change_x(int _x)        // change x ?!
  { const_cast<int&>(x) = _x; }
};

With the gnu and intel compilers, this actually compiles without warning AFAIK and may even work. But this violates the language rules and constitutes the dreaded UB (undefined behaviour). In other words, it may not always work as intended, since the compiler is allowed to assume that x is unchanged since initialization.

Upvotes: 0

PaperBirdMaster
PaperBirdMaster

Reputation: 13320

As mentioned, you need to initialize the const members of an object using its initializer list:

/* a.h */
class A {
private:
    const string cs;

public:
    A(const string &value) :
        cs(value) // <---- initialize here!.
    {};
};

It is the same for every const member of the class:

class A {
private:
    const string cs;
    const float numberofthebeast;
    const char z;

public:
    A(const string &value, const float number, const char character) :
        cs(value),
        numberofthebeast(number),
        z(character)
        {};
};

If you don't want to provide a constructor to initialize each value, you can provide a default value in the default constructor, but remember that you cannot change the value after the construction:

class A {
private:
    const string cs;
    const float numberofthebeast;
    const char z;

public:
    A(const string &value, const float number, const char character) :
        cs(value),
        numberofthebeast(number),
        z(character)
        {};

    // Default values!!!
    A() :
        cs("default ctor"),
        numberofthebeast(666.666f),
        z('Z')
        {};
};

The constructor initializer list is also useful to initialize other members, like references o complex data that doesn't provide default constructor:

const unsigned float PI = 3.14f;

class Weird
{
    Weird (int w);
    // no default ctor!
    int W;
};

class Foo
{
    // Error: weird doesn't provide default ctor, 
    Weird w;
    // Error: reference uninitialized.
    float &pi;
};

class Bar
{
    Bar() :
        // Ok, Weird is constructed correctly.
        w(1),
        // Ok, pi is initialized.
        pi(PI)
    {};
    Weird w;
    float &pi;
};

Upvotes: 1

Fred Larson
Fred Larson

Reputation: 62063

No. You could initialize it in a constructor initializer, but once initialized a const member cannot be changed. Otherwise, it wouldn't be a constant, now, would it?

Upvotes: 4

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76778

This is only possible in the constructor of your class, and there only in the initializer-list:

A() : cs("Set Value") {
}

Upvotes: 3

NPE
NPE

Reputation: 500237

No, you can only set it in a constructor. After construction, it's set in stone.

Upvotes: 2

Related Questions