Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C++ is there a way to define a function that takes any size variable?

Status
Not open for further replies.

Pommie

Well-Known Member
Most Helpful Member
I know you can overload functions but is there a way to pass any type variable to a function and then tell it's size within the function?
For example, say I wanted to be able to store any variable in EEPROM, can I have a function that will store any size variable?

Thanks,

Mike.
 
I think they use void pointers for that very reason.. However!!! If the eeprom has to cater for all sizes, then the biggest size will need to be reserved.. so cast ANY size to a 64bit long long and store that..
 
I feared that might be the case. Guess I'll have to overload it in every way I can think of.

Thanks,

Mike.
 
I normally create a union for this case.. 4 bytes, two int's and a long.. Once you recieve the data ( via a void pointer ) you can try sizeof() to see if it returns the correct amount of bytes.. Then place it into the union..
 
I can't see how sizeof can work with a void pointer but will play around with this tomorrow - 9pm here.

Mike.
 
For such as EEPROM reading or writing, I just pass the data size (or count) as well as a pointer to the data start & eeprom address..
No extra variables, nothing reserved etc.

Or if you wanted a single variable, put all that in a struct{} and pass a pointer to the structure.
 
You can use a templated function to handle any type, as shown below. This automatically generates code for each object type that needs writing.
C++:
template<typename T>
void eeprom_write_object(int offset, const T &obj)
{
   const unsigned char *ptr = (const unsigned char*)&obj;
   for(int i = 0; i < sizeof(T); i++)
      eeprom_write_byte(offset + i, *ptr++);
}

The other way, as mentioned above is to pass in the size of the object:
C++:
void eeprom_write_object2(int offset, const void *obj, int objSize) { /* ... as above */ }

You can use a macro to not have to specify the object size if you wanted, but it's a little gross.
C++:
#define EEPROM_WRITE(offset, obj) do{eeprom_write_object2(offset, &obj, sizeof(obj);}while(0)
 
Last edited:
One way to do this in c++ would be to derive all types you want to pass from one base class, e.g. MyObject

Code:
enum { OBJECT1 = 1, OBJECT2 = 2};

class MyObject
{
    public:
    virtual int Type() = 0;
};

class Object1 : public MyObject
{
    public:
    virtual int Type() {return OBJECT1;}
};

class Object2 : public MyObject

{
    public:
    virtual int Type() {return OBJECT2;}
};

Then you can use them like:

Code:
....

    Object1 obj1;
    Object2 obj2;
    
    DoSomething(&obj1);
    DoSomething(&obj2);
    
..


void DoSomething(MyObject* pObj)
{
    switch (pObj->Type())
    {
    case OBJECT1:
        {
        unsigned int size = sizeof(Object1);
        Object1* p = (Object1*)pObj;
        
        .....
        break;
        }
    case OBJECT2:
        .....
        break;
    default:
        assert(false);
    }
}

If all you are interested in is the size, you could add a virtual function to the base class which returns the size of the object, and simply overload it for each derived class.

Alternatively, if this suits your application, you could tell each derived class how to store itself, and simply have:

Code:
class MyObject

{

    public:

    virtual int Type() = 0;
    virtual void Store() = 0;
};


class Object1 : public MyObject

{

    public:

    virtual int Type() {return OBJECT1;}
    virtual void Store() {code to store this object, probably in cpp file}

};


class Object2 : public MyObject


{

    public:

    virtual int Type() {return OBJECT2;}
    virtual void Store() {code to store this object, probably in cpp file}
};


....
    Object1 obj1;
    Object2 obj2;
    
    ...
    
    obj1.Store();
    obj2.Store();
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top