overriding template function doesn't work?

T

thinktwice

template <class T>
class CThrowable
{
protected:
virtual void Assign(T val)
{
m_val = val;
if (IsBad())
{
throw val;
}
}
virtual bool IsBad() const
{
return false;
}
T m_val;
};

class CThrowableHRESULT : public CThrowable<HRESULT>
{
public:
CThrowableHRESULT(HRESULT val)
{
Assign(val);
}
protected:
bool IsBad()
{
return !SUCCEEDED(m_val);
}
};

class CThrowableBoolean : public CThrowable<bool>
{
public:
CThrowableBoolean(bool val)
{
Assign(val);
}
protected:
bool IsBad()
{
return !m_val;
}
};




CThrowableBoolean tBoolean(true);
tBoolean = false; //won't throw, actually it calls IsBad which is in
the base class ,why?
CThrowableHRESULT tHRESULT(S_OK);
tHRESULT = E_FAIL; //won't throw ,
 
T

thinktwice

another problem quite confuse me is
class CThrowableBoolean : public CThrowable<bool>
{
public:
CThrowableBoolean(bool val)
{
Assign(val);
}
/* if i comment this operator function when i call
tBoolean = false , it will call class construct
CThrowableBoolean(bool) , but if i open the operator= method , tBoolean
= flase will lead to call operator=(bool), is this the c++ standard
that c++ vendor should obey?
bool operator =(bool val)
{
Assign(val);
return m_val;
}
*/
protected:
bool IsBad()
{
return !m_val;
}
};
 
R

Rolf Magnus

thinktwice said:
CThrowableBoolean tBoolean(true);
tBoolean = false; //won't throw, actually it calls IsBad which is in
the base class ,why?

Because IsBad's signature in the base class is different from those in the
derived classes (const/non-const), so they are different functions.
 
T

thinktwice

how careless am i!
but even if i fix this problem , it still call the method in the base
class
 
R

Rolf Magnus

thinktwice said:
another problem quite confuse me is
class CThrowableBoolean : public CThrowable<bool>
{
public:
CThrowableBoolean(bool val)
{
Assign(val);
}
/* if i comment this operator function when i call
tBoolean = false , it will call class construct
CThrowableBoolean(bool) , but if i open the operator= method , tBoolean
= flase will lead to call operator=(bool), is this the c++ standard
that c++ vendor should obey?

Well, what did you expect?
Without your assignment operator, there is only one assignment operator
available, and that is the compiler-generated one that takes a reference to
a CThrowableBoolean as right hand sid. So the compiler can only use that
one to do the assignment. Therefore, it will use the conversion constructor
to convert the bool on the right side to a CThrowableBoolean and then do
the assignment. If you add your assignment operator that takes a bool, the
compiler has an option that fits better, because no conversion is needed,
so it will choose that one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top