繁体   English   中英

复制const char *

[英]Copy const char*

我从一个函数接收一个c-string作为参数,但我收到的参数将在以后销毁。 所以我想复制一份。

这就是我的意思:

class MyClass
{
private:
 const char *filename;

public:
 void func (const char *_filename);
}

void MyClass::func (const char *_filename)
{
 filename = _filename; //This isn't going to work
}

我想要实现的不是简单地将一个内存地址分配给另一个,而是复制内容。 我希望文件名为“const char *”而不是“char *”。

我试图使用strcpy,但它要求目标字符串是非const。

有办法吗? 没有在文件名上使用const_cast的东西?

谢谢。

使用std::string复制值,因为您已经在使用C ++。 如果你需要一个const char* ,请使用c_str()

class MyClass
{
private:
    std::string filename;
public:
    void setFilename(const char *source)
    {
        filename = std::string(source);
    }

    const char *getRawFileName() const
    {
        return filename.c_str();
    }
}

我同意最好的事情(至少在不了解你的问题的情况下)是使用std::string 但如果你坚持自己管理内存,你必须完全管理它。 所以C ++方式:

class MyClass
{
private:
 const char *filename;

 MyClass(const MyClass&); // no implementation
 MyClass operator=(const MyClass &); // no implementation

public:
 MyClass() {filename = 0;}
 ~MyClass() {delete[] filename;}

 void func (const char *_filename);
}

void MyClass::func (const char *_filename)
{
 const size_t len = strlen(_filename);
 char * tmp_filename = new char[len + 1];
 strncpy(tmp_filename, _filename, len);
 tmp_filename[len] = '\0'; // I'm paranoid, maybe someone has changed something in _filename :-)
 delete[] filename;
 filename = tmp_filename;
}

和C方式

class MyClass
{
private:
 const char *filename;

 MyClass(const MyClass&); // no implementation
 MyClass operator=(const MyClass &); // no implementation

public:
 MyClass() {filename = 0;}
 ~MyClass() {free(filename);}

 void func (const char *_filename);
}

void MyClass::func (const char *_filename)
{
 free(filename);
 filename = strdup(_filename); // easier than C++, isn't it?
}

您必须决定是否要将文件名设为const(因此无法更改)或非const(因此可以在MyClass :: func中更改)。

标准C库中有一个函数(如果你想进入C路由),名为_strdup。 它使用malloc进行实际分配,因此当你完成字符串后你需要自由调用。

所以,例如,

void MyClass::func (const char *_filename)
{
    if (filename)
    {
        free(filename);
    }
    filename = _strdup(_filename);
}

当然,不要忘记在析构函数中释放文件名。

std::string std::string

车削

const char *filename;

char *filename;

不会让你满意strcpy ,因为你实际上需要一些内存来复制你的字符串:)

对于手动内存管理代码部分,请参阅Tadeusz Kopec的答案,它似乎没有问题。

另外,请记住,两者之间存在差异

const char *filename; // "filename" points to "const char" 
                      //  and is not const itself
char const *filename; // semantically the same as above

char * const filename; // "filename" is const and points to "char", 
                       //  which is not const

在第一种情况下,您可以使filename指向任何其他const char字符串,在第二种情况下,您只能“就地”更改该字符串(因此保持filename值相同,因为它指向相同的内存位置) 。 当然,如果需要,可以将这两个(或者没有一个)组合在一起。

PS如果仅为成员函数的参数命名_filename以避免命名与成员变量filename冲突,则可以在其前面加上this (并删除下划线):

void MyClass::func (const char *filename)
{
 ...
 this.filename = copy;
}

如果你想坚持使用普通的C,请使用strncpy。 但我同意Ilya,使用std :: string,因为它已经是C ++了。 如果你的应用程序正在调用你的方法,你甚至可以在第一个地方收到一个std :: string,因为原始参数将被销毁。

为什么你把它作为const,如果你需要在类的一个方法中更改它们。

无论如何,非静态const数据成员和参考数据成员不能被赋值; 你应该使用初始化列表和构造函数来初始化它们。

MyClass::MyClass(const char *_filename) : filename( _filename ) 
{ 
   // filename = _filename; This isn't going to work 
}

初始化程序也可以调用如下函数

MyClass::MyClass(const char *_filename) : filename( getfilename() ) 
{ 
   // filename = _filename; This isn't going to work 
}

没有验证这个特定情况是否恰当,但初始化列表是为非静态const数据成员赋值的方法。

char const*表示该类不拥有与之关联的内存。 所有者总是需要一个非const指针,否则无法释放内存。 当你有非const指针时,你可以为它分配内存,然后使用strcpy (或memcpy )来复制字符串本身。 但是,在你的情况下使用std::string代替是一个更好的选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM