由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 私有成员不能用类成员函数修改?
相关主题
C++里get array size的问题 (转载)C++小程序查错
a simple question for C++ classc++ question
C++ Q04: template member请问一个exception题目
One c++ non-type template questionHelp: who has gcc 4.0 or higher
请教一个基本的constructor和destrcutor问题C++里面
小问题两个继承问题
a question about bitwise operation为什么我看不懂下面的code,是不是水平还不够?
[合集] 关于template和inheritance的问题请教C++ 弱问一个
相关话题的讨论汇总
话题: int话题: size话题: array话题: cout话题: result
进入Programming版参与讨论
1 (共1页)
m*******o
发帖数: 264
1
如果注释private,编译就能通过
#include
using namespace std;
template
class c_array {
//friend int sum(c_array &a);
public:
c_array(T s): size(s) { a = new T[size]; }
T& operator[](int i);
int get_size() { return size; }
bool operator < (c_array &x);
private:
int size;
int *a;
};
template
T sum(c_array& a){
T result = 0;
int size = a.get_size();
for ( int i = 0; i < size; i++ )
result += a[i];
return result;
}
P********e
发帖数: 2610
2
你在main里面直接用 . 去使用private,当然不行拉
class c_array{
public getArrayElement(int n){if(n }

【在 m*******o 的大作中提到】
: 如果注释private,编译就能通过
: #include
: using namespace std;
: template
: class c_array {
: //friend int sum(c_array &a);
: public:
: c_array(T s): size(s) { a = new T[size]; }
: T& operator[](int i);
: int get_size() { return size; }

m*******o
发帖数: 264
3
为什么在main里面直接用 . 去使用private不行?
我定义的类对象A[3]就是c_array啊,然后用他自己的成员函数T& operator[](int i)
来调用私有成员
s****u
发帖数: 118
4
人生就是这样的

)

【在 m*******o 的大作中提到】
: 为什么在main里面直接用 . 去使用private不行?
: 我定义的类对象A[3]就是c_array啊,然后用他自己的成员函数T& operator[](int i)
: 来调用私有成员

e***a
发帖数: 18
5
should be:
A[0][0] = 2; A[0][1] = 4; A[0][2] = 1;
A[1][0] = 5; A[1][1] = 3; A[1][2] = 7;
A[2][0] = 0; A[2][1] = 3; A[2][2] = 2;
for ( int i = 0; i < num; i++)
cout << A[0][i] << ' ';
cout << endl;
for ( int i = 0; i < num; i++)
cout << A[1][i] << ' ';
cout << endl;
for ( int i = 0; i < num; i++)
cout << A[2][i] << ' ';
cout << endl;
c********x
发帖数: 84
6
template
T sum(c_array& a){ <- you forget a "public" here
T result = 0;
int size = a.get_size();
for ( int i = 0; i < size; i++ )
result += a[i];
return result;
}
z***e
发帖数: 5393
7
然后用他自己的成员函数T& operator[](int i) 来调用私有成员
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
you didn't.

)

【在 m*******o 的大作中提到】
: 为什么在main里面直接用 . 去使用private不行?
: 我定义的类对象A[3]就是c_array啊,然后用他自己的成员函数T& operator[](int i)
: 来调用私有成员

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 弱问一个请教一个基本的constructor和destrcutor问题
C++疑问小问题
two c++ interview questions! (转载)a question about bitwise operation
请教一个作用域的问题[合集] 关于template和inheritance的问题请教
C++里get array size的问题 (转载)C++小程序查错
a simple question for C++ classc++ question
C++ Q04: template member请问一个exception题目
One c++ non-type template questionHelp: who has gcc 4.0 or higher
相关话题的讨论汇总
话题: int话题: size话题: array话题: cout话题: result