由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 对thinking in c++里这段代码没搞懂
相关主题
请问C++返回值和返回引用区别用那个design pattern好?
一个C++的概念问题关于inserter
intel icc hash_map 求救!binary_search只要求forward_iterator?
STL感觉实在太变态了deque的pointer和reference是怎么回事?
[菜鸟问题]类模板问题a question about std::stack
c++ iterator 弱问呼唤大侠们,我实在不能实现C++泛型的精神。
c++ template question:stl 的 member type 看起来挺头大的
请问Linux底下有没有最简易的show 2D x-y curve的工具how to write a function take iterators as parameters?
相关话题的讨论汇总
话题: intstack话题: int话题: index话题: top
进入Programming版参与讨论
1 (共1页)
l******d
发帖数: 530
1
volume 1, 2nd edition, p. 719 -720
对下面代码不是很懂。
IntStack只有一个constructor
IntStack() : top(0) {}
没有parameter,那么这句
IntStackIter(IntStack& is) : s(is), index(0) {}
initializer list里s(is)是怎么被调用的?(s是IntStack类型的reference)
书中代码如下:
//: C16:IterIntStack.cpp
// Simple integer stack with iterators
//{L} fibonacci
#include "fibonacci.h"
#include "../require.h"
#include
using namespace std;
class IntStack {
enum { ssize = 100 };
int stack[ssize];
int top;
public:
IntStack() : top(0) {}
void push(int i) {
require(top < ssize, "Too many push()es");
stack[top++] = i;
}
int pop() {
require(top > 0, "Too many pop()s");
return stack[--top];
}
friend class IntStackIter;
};
//////////////// An iterator is like a "smart" pointer:
class IntStackIter {
IntStack& s;
int index;
public:
IntStackIter(IntStack& is) : s(is), index(0) {}
int operator++() { // Prefix
require(index < s.top,
"iterator moved out of range");
return s.stack[++index];
}
int operator++(int) { // Postfix
require(index < s.top,
"iterator moved out of range");
return s.stack[index++];
}
};
l*********s
发帖数: 5409
2
What do you mean? it is initialization,right?

【在 l******d 的大作中提到】
: volume 1, 2nd edition, p. 719 -720
: 对下面代码不是很懂。
: IntStack只有一个constructor
: IntStack() : top(0) {}
: 没有parameter,那么这句
: IntStackIter(IntStack& is) : s(is), index(0) {}
: initializer list里s(is)是怎么被调用的?(s是IntStack类型的reference)
: 书中代码如下:
: //: C16:IterIntStack.cpp
: // Simple integer stack with iterators

l******d
发帖数: 530
3
哦,我没仔细看,s是reference to IntStack,不是IntStack,所以s(is)是用is来初
始化s,是这么理解吧?
如果s是pointer to IntStack,is也是个pointer to IntStack,这么也行,对吧

【在 l*********s 的大作中提到】
: What do you mean? it is initialization,right?
l*********s
发帖数: 5409
4
yes
b********r
发帖数: 1080
5
这不是缺省的copy constructor吗?

【在 l******d 的大作中提到】
: 哦,我没仔细看,s是reference to IntStack,不是IntStack,所以s(is)是用is来初
: 始化s,是这么理解吧?
: 如果s是pointer to IntStack,is也是个pointer to IntStack,这么也行,对吧

1 (共1页)
进入Programming版参与讨论
相关主题
how to write a function take iterators as parameters?[菜鸟问题]类模板问题
c++ interview: iterator 和 pointer区别?c++ iterator 弱问
C++ Q11: iteratorc++ template question:
stl container erase in a loop请问Linux底下有没有最简易的show 2D x-y curve的工具
请问C++返回值和返回引用区别用那个design pattern好?
一个C++的概念问题关于inserter
intel icc hash_map 求救!binary_search只要求forward_iterator?
STL感觉实在太变态了deque的pointer和reference是怎么回事?
相关话题的讨论汇总
话题: intstack话题: int话题: index话题: top