Moving C++/CLI Properties Out Of Headers

164 words.

A while back I railed against C++/CLI for its nonsensical inability to separate property implementations from the header file.  Well, it turns out you can do it - it’s just not widely reported.  Almost every example of a C++/CLI property shows it implemented in the header file.  But thankfully I stumbled upon an article about this very subject:  Splitting Properties Between Header And Implementation Files In C++/CLI.

Old Class.h:

    7 public ref class Effect

    8 {

    9 private:

   10    Route^ componentChain;

   11 

   12 public:

   13    property Route^ ComponentChain

   14    {

   15       Route^ get(void) { return this->componentChain; }

   16    }

   17 };

New Class.h:

    7 public ref class Effect

    8 {

    9 private:

   10    Route^ componentChain;

   11 

   12 public:

   13    property Route^ ComponentChain

   14    {

   15       Route^ get(void);

   16    }

   17 };

New Class.cpp:

   24 Route^ Effect::ComponentChain::get(void)

   25 {

   26    return this->componentChain;

   27 }

The key is the Class::Property::get and Class::Property::set syntax that I’d not seen documented anywhere before.  Hopefully now that there are two articles about this, it will move up higher in Google searches.

Related

This page is a static archival copy of what was originally a WordPress post. It was converted from HTML to Markdown format before being built by Hugo. There may be formatting problems that I haven't addressed yet. There may be problems with missing or mangled images that I haven't fixed yet. There may have been comments on the original post, which I have archived, but I haven't quite worked out how to show them on the new site.

Sorry, new comments are disabled on older posts. This helps reduce spam. Active commenting almost always occurs within a day or two of new posts.