1

Currently I'm doing something like this:

struct foo
{
    const int
        *const a,
        *const b, 
        *const c;

    foo(int a, int b, int c)
            : a(_a), b(_b), c(_c)
    {
        *_a = a;
        *_b = b;
        *_c = c;
    }

private:
    int _a[1], _b[1], _c[1];

};

but is there a way to do this without putting in the second set of pointers (_a, _b, _c)?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48

1 Answers1

4

but is there a way to do this without putting in the second set of pointers (_a, _b, _c)?

Sure. You can use:

foo(int a, int b, int c)
        : a(new int(a)), b(new int(b)), c(new int(c)) {}

Keep in mind The Rule of Three and implement copy constructor, copy assignment operator and destructor appropriately for foo.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    The destructor for this will likely be considerably more entertaining. – WhozCraig May 12 '15 at 04:51
  • @Praetorian, my understanding is that he wants to obviate the need for `_a`, `_b`, and `_c`. – R Sahu May 12 '15 at 04:53
  • @WhozCraig, yes, it will :) – R Sahu May 12 '15 at 04:53
  • @RSahu you're right. Solved my problem and helped me understand how the `new` operator works in C++. Thanks! – Joseph Nields May 12 '15 at 04:54
  • @JosephNields consider it an exercise off-site to figure out how to properly `delete` those. – WhozCraig May 12 '15 at 04:55
  • @RSahu I'll be damned, you managed to figure out the intent! I was thinking the OP was looking for something like [this](http://coliru.stacked-crooked.com/a/93a2e6dc93ff6a11) But the obvious solution in this case is for `a,b,c` to be `(const) int` instead of pointers. – Praetorian May 12 '15 at 04:56
  • @Praetorian, that might be the real solution after the OP tries to figure out how to delete the objects. – R Sahu May 12 '15 at 05:01
  • @WhozCraig what's wrong with just `~foo() {delete a, b, c;}` ? – Joseph Nields May 12 '15 at 05:02
  • @JosephNields, the compiler won't let you call `delete` on a pointer that points to a `const` object. – R Sahu May 12 '15 at 05:03
  • @RSahu why does that line compile then? – Joseph Nields May 12 '15 at 05:04
  • @JosephNields, I am surprised too. I thought that violated the `const`-ness of the object but maybe not. After all, destructors have to called even for `const` objects when they get out out scope. BTW, that should be `{delete a; delete b; delete c;}`. – R Sahu May 12 '15 at 05:06
  • 3
    Yes, that's exactly the reason `delete` on a `const` pointer works, see [this](https://stackoverflow.com/a/2271055/241631). @JosephNields `~foo() {delete a, b, c;}` is very wrong. You've only deleted `a`, and leaked `b` and `c`. Read up on the comma operator. – Praetorian May 12 '15 at 05:10
  • Thanks to all of you. All this time I never bothered trying to fire `delete` on a const pointer. (probably because I never had a need to, i guess. gotta learn something every day.=P) – WhozCraig May 12 '15 at 05:15
  • 2
    Exception safety goes out of the window, of course. If a later `new` throws, there's literally no way to release the memory previously allocated. – T.C. May 12 '15 at 05:16
  • That was an entertaining read. I could have bet that you cannot do `delete const T* const` – vsoftco May 12 '15 at 05:18