在 C++ 中,将类的数据保留为私有成员总是更好。
如果一个类有一个向量作为成员,最好把它作为私有成员还是公共成员?
如果我有一个向量作为私有成员,我将无法轻松访问该向量的成员函数。所以我必须为我需要访问向量方法的每个函数设计一个带有方法的类?
给出的例子:
class MyClass{
private:
std::vector<int> _myints;
public:
get_SizeMyints(){return _myints.size();}
add_IntToMyints(int x){_myints.push_back(x));
};
Run Code Online (Sandbox Code Playgroud)
还是最好保持向量公开并调用 MyClass._myints.push_back(x)?
- - - - - - - - - - -编辑 - - - - - - -
只是为了清楚这个问题需要什么:
蛇.h:
enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW };
class Snake
{
private:
enum directions head_dir;
int cubes_taken;
float score;
struct_color snake_color;
V4 head_pos;
public:
std::vector<Polygon4> p_list; //the public vector which should be private...
Snake();
V4 get_head_pos();
Polygon4 create_cube(V4 point); …
Run Code Online (Sandbox Code Playgroud) 我对C ++有点生疏,经过一年的python转换。自然,我想将python的懒惰翻译成C ++。
我刚刚发现rot13,我对此感到非常兴奋。我找到了3种方法,并且我想做一点性能测试。我还想看看在适当位置修改字符串或创建新字符串是否有区别。所以我最终有6个功能。
在第一种方法中,我使用std :: map映射字符,因此,我建立了一个初始化映射的类,在第二种方法中,我使用了三元运算符,第三种方法是使用了位移。
现在函数原型看起来像这样
// map dependent
void Rot13::convert_inplace(string& mystr){
string Rot13::convert(const string& mystr){
// ternary operator
void bitrot_inplace(string& mystr){
string bitrot(const string& mystr){
// bit shift
void bitshiftrot_inplace(string& mystr){
string bitshiftrot(const string& mystr){
Run Code Online (Sandbox Code Playgroud)
我想构造一个接受这些函数作为参数的函数,然后计算时间并打印结果
typedef void (*vfc)(string str);
void printtime_inplace(string title, vfc func){
Run Code Online (Sandbox Code Playgroud)
我尝试了这种构造,但这意味着我受到vfc
返回类型(在我的情况下为void
or)的限制string
,并且受制于我需要传递类的指针的事实。因此,我将必须做3个函数来容纳不同的函数,即用于类成员函数的函数,用于void返回类型的函数和用于字符串返回类型的函数。
所以我问自己,是否真的需要使用模板而不编写相同功能的3倍?我对模板真的没有信心,但是我应该做3 typedefs
并构造printtime函数以接受模板吗?此外,有没有办法告诉模板您将只接受这些类型(即我定义的类型)?
另一个问题,这可以说是一个好的设计吗?还是您会建议其他设计?其他实现?
是否可以初始化这样的类?
Quaternion::Quaternion(){ //default without arguments
Quaternion(0.,V3(0.,0.,0.));
}
Quaternion::Quaternion(double s, V3 v){ //with scalar and vector as a argument
coords[0] = s;
coords[1] = v[0];
coords[2] = v[1];
coords[3] = v[2];
}
Run Code Online (Sandbox Code Playgroud)
因为这是输出:
QUATERNION TEST
(2.122e-313:-3.22469e-232:2.122e-313:-1.998) //instanciated with Quaternion q;
(4:1:2:3) // instanciated with the Quaternion q(4,1,2,3);
(4:-1:-2:-3) // conjugated of the one above
Run Code Online (Sandbox Code Playgroud)
并且第一个没有初始化为0.因为它应该是......为什么?
我想将类保存到python中的文件.我想要这样的东西,我在python中有一个类似的类,就像这个C++结构:
struct WebSites
{
char SiteName[100];
int Rank;
};
Run Code Online (Sandbox Code Playgroud)
我想写这样的东西:
void write_to_binary_file(WebSites p_Data)
{
fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
binary_file.write(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
binary_file.close();
}
Run Code Online (Sandbox Code Playgroud)
这可以简单地读取:
void read_from_binary_file()
{
WebSites p_Data;
fstream binary_file("c:\\test.dat",ios::binary|ios::in);
binary_file.read(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
binary_file.close();
cout<<p_Data.SiteName<<endl;
cout<<"Rank :"<< p_Data.Rank<<endl;
}
Run Code Online (Sandbox Code Playgroud)
python中有一个方法可以做到这一点吗?
我正在尝试用C++构建一个颜色类,
这不是一个功课只是因为我仍在努力使用引用和const.
--Color.h
class Color{
private:
double r;
double g;
double b;
double a;
public:
//constructor, getters and setters...
Color& operator =(Color& other_color); //(1)
}
Run Code Online (Sandbox Code Playgroud)
--Color.cpp
Color& operator=(Color& other_color){
this->r = other_color.get_r(); //line 41
this->b = other_color.get_b();
//and so on...
return *this;
}
Run Code Online (Sandbox Code Playgroud)
像这样它工作正常,但我听说必须放一个const来避免因为错误,对象将被分配操作修改,所以必须将另一个对象声明为const.像这样:
Color& operator =(Color const& other_color); //(2)
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
/Users/../color.cpp:41: error: passing 'const Color' as 'this' argument of 'float Color::get_r()' discards qualifiers
Run Code Online (Sandbox Code Playgroud)
所以这是我的问题......
这里发生了什么?如果我不将other_color声明为const ,会发生什么?有什么可能的错误?
PS.:小问题:
我想将我的变量传递给opengl glColor4v(colorx.return_rgba()),返回Color类的数组[r,g,b,a].这个:
float* Color::return_rgba(){
float rgba[4] = {this->r, this->g, this->b, …
Run Code Online (Sandbox Code Playgroud) 我希望这不是一个太具体的问题......但如果你能提供帮助我真的很感激!
当我构建我的应用程序时,我收到此错误,你知道为什么吗?:
ld: duplicate symbol colors::white in mainwindow.o and main.o
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这是主要的定义:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
这是主窗口的定义:
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
glwidget = new MyGLBox(ui->centralWidget);
startTimer(11);
//test frame
BasicShapes shapes;
Polygon3 square = shapes.create_square(V3(0.,0.,0.),V3(1.0,1.0,1.),colors::cyan);
Polygon3 cube = shapes.create_cube(V3(-1.,-1.,-1.),V3(1.,1.,1.),colors::purple);
Polygon3 triangle = shapes.create_triangle(V3(0,1.,0),V3(1.,1.,1.),5.,colors::green);
//other stuff...
ui->vLayoutGLview->addWidget(glwidget); //add the glwidget to the ui framework
}
Run Code Online (Sandbox Code Playgroud)
colors是文件colors.h中定义的命名空间: …