Message: Re: G4VSolid to G4Box Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: G4VSolid to G4Box 

Forum: Geometry
Re: Question G4VSolid to G4Box (Manuel Castro Avila)
Re: None Re: G4VSolid to G4Box (Marc Verderi)
Re: None Re: G4VSolid to G4Box (Manuel Castro Avila)
Date: 08 Apr, 2013
From: Michael H. Kelsey <Michael H. Kelsey>

On Mon, 08 Apr 2013 15:28:51 GMT, Manuel Castro Avila wrote:
> Hi Marc,
> Thank you very much, I solved my problem with your suggestion.
> I would like your help. This kind of declaration (G4Box*)
> myLogicalVolume .... ( or something like that) I saw in some examples in
> Geant4 but I do not understand that very well. What do that mean? In
> this case (G4Box*) are you forcing that myLogicalVolume->GetSolid to be
> of G4Box type or ..?

Look up "downcasting" in your favorite C++ reference manual (or type "C++ downcasting" into Google). What you're doing is forcing the pointer returned by G4LogicalVolume::GetSolid() to be _interpreted_ as a pointer to a G4Box.

That means when you call member functions, the layout of the object in memory (function addresses, data members, etc.) will be _assumed_ to match G4Box in every way. If that assumption is wrong, you will get either incorrect results, or your job will crash.

There is a better way to do such a downcast in C++, which allows the system to check for you whether the assumption is correct. You may write:

   G4Box* myDaughterBox = dynamic_cast<G4Box*>(myLogicalVolume->GetSolid());
   if (0 != myDaughterBox) {
      // ... do what you want to do here ...

The "dynamic_cast" is a built-in function. If the pointer argument really was created as a G4Box*, the cast will return that same pointer, but with the new type so you can call all the functions you need. If the GetSolid() returned a pointer to something else (*not* a G4Box*), then dynamic_cast<>() returns a null pointer (zero).

    -- Michael Kelsey

 Add Message Add Message
to: "Re: G4VSolid to G4Box"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews

[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]