I can change the values even when I use const word with my own array class template

Doğuş Hamdi

So I'm trying to write my own array template and everything works until i try to create a const object of my class template. in main.cpp I create the object with the copy contructor and I change it which I would expect to not work but it works. Help would be appreciated :D

main.cpp

# include "Array.hpp"

int main( void ) {

    Array<int> l = 1;
    l.setValue(5, 0);


    const Array<int> abc(l);
    std::cout << abc[0] << std::endl;
    abc[0] = 3;
    std::cout << abc[0] << std::endl;

    return (0);
}

Array.tpp

#ifndef ARRAY_TPP
# define ARRAY_TPP

# include "Array.hpp"

template<class T>
class Array {
private:

    int size_;
    T *array_;

public:
    Array() : size_(0), array_(new T[size_]) {};

    Array(int n) : size_(n), array_(new T[size_]) {};

    Array(Array const& src) : size_(src.size()), array_(new T[src.size()]) {
        for (int i = 0; i < src.size(); ++i) {
            array_[i] = src[i];
        }
    };


    Array& operator=(Array const& copy) {
        size_ = copy.size();
        delete[] array_;
        array_ = new T[size_];
        for (int i = 0; i < size_; i++)
            array_[i] = copy[i];
        return (*this);
    }

    T& operator[](int n) const {
        if (n < 0 || n >= size_)
            throw std::out_of_range("out of range");
        return (array_[n]);
    }


    int size(void) const { return (size_); };

    void setValue(T value, int n) {
        if (n < 0 || n >= size_)
            throw std::out_of_range("out of range");
        array_[n] = value;
    }

    ~Array() { delete[] array_; };
};

#endif
463035818_is_not_a_number

The issue is this:

T& operator[](int n) const {
    if (n < 0 || n >= size_)
        throw std::out_of_range("out of range");
    return (array_[n]);
}

Because this is declared to be a const method, it can be called on a const Array. Though, it returns a non-const reference to the element. Because Array stores the elements via a T *, only that pointer is const in a const Array while modifiying the elements via that pointer is "fine".

You need two overloads of operator[]:

 T& operator[](int n);
 const T& operator[](int n) const;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I use custom values of UIControlState for my own control?

How can I use my own external class in CakePHP 3.0?

When I should use my own namespaces?

Why I have this error when I try to use my own CollectionViewCell class?

Why I can change const array in TypeScript

How can I use universal links when I call openURL inside my own app?

How can I set class properties when the values are specified as an array?

Can I render a Template in Rocket with my own serialized struct?

How can I wrap std::format() with my own template function?

I created my own template, but how can I add the blogger lightbox function on my template code

How can I use the Xamarin.Forms.Setter class in my own ContentView (custom control)?

Can I build a java class in jdk14 but use java 11 for my own library?

my datalist keeps showing repeated values even when I use ui.unique

In Rust, can I instantiate my const array without hard-coding in the values? Compile-time evaluation?

Can I change my own password in Active Directory using Powershell

How can I change my own user ID?

I can't change my own folders (newbie alert)

how can I list-initialize my own class?

Why can I not access UmbracoHelper in my own class

How can I specialize a standard concept for my own class?

How can I pass my class into its own constructor?

How can I use a public array from my Model in my Seeder to insert values in my table?

Can I use "token pasting operator" with 'const' template arguments?

How can I get Jackson to deserialize into my own Array implementation

How can I use my own build of a package with stack?

Can I use the special generic syntax for my own types?

How can I use realm with UITableViewController with my own sorting order?

Can I use HTC Vive to test my own developments?

How can I use my own icons in dijit.menuItems?