由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个c++题
相关主题
请问关于overloading << (转载)C++: 如何对const data member做assignment?
请教operator const char*() 的问题发发面经 攒人品 C++的
弱问个C++ 问题 (const_cast)问一个post fix 算式计算的问题
问个c++的问题求教:这个程序为什么不能编译?
C++ Q83: 这个const_cast什么意思?c++疑难问题。。
One C++ question弱问一道c++语法题
请教ebay 的面试题一道请帮忙看道题 c++ operator overload
问一道kth smallest element的题目请问一道c++题目
相关话题的讨论汇总
话题: ostream话题: operator话题: const话题: out话题: cout
进入JobHunting版参与讨论
1 (共1页)
l********n
发帖数: 54
1
有一个类
class A
{
};
要使得下面的code能够编译
int main(void)
{
A a;
cout< return 1;
}
并且cout< 问需要在这个类里添加什么functions,如何实现。
============
应该是要对<<进行operator overloading
ostream& operator<< (const ostream& out, const A& a);
不过不知道如何实现。
PS: 建议用用这个工具,这样大家可以讨论code起来比较方便。
http://www.ideone.com/kNSiq
S**I
发帖数: 15689
2
How about
out << "A";

【在 l********n 的大作中提到】
: 有一个类
: class A
: {
: };
: 要使得下面的code能够编译
: int main(void)
: {
: A a;
: cout<: return 1;

l*******y
发帖数: 1498
3
class A
{
private:
string name;
public:
....
friend ostream& operator<< (const ostream& out, const A& a);
};
ostream& operator<< (const ostream& out, const A& a)
{
out << a.name;
return out;
}
l********n
发帖数: 54
4
试了,不过不能compile. 试试用下面的link测试。
http://www.ideone.com/kNSiq

【在 l*******y 的大作中提到】
: class A
: {
: private:
: string name;
: public:
: ....
: friend ostream& operator<< (const ostream& out, const A& a);
: };
: ostream& operator<< (const ostream& out, const A& a)
: {

l*******y
发帖数: 1498
5
你看看是什么错误再修改一下不就可以了
#include
#include
using namespace std;
class A
{
private:
string name;
public:
A(string n):name(n){}

friend ostream& operator<< (ostream &out, const A &a);
};
ostream& operator<< (ostream &out, const A &a)
{
out << a.name;
return out;
}
int main()
{
A aa("test");

cout<
return 0;
}
l*******y
发帖数: 1498
6
你这个链接里的只能 A<
【在 l********n 的大作中提到】
: 试了,不过不能compile. 试试用下面的link测试。
: http://www.ideone.com/kNSiq

l********n
发帖数: 54
7
这个code没有问题,可以正确运行。
不过我还有一个问题,就是为什么要用友元函数,而不能作为成员函数。就像普通
operator overloading.我试过了,有
compiler error.不过不是很明白为什么。
class A
{
public:
ostream& operator<< (ostream &out, const A &a)
{
out << a.name;
return out;
}
}

【在 l*******y 的大作中提到】
: 你看看是什么错误再修改一下不就可以了
: #include
: #include
: using namespace std;
: class A
: {
: private:
: string name;
: public:
: A(string n):name(n){}

l*******y
发帖数: 1498
8
如果是成员函数,就只能由object调用,比如你那个链接里的,只能 a.operator<<(
out, a).
去看看c++ primer

【在 l********n 的大作中提到】
: 这个code没有问题,可以正确运行。
: 不过我还有一个问题,就是为什么要用友元函数,而不能作为成员函数。就像普通
: operator overloading.我试过了,有
: compiler error.不过不是很明白为什么。
: class A
: {
: public:
: ostream& operator<< (ostream &out, const A &a)
: {
: out << a.name;

h**k
发帖数: 3368
9
没有办法作为成员函数。因为对于二元操作符,如果定义为成员函数,缺省的左参数必
须是对象本身。
比如对于operator +,如果我们在一个class A里用成员函数overload它,那么我们只
能支持A+B,不能支持B+A。
在这个例子里,我们希望实现 cout << A,而不是A << cout。那么要不在cout所属类
中对A定义 <<,要不定义<<作为non-member operator。

【在 l********n 的大作中提到】
: 这个code没有问题,可以正确运行。
: 不过我还有一个问题,就是为什么要用友元函数,而不能作为成员函数。就像普通
: operator overloading.我试过了,有
: compiler error.不过不是很明白为什么。
: class A
: {
: public:
: ostream& operator<< (ostream &out, const A &a)
: {
: out << a.name;

l********n
发帖数: 54
10
这个问题搞懂了,多谢
1 (共1页)
进入JobHunting版参与讨论
相关主题
请问一道c++题目C++ Q83: 这个const_cast什么意思?
C++ Q48: illegal operation (C33)One C++ question
C++ 面试题请教ebay 的面试题一道
问个面试题问一道kth smallest element的题目
请问关于overloading << (转载)C++: 如何对const data member做assignment?
请教operator const char*() 的问题发发面经 攒人品 C++的
弱问个C++ 问题 (const_cast)问一个post fix 算式计算的问题
问个c++的问题求教:这个程序为什么不能编译?
相关话题的讨论汇总
话题: ostream话题: operator话题: const话题: out话题: cout