Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a variable that can be set only once?

Tags:

c++

I know of const, that can't be changed after creation. But I was wondering if there is a way to declare a variable that you set only once and after that, can't overwrite. In my code, I would like to avoid the bool variable by having an nFirst that, once set to nIdx, can't be set to the new value of nIdx.

My code:

    int nFirst = 0;
    int nIdx = 0;
    bool bFound = false;
    BOOST_FOREACH(Foo* pFoo, aArray)
    {
        if (pFoo!= NULL)
        {
            pFoo->DoSmth();
            if (!bFound)
            {
                nFirst= nIdx;
                bFound = true;
            }
        }
        nIdx++;
    }
like image 408
tzippy Avatar asked Mar 04 '16 16:03

tzippy


People also ask

What type of variable can be assign to only once?

Constants. In some languages, it is possible to define special variables which can be assigned a value only once – once their values have been set, they cannot be changed. We call these kinds of variables constants.

How do you initialize a variable only once?

Static Member Variables In A Class As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).

How do you set a variable only once in C#?

To do this, first you have to make a bool variable that tells you if the variable has been set: bool isSet; Then, we make the variable be true when the variable you want to be able to be set only once is set. Note that this returns null if this variable is accessed before it is set.

Can you define a variable twice?

Show activity on this post. int a; means both declaration and definition, but we know that defining a variable more than once is not allowed.


2 Answers

Pretty easy to roll your own.

template<typename T>
class SetOnce
{
public:
    SetOnce(T init) : m_Val(init)
    {}

    SetOnce<T>& operator=(const T& other)
    {
        std::call_once(m_OnceFlag, [&]()
        {
            m_Val = other;
        });

        return *this;
    }

    const T& get() { return m_Val; }
private:
    T m_Val;
    std::once_flag m_OnceFlag;
};

Then just use the wrapper class for your variable.

SetOnce<int> nFirst(0);
nFirst= 1;
nFirst= 2;
nFirst= 3;

std::cout << nFirst.get() << std::endl;

Outputs:

1

like image 196
lcs Avatar answered Oct 23 '22 15:10

lcs


I would like to avoid the bool variable

You can check nFirst itself, based on the fact that it won't be set a negative number. Such as:

int nFirst = -1;
int nIdx = 0;
BOOST_FOREACH(Foo* pFoo, aArray)
{
    if (pFoo != NULL)
    {
        pFoo->DoSmth();
        if (nFirst == -1)
        {
            nFirst = nIdx;
        }
    }
    nIdx++;
}
like image 43
songyuanyao Avatar answered Oct 23 '22 16:10

songyuanyao