operator overriding

Hello,

I need to create a class CASQLRow which overrides operator[]. The problem is the return value which might be different because of the sql field type.

What I did so far is:

1
2
3
4
5
6
7
class CASQLRow
{
public:
	virtual ~CASQLRow(){}
	virtual const char* operator[] (const char* field) const = 0;
	virtual const char* operator[] (int field) const = 0;
};


Yet, I would like something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CABlob;

class CASQLRow
{
public:
	virtual ~CASQLRow(){}
	virtual const char* operator[] (const char* field) const = 0;
	virtual const char* operator[] (int field) const = 0;
	virtual long operator[] (const char* field) const = 0;
	virtual long operator[] (int field) const = 0;
	virtual double operator[] (const char* field) const = 0;
	virtual double operator[] (int field) const = 0;
	virtual const CABlob & operator[] (const char* field) const = 0;
	virtual const CABlob & operator[] (int field) const = 0;
};


I know I am a c++ rookie, so please feel free to throw anything at me (especially valid information).

Thanks.
Overloaded methods should be different by params, not by return type
So your class CASQLRow is incorrect.
Thanks for the reply.

I know it is incorrect. I would likt to know how to do this using an operator. Else I will use functions like:
1
2
3
4
5
...
virtual int value(const char* field, long& v) const = 0;
virtual int value(const char* field, double& v) const = 0;
virtual int value(const char* field, CABlob** v) const = 0;
...


I would like to know if it is possible to have this kind of functionaly using operator[].

Thank you
1
2
template <typename T>
const T& operator[](const char* field) const;


But in this case this operator can't be virtual
Thanx,

I found an interesting solution to my problem. Perhaps it will be usefull for others:

http://www.parashift.com/c++-faq-lite/templates.html (35.7)

Ok, that's why Denis suggested, but I'm not seeing how that solution provides a palatable
solution, because the template only on return type sort of defeats the purpose of the
operator in the first place (the purpose being to provide a more natural, intuitive syntax).
Other alternatives include:

1) return a boost::variant<>;
2) return a boost::any;
3) use your own type erasure class (http://www.cplusplus.com/forum/articles/18756/);
4) always return a string, and perform conversions where necessary.

Topic archived. No new replies allowed.