0

I work with a following class:

class TheirClassA 
  {
    public:
      double GetP() const {return fP;}; // get p
    private:
      double fP; // p
  };

There are of course also constructors/destructor/etc which I'm not including for the sake of brevity. This class is given to me in the data I receive, so I cannot modify its code or anything.

Now in my code, I want to have a class, let's say MyClass, which reads TheirClassA contents and distributes them further. Sort of like an interpreter of the TheirClassA input. In my code, I wish to work with this interpreter, i.e. get data from MyClass instead of TheirClassA. Hopefully I'm still being clear. Something like:

TheirClassA *input = (TheirClassA*)Data->At(i); 
MyClass *interpreter = new MyClass(input);
double P = interpreter->AccessP();

But what is the best way to go about this? A simple way would be to get the values from the input and save it as data members of a MyClass instance. However, some of these members might be Double, or even strings, so I don't want to waste a lot of resources copying/storing values (I will be looping over millions of instances of the input class).

Is there a way for me to somehow define the interpreter MyClass in such a way that its members point at the addresses of the input's members, instead of holding the values? So that the the MyClass::AccessP() looks at the address of the TheirClassA::fP and passes its value?

Looking very much forward to suggested solutions! Thanks in advance.

P.S. Apparently such thing is called a 'wrapper'?

Oli M
  • 1
  • 2
  • That's a lot of text ^^ If your question is simply _"How to point to an address of a class data member?"_, does https://stackoverflow.com/q/670734/5470596 helps? – YSC Feb 05 '19 at 12:47
  • Too long to read for me... And is what you need a [*pointer to members*](https://en.cppreference.com/w/cpp/language/pointer)? – con ko Feb 05 '19 at 12:48
  • For those wondering, my main motivation for creating the MyClass interpreter is that sometimes I work with a different data format/older data version, where the methods can be named differently. So I would have to use e.g. TheirClassB::getP() in an older version. Thus I'd like to work with my own definition of the class, universally across all formats, and just define new constructors/setter methods whenever I move to a new version with a different naming of the input classes. – Oli M Feb 05 '19 at 12:49
  • @jrok my English ability can't support me to read such a large text... – con ko Feb 05 '19 at 12:49
  • I realise it's a lot of text but I had trouble reducing it down. Thanks a lot to everyone patient enough to go through it. – Oli M Feb 05 '19 at 12:51
  • Possible duplicate of [C++: Pointer to class data member "::\*"](https://stackoverflow.com/questions/670734/c-pointer-to-class-data-member) – Moia Feb 05 '19 at 12:55
  • 2
    I did read through it - it's underspecified. Without knowing the details of this system, actual size of the dataset, type of the data, it's impossible to answer, and the potential answers could range from "don't worry, just copy everything" to "implement an efficient intermediate storage and cache mechanism". You're asking "how do I express this program taking some input and returning some output" which is *literally every program ever*. – Bartek Banachewicz Feb 05 '19 at 12:56
  • @YSC and Moia: No I don't think so – Oli M Feb 05 '19 at 12:57
  • There is `addressof` that can help doing this, but doing something like this is bad, it feels like some kind of dynamic reflection that break encapsulation and that cannot work with member variables in a complex hierarchy. So don't do that. – Matthieu Brucher Feb 05 '19 at 13:04
  • Possible duplicate of [Design of a Wrapper Class in C++](https://stackoverflow.com/questions/43584860/design-of-a-wrapper-class-in-c) – Max Langhof Feb 05 '19 at 13:13
  • You are correct, this is called a wrapper class. See also [Wikipedia](https://en.wikipedia.org/wiki/Wrapper_library) or any of the numerous related Q/A on Stackoverflow. In general, the solution is to just wrap each function call instead of returning each member function pointer - the latter is just a way more complicated way to achieve the same as the former. Also note that you have no (legal) way of obtaining a pointer to `fP` in your example (it is a private member), so don't try to do that. – Max Langhof Feb 05 '19 at 13:15
  • @MaxLanghof Thank you Max this was really helpful. Glad to know that what I tried to do is impossible :) The wrapper design looks like an elegant solution. – Oli M Feb 05 '19 at 17:55

0 Answers0