mercredi 3 août 2016

Why b.isEm() prints different things on different lines?

Why b.isEm() prints different things on different lines when I have not changed anything after the last call of b.isEm()?

#include <iostream>
#include <string>

template <class T>
class Box
{
    bool m_i;
    T m_c;

public:
    bool isEm() const;
    void put(const T& c);
    T get();
};


template <class T>
bool Box<T>::isEm() const
{
    return m_i;
}

template <class T>
void Box<T>::put(const T& c)
{
    m_i = false;
    m_c = c;
}

template <class T>
T Box<T>::get()
{
    m_i = true;
    return T();
}


int main()
{
    Box<int> b;
    b.put(10);

    std::cout << b.get() << " " << b.isEm() << std::endl; 
    std::cout << b.isEm() << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire