Bryan
Bryan

Reputation: 117

Copy of a constant?

Polynomial operator + (const Polynomial& p);
//Adds existing Polynomial to p, returning the result

So because we can't change the value of a const, is there a way to make a copy of this p to allow me to access the functions of p?

Sorry guys at work and quite display the entire program, but in Poly.H in the private field it reads: private: List < Term > poly

So in PolyTest.cpp

If I need two polynomials p_add = poly1 + poly2

I need to write the function in the operator+ to add like terms. Hope this is helpful.

Upvotes: 2

Views: 5849

Answers (4)

vsoftco
vsoftco

Reputation: 56547

If you need a copy inside operator+, the most straightforward way of doing it is to pass p by (const) value:

Polynomial operator+ (Polynomial p);

Now p is a copy of the object you passed in that you can mess up with inside operator+. Of course if you pass by const value like

Polynomial operator+(const Polynomial p);

then the copy itself will be const, so you will be able to access only its const member functions.

As mentioned in the comments, sometimes passing by const reference and making sure that you only access the const member function is what you actually need, since you avoid the additional copy. However there are situations when a copy is necessary (most likely not here), such as when implementing the famous copy-and-swap idiom, in which case passing the object by value is the way to go, and the least verbose way of achieving the desired effect.

Upvotes: 3

Chris
Chris

Reputation: 1244

EDIT: You are probably referring to the const specifier at the end of a member function like so:

class Test
    // cannot modify members of Test
    void test() const;
    // can modify members of Test
    void test2();
};

You can only call methods declared as const at the end when operating on a const object.

If you need to modify a member inside a const member function (which is not often the case) you can declare it as mutable

The const specifier in front of Polynomial& p means it is a const reference to a Polynomial and hence you cannot modify the value of p. I has nothing to do with accessing functions. If the method is declared in your class you can ofc access all methods from the same class.

You can however make a simple copy to mess around with the copy but not the reference to the input paramter:

Polynominal p2(p);

or

Polynominal p3 = p;

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180585

If you need to make a copy then just copy it in the function

Polynomial operator + (const Polynomial& p);
{
    Polynomial copy = p;
    // do stuff
}

Now just because p is const doesn't mean you cannot use its members/functions. A typical operator+ would look like

Something operator + (const Something& s);
{
    return Something(this->some_memeber + s.some_memeber,
        this->another_memeber + s.another_memeber);
}

Upvotes: 6

Jon Purdy
Jon Purdy

Reputation: 54981

From the sound of it, you’re missing a const specifier on some member functions of Polynomial, e.g., you might have:

// Can modify *this.
std::vector<double> coefficients() { … }

When you need:

// Cannot modify *this.
std::vector<double> coefficients() const { … }

Adding the const specifier changes the type of this within the member function from Polynomial* to const Polynomial*. On a const reference, you can only invoke const member functions. (You can still invoke const member functions on a non-const reference.)

Upvotes: 1

Related Questions