1

I am trying to implement the rule of five using the swap idiom.

however, i have internal member fields that are const.

I tried at first to make const_cast, but as I see it probably impossible in this case.

I got into conclusion that the best way will be to turn the member field into ptr to const.

I tried to do this via the initalization list, but cannot find the right syntax. the following doesn't compile:

HashMap<KEY, VALUE>::HashMap(double threshold)
: *_thresholdPtr(threshold)

...

const double * _thresholdPtr;

I will be grateful to your help, first if ptrs to const are the best way in this case, and how to do it if yes.

thanks!

EDIT: I know that const double* is not like double * const. but what I'm trying to do is to recieve a double number, and attach it as a const double to a pointer which is not const (in order to be able to use the swap)

Nissim Levy
  • 165
  • 10
  • 1
    Either the class members cannot be `const`, or moving/assigning the objects does not move/assign these members. Those are your options. – Sam Varshavchik Sep 14 '19 at 16:54
  • **Obviously** if your field are `const`, they cannot be modified as it is the whole purpose of making an object `const`. By the way, `const_cast` should rarely be used. You should almost always fix the constness at the source and make any necessary change in the whole chain. – Phil1970 Sep 14 '19 at 17:06
  • And you should always format your code in a question. Unformatted code is harder to read. By putting some effort in writing your question, you increase the chance that people might help you. Also showing all required code won't hurt. – Phil1970 Sep 14 '19 at 17:10
  • It's not clear what you want here. You created a class member variable that is a pointer. What exactly do you want it to point to? – David Schwartz Sep 14 '19 at 17:29
  • If `_thresholdPtr` is a pointer, you must first make it actually point somewhere before you can dereference it. In the code you show you don't make it point anywhere. Why do you have it be a pointer in the first place? What problem is that supposed to solve? – Some programmer dude Sep 14 '19 at 17:29
  • @NissimLevy `HashMap::HashMap(double threshold): *_thresholdPtr(threshold)` What does this means? Or what were you trying to say? What did you actually want? Perhaps you meant to write to `HashMap::HashMap(const double* thresholdPtr) : _thresholdPtr(thresholdPtr)`? – ALX23z Sep 14 '19 at 17:41
  • 1
    Note that `const doublt *_thresholdPtr` is NOT const -- it is a NON CONST pointer to a const double. So there's no problem with changing the pointer in the swap or assignment or wherever. – Chris Dodd Sep 14 '19 at 17:44

1 Answers1

0

The order where your const declaration happens in relation to the data type matters when declaring pointers. const double *_thresholdPtr; creates a double that can be pointed to, but itself cannot be changed. double *const _thresholdPtr; creates a constant pointer to an existing double.

I'm not sure if it will help, but I ran into a similar issue with some code. This post helped me understand the difference.

EDIT: As comment below, the difference is where pointer is declared.

  • `double *const foo:` is a const pointer to non-const double, the two you mentioned are equivalent. – Quentin Sep 14 '19 at 18:11