Re: TString question

Nick van Eijndhoven (Nick@fys.ruu.nl)
Sun, 15 Mar 1998 16:54:52 +0100 (MET)


*** Radovan Chytracek wrote :
>
> On Sun, 15 Mar 1998, Nick van Eijndhoven wrote:
>
> > > class MyClass {
> > >
> > > private:
> > >
> > > TString *s;
> > > ...
> > >
> > > public:
> > >
> > > char *GetData()
> > > {
> > > return( s->Data() );
> > > {
> > > ...
> > > };
> > >
> > > Use it as:
> > >
> > > MyClass mcls;
> > >
> > > char *ch = mcls.GetData();
> > >
> > > Happy ROOTing
> > >
> > > Radovan
> > >
> > >
> > >
> > Hi Radovan,
> > Since I need the TString contents in char* format in a member function
> > of the same class (in which then I use the char* as an argument for the
> > invokation of a member function of another class) the above construction
> > should not be needed according to standard C++.
> > So to me it seems we have some sort of CINT problem here.
>
> TString.h contains the following prototype for its public method Data():
>
> const char *Data() const { return fData; }
>
> I think that you can call the GetData() method whereever you need to get
> TString data in (char *) format. To be sure you get data from TString
> in ( char * ) format the function GetData() may be rewritten as :
>
> char *GetData()
> {
> return( StrDup( s->Data() );
> }
>
> In this case you get (char *) instead of (const char *) because StrDup()
> is defined in TString.h as:
>
> extern char *StrDup(const char *str); // duplicate str, free with delete []
>
> So you can use the copy of TString data without worry
> that you overwrite TString object data.
>
> Hope this helps
>
> Radovan
>
>
>
Hi Radovan,
Thanks for your elaborate explanation; seems indeed that the above should
work.
However, I am very stubborn (as the ROOT team knows) and I encountered
the following problem, which to my opinion indicates that there is some
ROOT/CINT problem involved.
As I said before, I have :

TString* s; // In the private area of Myclass

// Next 2 lines are in the member function void Myclass::doit1()
s = new TString("test");
cout << " First test output " << s->Data() << endl;

// Next 2 lines are in the member function void Myclass::doit2()
cout << " Here comes the second." << endl;
cout << " Second test output " << s->Data() << endl;

Now the crazy thing is that the 'First test output' is o.k.,
whereas in the second case I get an error due to 'illegal pointer
to class object'.

To my opinion this points to a ROOT/CINT problem, which might be
solved in version 2.0 already (I am running ROOT 1.03/09 g++ on
DECAlpha).

Sorry to have polluted this roottalk list with all these mails,
I just realized that this stuff should go to ROOTDEV.
I will do so in case more mails on this subject are needed.

Cheers,
_/_/ _/ _/ _/_/_/_/ _/ _/
_/ _/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/_/
_/ _/_/ _/ _/ _/ _/
_/ _/ _/ _/_/_/_/ _/ _/

*----------------------------------------------------------------------*
Dr. Nick van Eijndhoven Department of Subatomic Physics
email : nick@fys.ruu.nl Utrecht University / NIKHEF
tel. +31-30-2532331 (direct) P.O. Box 80.000
tel. +31-30-2531492 (secr.) NL-3508 TA Utrecht
fax. +31-30-2518689 The Netherlands
WWW : http://www.fys.ruu.nl/~nick Office : Ornstein lab. 172
----------------------------------------------------------------------
tel. +41-22-7679751 (direct) CERN PPE Division / ALICE exp.
tel. +41-22-7675857 (secr.) CH-1211 Geneva 23
fax. +41-22-7679480 Switzerland
CERN beep : 13+7294 Office : B 160 1-012
*----------------------------------------------------------------------*