小编Sch*_*lar的帖子

当您不修改变量时,通过引用传递的角色是什么?

这些引用(&)只是一个保存内存或习语的问题,还是有理由为什么这些语句在复制时使用引用会完成同样的事情.

template <class T>
bool testGreater (const T& one, const T& two);
Run Code Online (Sandbox Code Playgroud)

或者像这样的对象声明:

Cars BMW (const Engine&); //where engine is a class
Run Code Online (Sandbox Code Playgroud)

当您不需要修改传递的项目时,通过引用传递的功能是什么?

c++ memory const reference

4
推荐指数
1
解决办法
141
查看次数

如何将argv []的内容复制到ac样式字符串中?

我正在尝试编写一个程序,它在运行时接受几个参数来将文本附加到文件中.

该程序在运行时产生分段错误.这是代码:

int main(int argc, char* argv[])
{
    //error checking
    if (argc < 1 || argc > 4) {
        cout << "Usage: -c(optional - clear file contents) <Filename>, message to write"    << endl;
        exit(EXIT_FAILURE);
    }

    char* filename[64];
    char* message[256];

    //set variables to command arguments depending if -c option is specificed
    if (argc == 4) {
        strcpy(*filename, argv[2]);
        strcpy(*message, argv[3]);
    } else {
        strcpy(*filename, argv[1]);
        strcpy(*message, argv[2]);
    }

    int fd; //file descriptor 

    fd = open(*filename, O_RDWR | O_CREAT, 00000); //open file …
Run Code Online (Sandbox Code Playgroud)

c++ unix string segmentation-fault

3
推荐指数
1
解决办法
2万
查看次数

你能动态分配一个向量吗?

我需要我的矢量容器持久超过分配它的函数的末尾.

void initVector() {
    vector<obj1*> R;
}
Run Code Online (Sandbox Code Playgroud)

为什么这无效?

void initVector() {
    vector<obj1*> R = new vector<obj1*>;
}
Run Code Online (Sandbox Code Playgroud)

c++ memory stl vector

0
推荐指数
1
解决办法
85
查看次数

标签 统计

c++ ×3

memory ×2

const ×1

reference ×1

segmentation-fault ×1

stl ×1

string ×1

unix ×1

vector ×1