开发者

C++ Set MIL Question

I am trying to initialize a set with more than one member in my member initialization list and I am not sure about the syntax. The ** is where I am getting my syntax error (without the ** of course). Thanks. Here is an example:

//=============================================================================
class myClass_t
{
//-------------------------------------------开发者_运维知识库----------------------------------
public: // FUNCTIONS
//-----------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Constructor
    myClass_t
        (
        )
    :
        BaseClass_t( IDD_BASEPAGE ),
        **mCapabilities( 1, 2 ),
        mDevice( mCapabilities )
    {
    }

    //-------------------------------------------------------------------------
    // Destructor
    ~myClass_t
        (
        )
    {
    }

//-----------------------------------------------------------------------------
private: // DATA
//-----------------------------------------------------------------------------

    fdx::myDevice_t               mDevice;    
    std::set<int>                 mCapabilities;

}; // end of class

} // end of namespace


If you can use the Boost Assign library, the thing you ask for is easy:

#include <boost/assign/list_of.hpp>
...
mCapabilities(boost::assign::list_of(1)(2))
...

I've just tested it on MSVC8 and it works fine.


There is no such constructor available in set with which you can insert the elements. You need to explictly call set::insert method in the body of the constructor. i.e. you need to do mCapabilities.insert(1);mCapabilities.insert(2);.


The set, like a standard containers, provides a constructor that takes an iterator range as input to construct the collection.

So you cannot directly put the elements that you want to insert into the constructor but you can have a static array and use that to initialise he members:

class myClass_t {
    static int const m_init_values[];
    static unsigned const m_init_size;
    …
};

int const myClass_t::m_init_values[] = { 1, 2 };
unsigned const myClass_t::m_init_size =
    sizeof m_init_values / sizeof m_init_values[0];

Note that you need to define the static constant outside of the class, as shown here.

Now you can use these values in your constructor:

myClass_t()
    : BaseClass_t(IDD_BASEPAGE)
    , mCapabilities(&m_init_values, &m_init_values + m_init_size)
    , mDevice( mCapabilities )
{ }

A last remark: your formatting is very space consuming, and consequently requires a lot of scrolling to read the code. You should ask yourself whether the spurious delimiter comments and redundant line breaks really help readability. It’s usually advisable not to have to scroll to read one connected piece of code.


set<int>::set() doesn't accept the arguments you have mentioned. To see the possible ways of constructors, you can refer to the set constructor example.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜