Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename class members by inheriting class in C++

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); };

    };
like image 991
Dony Avatar asked May 15 '14 12:05

Dony


1 Answers

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;
}
like image 106
Michael J Avatar answered Sep 18 '22 01:09

Michael J