Archived

This forum has been archived. Please start a new discussion on GitHub.

slice2cpp does not support copy construct and operator =

the struct class class that was generated by slice2cpp does not support copy construct and operator =,
maybe it too simple, but is important to me, because it's ISO standard.

sample:

slice:

sequence< string > NameList;

class FlatTree
{
string Id;
string Type;
string Name;
NameList Parents;
NameList Members;
};

the generated cpp code list bellow:

class FlatTree : virtual public ::Ice::Object
{
public:

void __copyMembers(::jf::server::FlatTreePtr) const;
virtual ::Ice::ObjectPtr ice_clone() const;

static const ::std::string __ids[2];
virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;
virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;
virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;
static const ::std::string& ice_staticId();

::std::string Id;

::std::string Type;

::std::string Name;

::NameList Parents;

::NameList Members;

virtual void __write(::IceInternal::BasicStream*) const;
virtual void __read(::IceInternal::BasicStream*, bool);
virtual void __write(const ::Ice::OutputStreamPtr&) const;
virtual void __read(const ::Ice::InputStreamPtr&, bool);

static const ::Ice::ObjectFactoryPtr& ice_factory();

private:

static ::Ice::ObjectFactoryPtr _factory;
};

Now, I need copy a FlatTree object:

FlatTreePtr o( new FlatTree() );
...
FlatTree o2( * o ); // copy construct is not support
FlatTree o2 = * o; // operator = is not support

then, I must do this:

FlatTreePtr o2( new FlatTree() );
o->__copyMembers( o2 );

It's feel not beautiful.

I know, mostly I needn't call copy construct function, because the Ice::Handle can copy a object to anywhere, but now I really to do this. :-)

Can slice2cpp support C++ copy construct and operator = ?

Comments

  • marc
    marc Florida
    You should use ice_clone(). __copyMembers() is an internal function (everything in Ice that starts with underscores is internal). For example:

    FlatTreePtr o = FlatTree::dynamicCast(o2->ice_clone());

    However, off hand, I can't think of any reason why we cannot support the copy constructor or assignment operator for classes. Note that for structs, there are compiler-generated copy constructors and assignment operators.
  • As Marc says, for structures, the compiler-generated default copy constructor and assignment operator do the right thing.

    For classes, copy construction and assignment are deliberately disabled. (All classes ultimately derive from IceUtil::noncopyable, which hides the copy constructor and assignment operator -- see page 208 of the doc.) That's because class instances are reference counted and must be heap-allocated, and direct assignment and copy construction don't make sense. If you want to make a copy of a class, call ice_clone() and assign the return value to a smart pointer for the class. (See page 210 in the doc.)

    Cheers,

    Michi.
  • Ice 2.2 will provide one-shot constructors for exceptions and classes, so you can initialize the data members in a single statement (instead of having to default-construct the instance and then assign each data member).

    Ice 2.2 will also provide copy constructor and assignment operator for classes, so you can assign classes to each other.

    Cheers,

    Michi.