开发者

Please enlighten on the following C++ Syntax and its sense! (probably a case of overloading)

I am going through a legacy, and found the following syntax of C++ to be strange:

the class definition follows as:

class thread_shared : public master
{
public:
    thread_shared() 
    {
        thread_id = 0;
    }
    int thread_id;
    std::string title;
    std::string (*text2html)(std::string const &);  // What's this declaration?

};

and 开发者_如何转开发the definition of text2html as:

namespace {
    std::string text2html(std::string const &s)
    {
        std::string tmp=cppcms::util::escape(s);
        std::string res;
        res.reserve(tmp.size());
        for(unsigned i=0;i<tmp.size();i++) {
            if(tmp[i]=='\n') {
            res+="<br />";
            }
            res+=tmp[i];
        }
        return res;
    }
}

and then the usage which is as:

c.id = 1; // any integer
c.title = "some string";        
c.text2html = text2html;   // whats this??

where c is an instance of the thread_shared class declared above.

As Pointed above, could someone explain me the declaration such as:

std::string (*text2html)(std::string const &); and then c.text2html = text2html;

what exactly does the above code do?


std::string (*text2html)(std::string const &);  // What's this declaration?

That is a function pointer definition. It declares a variable text2html to be a pointer to a function with the signature std::string (std::string const &).

c.text2html = text2html;   // whats this??

That is setting the pointer to refer to the text2html function that has the appropriate signature. The right hand side is a function identifier and decays into &text2html, which in this case it resolves to the address in the unnamed namespace.

Note that you could set it to any function with the same signature, the coincidence of names is just a coincidence:

std::string foo( std::string const & ) {}
c.text2html = &foo;  // & optional


text2html in thread_shared is a so called function pointer. This pointer is then assigned to point to the function text2html.


std::string (*text2html)(std::string const &); is the declaration of a pointer to a function that takes an std::string const & as argument, and returns an std::string.

c.text2html = text2html initializes this pointer with the function text2html


You have a name collision going on here: text2html is being used both as the name of a field of the thread_shared class, as well as a function.

The first part:

std::string (*text2html)(std::string const &);

Defines a function pointer field named text2html on the thread_master class. Specifically, the field is a pointer to a function with a signature like this:

std::string foo(std::string const&);

The line

c.text2html = text2html

assigns the text2html function defined in your second code block to the 'text2html' field of c.

you could just as easily define the function

std::string pants(std::string const& dude) { return dude + " has red pants";}

and then assign it to c:

c.text2html = pants;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜