linuxeasy
linuxeasy

Reputation: 6499

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?

Upvotes: 2

Views: 189

Answers (4)

Mark P Neyer
Mark P Neyer

Reputation: 1009

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;

Upvotes: 1

Didier Trosset
Didier Trosset

Reputation: 37437

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

Upvotes: 3

Andreas Brinck
Andreas Brinck

Reputation: 52529

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

Upvotes: 3

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

Upvotes: 11

Related Questions