2
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.

Bryan
  • 117
  • 3
  • 13
  • I'm unsure why you need a copy of `p` instead of simply using it directly. If you need to change `p`, then why not just change your `operator+` to accept `Polynomial p`? At any rate, if you need a copy of `p` for some weird reason, inside the body of the operator, then just make one: `Polynomial pCopy(p);` or, if you're using C++11: `auto pCopy(p)`. – Nik Bougalis Oct 23 '15 at 19:18
  • 4
    I read the question 4 times and I still dont know what you want to achieve. What has the const specifier to do with accessing a member function? – Chris Oct 23 '15 at 19:18
  • @Chris The p is a constant and I can't access the functions tied to it. So if I understand it correctly, you can make a copy which will allow you to access those functions. – Bryan Oct 23 '15 at 19:21
  • @Bryan You should be able to use any of the functions that only read from `p`, you can't use any of the functions that would modify `p`. – Barmar Oct 23 '15 at 19:22
  • @Bryan you _can_ access functions that are marked as `const`. Obviously, you can't access functions that aren't, since `p` is a `const` reference. Generally, if a member function doesn't modify the state of the object, you should mark it as `const`. – Nik Bougalis Oct 23 '15 at 19:22
  • 4
    I would say that there are missing `const` in the methods... – Jarod42 Oct 23 '15 at 19:22
  • If you make your `Polynomial` _const correct_ you won't need it to be changeable to access its functions. You may start with changing `+` signature to `Polynomial operator + (const Polynomial& p) const` – Lol4t0 Oct 23 '15 at 19:25
  • Just make it `const Polynomial& operator + (const Polynomial& p) { return p; }` –  Oct 23 '15 at 19:30
  • 1
    @Bryan Try reading http://stackoverflow.com/questions/4421706/operator-overloading so you can make an idea of how to properly implement the most typical overloaded operators. – vsoftco Oct 23 '15 at 19:39
  • @vsoftco Thank you much. – Bryan Oct 23 '15 at 19:41
  • @DieterLücking `operator+` returning a reference? Weird. – juanchopanza Oct 23 '15 at 19:55

4 Answers4

6

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);
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
3

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.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • "there are situations when a copy is necessary" Most definitely not here. Addition just should should not modify sum components – Lol4t0 Oct 23 '15 at 19:34
  • I think the only drawback from passing by value is that if the value is returned, RVO is inhibited. – juanchopanza Oct 23 '15 at 20:04
  • @juanchopanza Hmm, I didn't know that. Do you know what is the reason? I would have though that NRVO kicks in also. – vsoftco Oct 23 '15 at 20:24
  • @vsoftco I think it is just hard/impossible to implement. – juanchopanza Oct 23 '15 at 20:27
  • @juanchopanza What I actually meant was the following: is there any difference from doing RVO on `p` and doing NRVO on a locally defined `pcopy`? I thought the latter is optimized out. – vsoftco Oct 23 '15 at 20:29
  • As long as `pcopy` isn't the function parameter and has the right type, RVO can kick in. NRVO to be precise. – juanchopanza Oct 23 '15 at 20:34
1

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.)

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
0

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;
Chris
  • 1,226
  • 1
  • 12
  • 26
  • 2
    using `const_cast` to remove `const` is undefined behavior. – NathanOliver Oct 23 '15 at 19:29
  • I would just remove the whole thing as not using it defeats the purpose of doing it. – NathanOliver Oct 23 '15 at 19:38
  • 1
    @NathanOliver only if the initial object passed as reference was a `const`. If the object was non-`const`, then passing by `const` reference and removing `const` via a `const_cast` should be ok. But in any case, casts should be avoided as much as possible, and in this case you're right, `const_cast` is not the way to go. – vsoftco Oct 23 '15 at 19:50