关于重载运算符operator ()

ivanhh 2008-03-29 03:41:26
template <typename T>
Wrapper<T>::operator T const& ()const
{
return m_data;
}//m_data是Wrapper类的一个成员变量

然后我要想在程序中得到m_data的值,应该怎么调用这个重载的运算符。
下面是类的声明。

template <typename T>
class Wrapper : public Object
{
public:
Wrapper();
Wrapper(T const &data);
Wrapper(Wrapper const &wrapper);
Wrapper& operator = (T const &data);
Wrapper& operator = (Wrapper const &wrapper);

//强制类型转换重载,注意格式,语法
operator T const& () const; // To be revised.

T const& WrappedData()const;
HashValue Hash() const;
void Put(ostream &os) const;

protected:
T m_data;
int CompareTo(Object const &robj) const;
};

我是这样用的,但是总是报错:提示没有 <<的右操作数为Object类
typedef Wrapper<int> Int;
StackAsArray stack(5);
stack.Push(*new Int(3));
stack.Push(*new Int(4));
stack.Push(*new Int(5));
stack.Push(*new Int(6));

cout << stack.Pop() << endl;
其中StackAsArray的类声明是:
class StackAsArray : public Stack
{
private:
class Iter;
public:
StackAsArray(unsigned int size);
void Purge();
~StackAsArray();

void Push(Object &obj);
Object& Pop();
Object& Top() const;
Iterator& NewIterator() const;
friend class Iter;

void Accept(Visitor &visitor) const;

protected:
int CompareTo(Object const &robj) const;

private:
Array<Object*> m_array;
};


class Stack : public virtual Container
{
public:
virtual Object& Top() const = 0;
virtual void Push(Object&) = 0;
virtual Object& Pop() = 0;
};
...全文
638 55 打赏 收藏 转发到动态 举报
写回复
用AI写文章
55 条回复
切换为时间正序
请发表友善的回复…
发表回复
paidfighting 2008-04-02
  • 打赏
  • 举报
回复
[Quote=引用 49 楼 chao_83 的回复:]
Put函数在 Person 类中是有声明的,并且必须是virtual,
我想是有虚函数调用才会引发到派生类的函数查询。

如果用Parent派生类自己独有的函数调用肯定不行,
Person看不到那个函数的。

所以Object的引用是不能用operator const int & ()的。
如果Object声明了该 operator 的虚函数的话或许可以。
[/Quote]

汗。。。。re这个

当然要为Object类声明virtual operator const int &(),这是多态的要求。。。

否则没有虚函数覆盖,怎么能找到正确的函数入口。。
wanghao111 2008-03-30
  • 打赏
  • 举报
回复
交流群 C++ java 36414176
hityct1 2008-03-30
  • 打赏
  • 举报
回复
对于7楼的简单情况,改成char就不行了:

A<char> c('a');
cout << c << endl;//不正确,输出97

还是重载 << 吧。

至于为什么这样,我也不明白,可能是c++的自动转型规则作怪吧。

期待高手解答。
stude 2008-03-30
  • 打赏
  • 举报
回复
go go study
qiuwenping001 2008-03-29
  • 打赏
  • 举报
回复
看到paidfighting, chao_83,invanhh 三位的探讨终于有了结果,我表示由衷的祝贺,虽然我还是不怎么懂.以后会加油的
w375893296 2008-03-29
  • 打赏
  • 举报
回复
在这里有没软件高手的,有的话加入群58773512,一起深度探索软件界的一切奥妙...............
chao_83 2008-03-29
  • 打赏
  • 举报
回复
Put函数在 Person 类中是有声明的,并且必须是virtual,
我想是有虚函数调用才会引发到派生类的函数查询。

如果用Parent派生类自己独有的函数调用肯定不行,
Person看不到那个函数的。

所以Object的引用是不能用operator const int & ()的。
如果Object声明了该 operator 的虚函数的话或许可以。

ivanhh 2008-03-29
  • 打赏
  • 举报
回复
你说的方法已经可以用了,真是很感谢。我搞了一个上午。
我只是想自己体会一下设计模式的概念。
[Quote=引用 46 楼 chao_83 的回复:]
老兄你在写库啊, 我先吃饭去,晚一会在看。
我直觉好像用new不太好,至少别人未必这么用啊。
[/Quote]
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
改写成这个以后就可以得到正确结果了。
cout << ( static_cast <Wrapper <int> & > ( stack.Pop() ) );
请问,为什么要把返回的基类的引用在强制用static转换成派生类Wrapper<int>
C++的多态性不是会自动知道返回的是Wrapper<int>类的吗?

比如class Parent : public Person{};
Person &p = *new Parent;
p.Put(cout);
还是会执行Parent::Put(cout);的呀
chao_83 2008-03-29
  • 打赏
  • 举报
回复
老兄你在写库啊, 我先吃饭去,晚一会在看。
我直觉好像用new不太好,至少别人未必这么用啊。
paidfighting 2008-03-29
  • 打赏
  • 举报
回复
等待大牛们来吧,或者你发我邮箱里,paideng@163.com
paidfighting 2008-03-29
  • 打赏
  • 举报
回复
看得眼睛花。。。
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
代码全贴在上面了,拜托了
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
Wrapper.h

#ifndef _H_WRAPPER
#define _H_WRAPPER

#include <iostream>
using namespace std;

#include "Object.h"
#include "NullObject.h"

typedef int HashValue;

template <typename T>
class Wrapper : public Object
{
public:
Wrapper();
Wrapper(T const &data);
Wrapper(Wrapper const &wrapper);
Wrapper& operator = (T const &data);
Wrapper& operator = (Wrapper const &wrapper);

//强制类型转换重载,注意格式,语法
operator T const& () const; // To be revised.

T const& WrappedData()const;
HashValue Hash() const;
void Put(ostream &os) const;

protected:
T m_data;
int CompareTo(Object const &robj) const;
};

/* implemenation of the class Wrapper */
//implemenation of the constructors
template <typename T>
Wrapper<T>::Wrapper()
: m_data()
{}

template <typename T>
Wrapper<T>::Wrapper(T const &data)
: m_data(data)
{}

template <typename T>
Wrapper<T>::Wrapper(Wrapper const &wrapper)
: m_data(wrapper.m_data)
{}

//implemenation of the operator overloaded functions
template <typename T>
Wrapper<T>& Wrapper<T>:: operator = (T const &data)
{
m_data = data;
return *this;
}

template <typename T>
Wrapper<T>& Wrapper<T>::operator = (Wrapper<T> const &wrapper)
{
if(this == &wrapper)
return *this;

m_data = wrapper.m_data;
return *this;
}

template <typename T>
Wrapper<T>::operator T const& ()const
{
return m_data;
}

template <typename T>
T const& Wrapper<T>::WrappedData()const
{
return m_data;
}

template <typename T>
HashValue Wrapper<T>::Hash(void) const
{
// To do:
return -1;
}

template <typename T>
void Wrapper<T>::Put(ostream &os) const
{
os << m_data;
}

template <typename T>
int Wrapper<T>::CompareTo(Object const &robj) const
{
Wrapper<T> const &arg = dynamic_cast<Wrapper<T> const&>(robj);
return -1; // To be revised
//return ::Compare(m_data, arg.m_data);
}























#endif
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
Visitor.h

#ifndef _H_VISITOR
#define _H_VISITOR

#include "Object.h"

class Visitor
{
public:
virtual void Visit(Object &obj) = 0;
virtual bool IsDone() const
{return false;}
};














#endif
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
StackAsArray.h

#ifndef _H_STACKASARRAY
#define _H_STACKASARRAY

#include "Stack.h"
#include "Iterator.h"
#include "Object.h"
#include "Array.h"
#include "Visitor.h"

class Iter;

class StackAsArray : public Stack
{
private:
class Iter;
public:
StackAsArray(unsigned int size);
void Purge();
~StackAsArray();

void Push(Object &obj);
Object& Pop();
Object& Top() const;
Iterator& NewIterator() const;
friend class Iter;

void Accept(Visitor &visitor) const;

protected:
int CompareTo(Object const &robj) const;

private:
Array<Object*> m_array;
// class Iter; // why???? the syntax or the meaning???
//内嵌类
};

class StackAsArray::Iter : public Iterator
{
public:
Iter(StackAsArray const &stack);
bool IsDone() const;
Object& operator * () const;
void operator ++ ();
void Reset();

private:
StackAsArray const &m_stack;
unsigned int pos;
};














#endif
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
stack.h
#ifndef _H_STACK
#define _H_STACK

#include "Container.h"
#include "Object.h"

class Stack : public virtual Container
{
public:
virtual Object& Top() const = 0;
virtual void Push(Object&) = 0;
virtual Object& Pop() = 0;
};



#endif
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
Ownership.h
#ifndef _H_OWNERSHIP
#define _H_OWNERSHIP

class Ownership
{
public:
void AssertOwnership()
{
m_bIsOwner = true;
}
void rescindOwnership()
{
m_bIsOwner = false;
}
bool IsOwner() const
{
return m_bIsOwner;
}

protected:
Ownership() : m_bIsOwner(true)
{}
Ownership(Ownership &arg) : m_bIsOwner(arg.m_bIsOwner)
{
arg.m_bIsOwner = false;
}

private:
bool m_bIsOwner;
};







#endif
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
#ifndef _H_OBJECT
#define _H_OBJECT

Object.h

#include <iostream>
using namespace std;

typedef int HashValue;

class Object
{
public:
virtual ~Object();
virtual bool IsNull() const;
virtual int Compare(Object const &robj) const;
virtual HashValue Hash() const = 0;
virtual void Put(ostream &os) const = 0;

protected:
virtual int CompareTo(Object const &robj) const = 0;
};
ivanhh 2008-03-29
  • 打赏
  • 举报
回复
Iterator.h
#ifndef _H_ITERATOR
#define _H_ITERATOR
#include "Object.h"

class Iterator
{
public:
virtual ~Iterator();
virtual void Reset() = 0;
virtual bool IsDone() const = 0;
virtual Object& operator * () const = 0;
virtual void operator ++ () = 0;
};





#endif

------------------------------------------------
NullIterator.h

#ifndef _H_NULLITERATOR
#define _H_NULLITERATOR

#include "Iterator.h"

class NullIterator : public Iterator
{
public:
NullIterator();
~NullIterator();

void Reset();
bool IsDone() const;
Object& operator * () const;
void operator ++ ();
};





#endif

------------------------------------------------------

NullObject.h

#ifndef _H_NULLOBJECT
#define _H_NULLOBJECT

#include <iostream>
using namespace std;
#include "Object.h"

typedef int HashValue;

class NullObject : public Object
{
public:
bool IsNull() const;
HashValue Hash() const;
void Put(ostream &os) const;

static NullObject& Instance();

protected:
int CompareTo(Object const &robj) const;

private:
static NullObject instance;
NullObject(); //为了保证该类为单元集类,有且仅有一个实例对象
};
加载更多回复(35)

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧