SetBranchAddress() for inherited classes

Dear Rooters

I have a question how to SetBranchAddress() using the following inherited classes.

   class MyClass   //as base class
   class MyClassA: public MyClass 
   class MyClassB: public MyClassA
   class MyClassC: public MyClassB 

When doing the following:

   MyClass *myclass = 0;
   if (strcmp(Class, "MyClassC") == 0) { 
      tree->SetBranchAddress("MyBranch", &((MyClassC*)myclass));
   } else if (strcmp(Class, "MyClassB") == 0) {
      tree->SetBranchAddress("MyBranch", &((MyClassB*)myclass));
   } else {
      tree->SetBranchAddress("MyBranch", &((MyClassA*)myclass));
   }

   for (Int_t i=0; i<entries; i++) {
      tree->GetEntry(i);
      arr[i] = myclass->GetData();
   }

I get the following compiler warning:
"warning: argument to ‘&’ not really an lvalue; this will be a hard error in the future"
although the program works fine.

However, when I do the following:

   MyClass *myclass = 0;
   if (strcmp(Class, "MyClassC") == 0) { 
      myclass = ((MyClassC*)myclass);
      tree->SetBranchAddress("MyBranch", &myclass);
   } else if (strcmp(Class, "MyClassB") == 0) {
      myclass = ((MyClassB*)myclass);
      tree->SetBranchAddress("MyBranch", &myclass);
   } else {
      myclass = ((MyClassA*)myclass);
      tree->SetBranchAddress("MyBranch", &myclass);
   }

   for (Int_t i=0; i<entries; i++) {
      tree->GetEntry(i);
      arr[i] = myclass->GetData();
   }

everything is fine w/o compiler warning.

What is the difference, and is there a better way?

Thank you in advance.
Best regards
Christian

Christian,

I do not understand why you want to complicate things when things are simple. Simply do:

MyClass *myclass = 0; tree->SetBranchAddress("MyBranch", &myclass);

This will work independently for all your inheritance modes.

Rene

Dear Rene

Thank you for this clarification, I am not sure, why I did this. Probably, since I have to do “if-else if-else” anyhow,
I forgot to put SetBranchAdress after the if-statement.

Best regards
Christian