警告使用封装函数
Warning to use encapsulated functions
我在用私有对象对数组进行排序时遇到了问题,这些对象是封装的(有getter和setter)。我使用自己的bubble排序函数。
void BubbleSort(apvector <int> &num)
{
int i, j, flag = 1;
int temp;
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
flag = 1;
}
}
}
问题是eclipseIDE向我发出警告,要在类的声明中使用getter和setter。
为什么使用getter和setter更好?
第S页很抱歉我的问题问得不好(这是我的第一个问题):)
void bubbleSort(Student* student, int size)
{ [...] }
变量student
是指向数组的指针
您还必须指定数组的大小
称之为:
Student* myClass=new Student[5];
bubbleSort(myClass, 5); // Pass the array, and the size of the array both.
在将上述数组作为参数发送到函数之前,需要创建该数组。或者,你可以在函数中创建它,但我认为这不是你想要的。
Student* students = new Student[5];
在调用函数之前,您应该在某个地方写下这篇文章。然后,您的函数签名必须转到以下位置:
void bubbleSort(Student* student)
不过,合乎逻辑的做法是在这里使用std::vector
,它比您要使用的方法要好得多。请参阅:http://en.cppreference.com/w/cpp/container/vector
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将可变参数函数的参数封装在类实例中
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何在类中封装C/C++套接字发送和接收函数?
- 类C++友元函数无法访问封装的类
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 如何使用提升范围将自定义迭代器封装在函数中
- 在封装指针的类中,移动语义无意中被复制构造函数取代
- 用Cython封装具有OpenCV参数的C++函数
- 使用Boost::Python封装纯虚拟函数
- 当使用另一个函数封装 printf 和 vfprintf 时,这种差异的原因是什么?
- 如何封装std::函数
- 非好友、非成员函数增加了封装
- 警告使用封装函数
- 在函数调用中封装std::cout
- 如何在haskell中封装对象构造函数和析构函数
- 用Ruby的FFI封装C函数
- 如何使用SWIG将C++工厂方法封装在Python_init__函数中
- 将许多C调用函数封装到单个实现中