Linked Questions

Popular Questions

Referencing a struct memory as a data type

Asked by At

I am sorry if this question is duplicate.


How to :

struct t{ float[3] smthng = {9,1,2,3}; };
float a = (float*)&t; // to get an 9;

So, i am currently trying to save some space in memory by transitioning the python concept to C++. The idea is to make an stepping object timer (X = X0 + I*STEP) , that will increment itself every time it is called and return the current value stored in it. So when i need to make a wave using frequency formula i can just pass the current value timer value.

I can't use arrays or list since i need to manipulate it a lot more than just stepping function such as resetting it after some interval, adding phasing and different starting points and etc...

So i've made a structure of current, step and end floats, but now i need to be able to range loop over it and to be able to reference only current attribute ( step and end are private in this case.

Timer struct

struct timer{

    void reset(){ // resets a current slot
        this->curr = 0.0f;
    bool check(){ // checks if the next steps overflows the limit
        return (( this->curr+this->step) < this->end );
    float curr,step,end;
        this->curr = 0.0f;
        this->step = 1.0f;
        this->end = 10.0f;
    template < class N, class T >
    timer( N step, T end ){
        this -> curr = 0.0f;
        this -> step = (float) step;
        this -> end  = (float) end;

    template < class N >
    timer( N end ){
        this -> end = (float) end;

std::ostream& operator<< (std::ostream &out, timer &data) {
    out<< "f(x) = "
            <<"I*" << data.step 
            << " [ "<< data.curr <<" / " << data.end << " ]"; 
    return out;

So all i need to do before tackling the __begin and __end methods is to do simple:

timer obj;
float a = &obj; // and to get an obj.current value

I've tried overloading the operator&,operator* and etc. But everytime i try to read a memory of timer struct all i get is conversion error.

So does anyone knows how to reference a struct memory as a specific type ?

Related Questions