5

Lets say in my main method, I declare a const int array pointer pointing to an array created on the heap. I then want to initialize it's values (using the memory address) in a constructor TryInitialize() and then print them out. This is not working and I'm wondering what I'm doing wrong? Thanks!

#include "stdafx.h"
#include "part_one.h"
#include <string>
#include <iostream>

using namespace std;

string createTable(unsigned int* acc, double* bal, int n) {
    string s;
    char buf[50];

    for (int i = 0; i < n; i++) {
            sprintf_s(buf,"%7u\t%10.2f\n",acc[i], bal[i]);
            s += string(buf);
    }

    return s;
}



int _tmain(int argc, _TCHAR* argv[])
{

    const int *tempInt = new const int[4];
    TryInitialize(tempInt);
    std::cout << tempInt[1] << endl;

    system("pause");

    return 0;
}

And here is my code for my constructor:

#include "part_one.h"


TryInitialize::TryInitialize(void) {

}

TryInitialize::TryInitialize(int constInt[]) {
    constInt[0] = 8;
    constInt[1] = 0;
    constInt[2] = 0;
    constInt[3] = 8;
}
Csq
  • 5,775
  • 6
  • 26
  • 39
Taylor C. White
  • 836
  • 1
  • 12
  • 30
  • What do you mean "not initialize it until later"? Do you plan on having a user input it? How do you plan to initialize it? If you plan on having the user input it via `std::cin` then this is a duplicate of http://stackoverflow.com/questions/12279601/are-there-any-tricks-to-use-stdcin-to-initialize-a-const-variable – Rapptz Feb 01 '13 at 01:15
  • 2
    You shouldn't. That is the point of `const`. – Csq Feb 01 '13 at 01:15
  • 2
    Where is your class? Having a constructor modify its *parameters* is a terrible idea (so terrible that the only standard library class to do so got replaced). It's supposed to initialize members of the new object, not have unrelated side-effects. – Ben Voigt Feb 01 '13 at 01:22
  • I know, but that is my goal I'm trying to see if that is even possible. Pseudo idea: Main method- const int x; NewClass(x) constructor- initialize x; sys print x val; – Taylor C. White Feb 01 '13 at 01:29
  • 1
    @TaylorWhite, while there are useful C++ idioms where an object changes its parameter (for instance, aquire a lock and free it automatically in the destructor), the parameter isn't `const`. – StoryTeller - Unslander Monica Feb 01 '13 at 01:51

3 Answers3

3

You should not change a const value.

For what you trying to accomplish I'd recommend declaring a non-const pointer and a const pointer and assigning the non-const one to the const one after the initialization:

int _tmain(int argc, _TCHAR* argv[])
{
    const int *tempTempInt = new int[4];
    TryInitialize(tempInt);
    const int* const tempInt = tempTempInt;
    std::cout << tempInt[1] << endl; //this is now constant.

    system("pause");

    return 0;
}

Also pay attention where you put the const in the pointer declaration:

const int* const tempInt = tempTempInt;

In the declaration above the second const means that you cannot change the pointer; the first const means that you cannot change the pointed value(s).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Csq
  • 5,775
  • 6
  • 26
  • 39
  • 1
    Rephrase your last sentence, right now it's unclear and probably wrong. – Ben Voigt Feb 01 '13 at 01:23
  • Is there any way to not do it that way? I understand the purpose of constant, but I'm trying to see if I can get around it. For instance lets say I pass by reference the memory address, then set the values in another class. Thanks for the response by the way! – Taylor C. White Feb 01 '13 at 01:26
  • @BenVoigt Thanks, it had a typo as well – Csq Feb 01 '13 at 01:26
  • 1
    @user2031046 This is C++. Everything has a workaround. (That is why I began the answer with should not and not cannot.) But why use hacks when you can write good code instead? – Csq Feb 01 '13 at 01:29
  • I'm just trying to see what I can get away with :) – Taylor C. White Feb 01 '13 at 01:32
  • @TaylorWhite Getting around it is not a good idea as it may fool the optimizer and you get unexpected results. – Csq Feb 01 '13 at 01:41
1

You declare the pointer as const int*. The const modifier means that you cannot change the array values.

Either remove the const, or create an initializer method for it that can allocate the array and return it (unlike a constructor).

const int* init_my_array()
{
  int * ret = new int[4];
  ret [0] = 8;
  ret [1] = 0;
  ret [2] = 0;
  ret [3] = 8;
  return ret;
}
...
const int *tempInt = init_my_array();
gsamaras
  • 71,951
  • 46
  • 188
  • 305
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

You don't. Why? Because if it's const, then it can't be changed once the object has been constructed. Note: Even setting it is effectively changing its value from its uninitialized value, which goes against the definition of const to begin with.

Carl
  • 43,122
  • 10
  • 80
  • 104