Put const and non-const objects in an array?

Is there a way to put const and non-const objects in an array?
I am trying to make most of the array elements const to save memory (in Arudino micro controller).
But a few of the array elements can not be const.
The following is one of my failed attempts to polymorph const and non-const elements into an array.
The array of const and non-const objects is on line 40.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

class Base
{
    public:
        //overloaded functions
        virtual void func() = 0;      //non-const function
        virtual void func() const = 0; //const function
};

class ConstN : public Base //class with no variables (just constants)
{
    private:
        const int n;
    public:
        ConstN(): n(3) {}
        void func() const
        {
            std::cout << "ConstN::n=" << n << "\n";
        }
};

class VarN : public Base  //class with variable
{
    private:
        int n;
    public:
        VarN(): n(3) {}
        void func()
        {
            std::cout << "VarN::n=" << ++n << "\n";
        }
};

int main()
{
    const ConstN c;             //object is const to save memory (in Arudino micro controller)
    VarN v;

    Base* ptrBase[] = {&c, &v}; //is there a way to put const and non-const objects in an array?

    for (int i=0; i<2; i++)
    {
        ptrBase[i]->func();     //polymorphism needs func() defined in Base
    }
}
Last edited on
Is there a way to put const and non-const objects in an array?
No, an array can have just one type.
Thank you coder777.
it depends sometime if you declare a attribute as mutable.

Note also func() and func() const might be ambiguous at runtime in time that both method are valid in certain cases . So probably a better idea to give different names

Note also that because both methods are pure , you must override them in all derived classes.
Thank you Ericool.

This runs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

class Base
{
    public:
        virtual void func() const = 0; //const function
};

class ConstN : public Base
{
    private:
        const int n;
    public:
        ConstN(): n(3) {}
        void func() const       //function is const for n
        {
            std::cout << "ConstN::n=" << n << "\n";
        }
};

class VarN : public Base
{
    private:
        mutable int n;          //mutable
    public:
        VarN(): n(3) {}
        void func() const       //function is const to match Base, although n is really mutable
        {
            std::cout << "VarN::n=" << ++n << "\n";
        }
};

int main()
{
    const ConstN c;
    const VarN v;                    //mutable const

    const Base* ptrBase[] = {&c, &v}; //no way to put const and non-const objects in same array

    for (int i=0; i<2; i++)
    {
        ptrBase[i]->func();
    }
}
Last edited on
you're welcome.
Topic archived. No new replies allowed.