Union class member of root class


_ROOT Version:6.22.02
_Platform:mac os x
_Compiler:homebrew version


could I generact a root class with union class members

as a tutorial

#include <TObject.h>
class ag_t: public TObject
{
	public:
		double a;
		union{double c; double b;};

		ag_t():a(3),c(-1.0){};
		virtual ~ag_t(){};


		ClassDef(ag_t,1);
};
rootcling -f out.cxx ag_t.hh

the out.cxx file is generate with errors

       ¦ ¦ R__c = R__b.WriteVersion(ag_t::IsA(), kTRUE);
       ¦ ¦ TObject::Streamer(R__b);
       ¦ ¦ R__b << a;
   >>  ¦ ¦ R__b.StreamObject(&(),typeid());
error: expected expression
      R__b.StreamObject(&(),typeid());

Storing union in ROOT file is not supported yet.

rootcling -f out.cxx ag_t.hh

I recommend that you write a linkdef file so that you can benefit from using the “new”/“current” I/O scheme:

// ag_LinkDef.h
#pragma link C++ class ag_t+;

and using
rootcling -f out.cxx ag_t.hh ag_LinkDef.h
even with your existing code this will lead to a dictionary source file that can be compiled … it will still no store the union but will simply warn you at run-time.

Since both union member are double you can work around the limitation with:

class ag_t: public TObject
{
	public:
		double a;
#ifdef __CLING__
               double b;
#else
		union{double c; double b;};
#endif
		ag_t():a(3),c(-1.0){};
		virtual ~ag_t(){};


		ClassDef(ag_t,1);
};

solved, thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.