这样的实现,该怎么重载<<

chary8088 2014-12-16 08:30:20
写文件的类,类似这样的,怎么重载<<
class log
{
public:
log(const char* file)
{
}
......
};

log("test.txt")<<"adf"<<" asdf"<<endl;

请问,该怎么重载<<?
先谢谢
...全文
180 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccnyou 2014-12-19
  • 打赏
  • 举报
回复
引用 5 楼 u011337306 的回复:
[quote=引用 4 楼 ccnyou 的回复:] [quote=引用 3 楼 chary8088 的回复:] [quote=引用 2 楼 ccnyou 的回复:] 随手写了一个,看看符不符合楼主需要

#include <iostream>
#include <fstream>
#include <string>

namespace Demo
{
	class log
	{
	public:
		log(const char* pszFileName)
		{
			m_logFile.open(pszFileName);
		}

		~log()
		{
			if (m_logFile.is_open())
			{
				m_logFile.close();
			}
		}

		template<class T>
		log& operator<<(const T& anyMessage)
		{
			m_logFile << anyMessage;
			return *this;
		}

		typedef std::ostream& TFUNC(std::ostream&);
		log& operator<<(TFUNC* pFunc)
		{
			pFunc(m_logFile);
			return *this;
		}

	private:
		std::ofstream	m_logFile;
	};

	void Test()
	{
		std::string aStr("A_STRING");
		log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl;
	}
}

int main(int argc, char* argv[])
{
	Demo::Test();
	return 0;
}
输出:
多谢,非常感谢,看来我对操作符重载理解不深刻 请教下,这个重载<<为什么要返回log&?
template<class T>
        log& operator<<(const T& anyMessage)
        {
            m_logFile << anyMessage;
            return *this;
        }
[/quote] 不返回自己怎么接着玩下去呢? 例如这里一步步来是这样的: log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl; log("log.txt") 构造了一个匿名对象假设叫做 mylog 吧 第一步执行: mylog << "hello, this is a number: " 然后返回了自己就是 mylog 然后再来 mylog << 1234 然后又返回了自己就是 mylog 然后再来 mylog << " and a string: " [/quote] 直接说为了实现链式表达式不就行了吗?这么纠结。。。[/quote] 为了尽量说得简单点
zacharyLiu 2014-12-17
  • 打赏
  • 举报
回复
引用 4 楼 ccnyou 的回复:
[quote=引用 3 楼 chary8088 的回复:] [quote=引用 2 楼 ccnyou 的回复:] 随手写了一个,看看符不符合楼主需要

#include <iostream>
#include <fstream>
#include <string>

namespace Demo
{
	class log
	{
	public:
		log(const char* pszFileName)
		{
			m_logFile.open(pszFileName);
		}

		~log()
		{
			if (m_logFile.is_open())
			{
				m_logFile.close();
			}
		}

		template<class T>
		log& operator<<(const T& anyMessage)
		{
			m_logFile << anyMessage;
			return *this;
		}

		typedef std::ostream& TFUNC(std::ostream&);
		log& operator<<(TFUNC* pFunc)
		{
			pFunc(m_logFile);
			return *this;
		}

	private:
		std::ofstream	m_logFile;
	};

	void Test()
	{
		std::string aStr("A_STRING");
		log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl;
	}
}

int main(int argc, char* argv[])
{
	Demo::Test();
	return 0;
}
输出:
多谢,非常感谢,看来我对操作符重载理解不深刻 请教下,这个重载<<为什么要返回log&?
template<class T>
        log& operator<<(const T& anyMessage)
        {
            m_logFile << anyMessage;
            return *this;
        }
[/quote] 不返回自己怎么接着玩下去呢? 例如这里一步步来是这样的: log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl; log("log.txt") 构造了一个匿名对象假设叫做 mylog 吧 第一步执行: mylog << "hello, this is a number: " 然后返回了自己就是 mylog 然后再来 mylog << 1234 然后又返回了自己就是 mylog 然后再来 mylog << " and a string: " [/quote] 直接说为了实现链式表达式不就行了吗?这么纠结。。。
ccnyou 2014-12-16
  • 打赏
  • 举报
回复
引用 3 楼 chary8088 的回复:
[quote=引用 2 楼 ccnyou 的回复:] 随手写了一个,看看符不符合楼主需要

#include <iostream>
#include <fstream>
#include <string>

namespace Demo
{
	class log
	{
	public:
		log(const char* pszFileName)
		{
			m_logFile.open(pszFileName);
		}

		~log()
		{
			if (m_logFile.is_open())
			{
				m_logFile.close();
			}
		}

		template<class T>
		log& operator<<(const T& anyMessage)
		{
			m_logFile << anyMessage;
			return *this;
		}

		typedef std::ostream& TFUNC(std::ostream&);
		log& operator<<(TFUNC* pFunc)
		{
			pFunc(m_logFile);
			return *this;
		}

	private:
		std::ofstream	m_logFile;
	};

	void Test()
	{
		std::string aStr("A_STRING");
		log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl;
	}
}

int main(int argc, char* argv[])
{
	Demo::Test();
	return 0;
}
输出:
多谢,非常感谢,看来我对操作符重载理解不深刻 请教下,这个重载<<为什么要返回log&?
template<class T>
        log& operator<<(const T& anyMessage)
        {
            m_logFile << anyMessage;
            return *this;
        }
[/quote] 不返回自己怎么接着玩下去呢? 例如这里一步步来是这样的: log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl; log("log.txt") 构造了一个匿名对象假设叫做 mylog 吧 第一步执行: mylog << "hello, this is a number: " 然后返回了自己就是 mylog 然后再来 mylog << 1234 然后又返回了自己就是 mylog 然后再来 mylog << " and a string: "
chary8088 2014-12-16
  • 打赏
  • 举报
回复
引用 2 楼 ccnyou 的回复:
随手写了一个,看看符不符合楼主需要

#include <iostream>
#include <fstream>
#include <string>

namespace Demo
{
	class log
	{
	public:
		log(const char* pszFileName)
		{
			m_logFile.open(pszFileName);
		}

		~log()
		{
			if (m_logFile.is_open())
			{
				m_logFile.close();
			}
		}

		template<class T>
		log& operator<<(const T& anyMessage)
		{
			m_logFile << anyMessage;
			return *this;
		}

		typedef std::ostream& TFUNC(std::ostream&);
		log& operator<<(TFUNC* pFunc)
		{
			pFunc(m_logFile);
			return *this;
		}

	private:
		std::ofstream	m_logFile;
	};

	void Test()
	{
		std::string aStr("A_STRING");
		log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl;
	}
}

int main(int argc, char* argv[])
{
	Demo::Test();
	return 0;
}
输出:
多谢,非常感谢,看来我对操作符重载理解不深刻 请教下,这个重载<<为什么要返回log&?
template<class T>
        log& operator<<(const T& anyMessage)
        {
            m_logFile << anyMessage;
            return *this;
        }
ccnyou 2014-12-16
  • 打赏
  • 举报
回复
随手写了一个,看看符不符合楼主需要

#include <iostream>
#include <fstream>
#include <string>

namespace Demo
{
class log
{
public:
log(const char* pszFileName)
{
m_logFile.open(pszFileName);
}

~log()
{
if (m_logFile.is_open())
{
m_logFile.close();
}
}

template<class T>
log& operator<<(const T& anyMessage)
{
m_logFile << anyMessage;
return *this;
}

typedef std::ostream& TFUNC(std::ostream&);
log& operator<<(TFUNC* pFunc)
{
pFunc(m_logFile);
return *this;
}

private:
std::ofstream m_logFile;
};

void Test()
{
std::string aStr("A_STRING");
log("log.txt") << "hello, this is a number: " << 1234 << " and a string: " << aStr << std::endl;
}
}

int main(int argc, char* argv[])
{
Demo::Test();
return 0;
}


输出:
chary8088 2014-12-16
  • 打赏
  • 举报
回复
自 己 up下

64,637

社区成员

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

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