473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overriding template function doesn't work?

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 ,

Mar 9 '06 #1
4 1671
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;
}
};

Mar 9 '06 #2
thinktwice wrote:
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.
CThrowableHRESULT tHRESULT(S_OK);
tHRESULT = E_FAIL; //won't throw ,


Mar 9 '06 #3
how careless am i!
but even if i fix this problem , it still call the method in the base
class

Mar 9 '06 #4
thinktwice wrote:
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.
bool operator =(bool val)
{
Assign(val);
return m_val;
}
*/
protected:
bool IsBad()
{
return !m_val;
}
};


Mar 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
1
by: Emil Karlen | last post by:
Using the import-tag, a template appearing in the imported file, can be extended in the importing file. I am extending a template this way and in the main-file, inside the template use...
2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.