简体   繁体   中英

How to write a function returning const char*, which is a modified const char* argument?

I have to write a function (c++), which definition is:

const char* getFileName(const char* name);

to make things short, this function will take name, check some conditions and append to it a postfix, making it a proper filename. For example:

name = "someFile", return = "someFile-hd.png"

const char* returned by this function will be immediately passed to another, which takes const char* as an argument

something->loadFile(getFileName("someFile"));

The problem I have, is that when I create a const char* in my function it will have a scope limited to the function it was created in. On the other hand, I can't modify the code of loadFile function, since It's a 3rd party library I am using.

I can write a wrapper around loadFile to delete the const char* I've created, but I would not like to do that, since this is a porting exercise and this would break some templates I've created to make it easier.

Is there a way to write this function like I've described?

Thanks

EDIT: small explanation:

getFileName is a function I am trying to write, I can change the return type, but it has to fit as a parameter to loadFile function. My main problem is that const char* returned by getFileName is either lost because it's out of scope or I have to delete it explicitly, which breaks my porting template. I would like to generate the filename in one function call and do nothing else about it [in other words, in original code I have loadFile("something.png") and I want to change it to loadFile(getFileName("something")) without adding any new lines after that.

If you can change the return type of getFileName() to be std::string you could do the following:

std::string getFileName(const char* a_name)
{
    std::string result(a_name);
    result += ".png";

    return result;
}

// No memory management req'd
something->loadFile(getFileName("someFile").c_str());

The main logic of using const arguments is that it means, that you are not going to change this argument in the body of function that takes it.

If you want to change it, don't use const .

And don't worry about casting char* to const char* . There is implicit conversion for this.

You don't really need to change this const char* argument though. You could use it to construct std::string object and avoid possible memory leak - check answer by hmjd .

Returning pointers from functions usually don't make sense in C (and even less so in C++). Since you tagged this both C and C++, this is the C answer.

The common way to do this would be something like:

void getFileName (const char* name, 
                  char*       complete_name, 
                  size_t      complete_name_n)
{
  ... // create a new file name in a temp buffer

  if(the new file name has a strlen() < complete_name_n)
  {
    strcpy(complete_name, the new file name);
  }
}

// caller:
char name_buf [N];

getFileName(name, name_buf, N);
something->LoadFile(name_buf);

For example, the whole Windows API calls functions exactly in this way, leaving the allocation of parameters to the caller, letting the function concern itself only with its task (and not with memory allocation etc).

Though of course, this means that the caller has to type several rows instead of one. At this point you should ask yourself what's most important:

  • to write clean function interfaces without obscure pointer returns nor strange, needless, leaking dynamic allocations, or
  • to type as little code as possible in the caller to reduce keyboard and programmer wear-and-tear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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