7

I would like to "rename" some members of my class ofVec4f.

I know it's impossible in strict C++ but could I create a new class that inherits from my class and declare new members which are aliases or pointers to the original members?

I tried the following:

class ofVec4fGraph : public ofVec4f {

    public :
        float& minX;
        float& maxX;
        float& minY;
        float& maxY;

        ofVec4fGraph(float _minX,float _maxX, float _minY, float _maxY )
                    : minX(_minX), maxX(_maxX), minY(_minY), maxY(_maxY)
                    { ofVec4f(_minX, _maxX, _minY, _maxY); };

    };
Axalo
  • 2,953
  • 4
  • 25
  • 39
Dony
  • 95
  • 1
  • 8
  • You should call base constructor in initialization list: ...`) : ofVec4f(_minX, _maxX, _minY, _maxY), `... – marcinj May 15 '14 at 12:45
  • In bracket is the wrong place to do that ? – Dony May 15 '14 at 12:48
  • it would be ok in Java, but in C++ your code is only creating on stack temporary value of type ofVec4f. – marcinj May 15 '14 at 12:52
  • Note that each reference will take `sizeof(float*)` more bytes, and accessing them will require dereferencing a pointer. Consider using member function if possible - there's no performance penalty, compilers are smart enough to inline the functions. – user202729 Nov 20 '19 at 06:34

3 Answers3

5

Your class should be:

class ofVec4fGraph : public ofVec4f {
public :
  float& minX;
  float& maxX;
  float& minY;
  float& maxY;

  ofVec4fGraph(float _minX,float _maxX, float _minY, float _maxY )
                    : ofVec4f(_minX, _maxX, _minY, _maxY), minX(x), maxX(y), minY(z), maxY(w)
     {};

};

Constructor chaining is not possible in C++. You use initializer list to initialize base class.

You can now use this as:

ofVec4fGraph obj;
fun1(obj.x, obj.y);
fun2(obj.maxX, obj.minY);
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
5

I think this may be what you want.

#include <iostream>

class CBase
{
public:
    CBase() : a(0), b(0), c(0) {}
    CBase(int aa, int bb, int cc) : a(aa), b(bb), c(cc) {}
    int a, b, c;
};

class CInterface
{
public:
    CInterface(CBase &b) 
    : base(b), x(b.a), y(b.b), z(b.c) 
    {
    }
    int &x, &y, &z;
private:
    CBase &base;
};

int main() 
{
    CBase      base(1, 2, 3);
    CInterface iface(base);

    std::cout << iface.x << ' ' << iface.y << ' ' << iface.z << std::endl;
    std::cout << base.a << ' ' << base.b << ' ' << base.c << std::endl;

    iface.x = 99;
    base.c = 88;

    std::cout << iface.x << ' ' << iface.y << ' ' << iface.z << std::endl;
    std::cout << base.a << ' ' << base.b << ' ' << base.c << std::endl;

    return 0;
}
Michael J
  • 7,631
  • 2
  • 24
  • 30
0
Is not à job for inherited class ? 

Not necessarily.

Proper Inheritance dictates that you only inherit publicly when the derived class is substitutable for base class. In this case you are implementing derived class in terms of base class, here the preferred approach is to use private inheritance, or yet better object composition. And composition is better than inheritance. You should either use the approach described by @Michael J, or use private inheritance.

class Base { protected: int x; };
class Derived: private Base { 
 public:
  int getValue(){ return x;} // name the method whatever you like.
};

Also learn why public data member is bad

Community
  • 1
  • 1
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • My class, ofVec4f, from openFrameworks main.h header as his member already in public – Dony May 15 '14 at 15:55