Re[5]: наследование или...
От: Glоbus Украина  
Дата: 10.08.04 10:08
Оценка:
Здравствуйте, Oleg A. Bachin, Вы писали:

OAB>Здравствуйте, Glоbus, Вы писали:


OAB>повторюсь, я только учусь...


OAB>можно вот здесь уточнить?


G>>
G>>template <typename T>
G>>class nullable
G>>{
G>>public:

G>>    bool operator == (const T &other) const // 1*
G>>    {
G>>        if (this.is_null || other.is_null) // 2*
G>>            throw std::logic_error("null_value_compare");
G>>        return m_Value == T // 3*
G>>    }
G>>


OAB>1. (const T &other) — это значит что мы сравниваем с T? тогда у него нет ф-ции is_null. Наверное всетаки (const nullable<T> &other)


OAB>2. я так понял, что this имеет тип указатель на nullable<T>. тогда может так: this->is_null ?

OAB>3. по-моему тут явная опечатка, но я не могу все равно написать эту строку...

OAB>я сделал ф-цию value, которая возвращает const T &.

OAB>
OAB> const T & value() const
OAB>    {
OAB>        return value_;
OAB>    }
OAB>

OAB>и пытаюсь написать вот так:
OAB>
OAB>    template <typename T>
OAB>    bool operator==(nullable<T> const& other)
OAB>    {
OAB>        if (this->is_null() || other.is_null()) 
OAB>            throw null_value_compare("null_value_compare");
OAB>        return (this->value_ == other.value());
OAB>    }
OAB>


OAB>на что получаю:

OAB>

OAB>error C3767: '==' matching function is not accessible


Да, пардон, должно было быть так

template <typename T>
class nullable
{
public:

    nullable(): is_null_(true) {};
    nullable(const T &other): is_null_(false), m_Value(other) {};

    bool & is_null() const 
    {
        return is_null_;
    }

    bool operator == ( const nullable<T>& _other )
    {
        if (this->is_null_ || _other.is_null_) 
            throw std::logic_error("null_value_compare");
        return m_Value == _other.m_Value;

    }
    
    operator T ()
    {
        return m_Value;
    }
private:
    bool is_null_;
    T m_Value;
};






int main()
{
//использование
    nullable<int> obj1(5);
    nullable<int> obj2(5);

    std::cout << (obj1 == obj2) << std::endl;
    std::cout << obj1 << std::endl;
    std::cout << ( obj1 + 5 ) << std::endl;

    

}
Удачи тебе, браток!
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.