0

I am a beginner in C++, and got confused about the initialization of const pointers declared in headers. To give but one example, I have in my header a structure and a class like:

/* header file.h */
typedef struct A{ ... } A;

class myClass{
    protected:
        A *const myPtrA;
}

And would like to instantiate the content of myPtrA, for instance in a constructor, knowing that A is a quite complicated structure composed of substructures, and needs to be dynamically instantiated:

/* source file.cpp */
#include file.h

myClass::myClass() {
   A *tmpA = new A;
   *myPtrA = *tmpA;
}

Is this the good way to initialize my const pointer myPtrA? And to the extent each new call needs a dedicated delete call, can I delete my pointer tmpA immediately after the line *myPtrA = *a; without risk of losing the content pointed by myPtrA?

Thank you in advance for your time (and pardon my English ;) )

qroh

  • 2
    [Find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read about *constructor initializer lists*. – Some programmer dude May 30 '17 at 09:09
  • By the way, in C++ a `struct` is just like a class, the only difference being the default visibility (`public` for `struct`, `private` for `class`). That means you don't need `typedef` for a `struct`. – Some programmer dude May 30 '17 at 09:10
  • 1
    ``typedef struct A { ... } A;`` is the C way of defining a struct. In C++ you define it like you do for classes: ``struct A{...};``. The only difference between struct and classes in C++ is the default visibility. – nefas May 30 '17 at 09:11
  • _knowing that A is a quite complicated structure composed of substructures, and needs to be dynamically instantiated_ Umm.. Why can't you initialize it within _initializer list_ via the call of the constructor? Why do you think, that you need dynamic allocation here. Note: The example, that you show, exhibits undefined behavior, because `myPtrA` is not initialized. And no, `*myPtrA = *tmpA;` is **not** an initialization of it. That line is trying to _dereference_ pointer, that was never initialized. – Algirdas Preidžius May 30 '17 at 09:11
  • Why do you even have a pointer there in the first place? – Bartek Banachewicz May 30 '17 at 09:11
  • Well ok for struct typedef :) it's noted. –  May 30 '17 at 09:27
  • Well, I simplified the problem because my structure A is defined in another header associated with a C file (that's why I let this semantic with typedef, sorry for that). To be more precise, I need to create a cpp wrapper over a c file. My cpp constructor must begin by calling the wrapped c constructor, and the latter takes as an argument a const pointer to structure A. –  May 30 '17 at 09:39
  • @Someprogrammerdude: No, a `struct` **is** a class. – Lightness Races in Orbit May 30 '17 at 09:56
  • @nefas: Please don't spread [misconceptions](https://stackoverflow.com/a/34108140/560648). – Lightness Races in Orbit May 30 '17 at 09:56

1 Answers1

2
*myPtrA = *tmpA;

Is this the good way to initialize my const pointer myPtrA?

No. You haven't initialized myPtrA at all. It was default-initialized and therefore has indeterminate value. Dereferencing the pointer with indeterminate value (*myPtrA) has undefined behaviour.

can I delete my pointer tmpA immediately after the line *myPtrA = *a; without risk of losing the content pointed by myPtrA?

Yes, it is safe. The object pointed by myPtrA is copy (by assignment) of the one pointed by tmpA. However, it is completely pointless to allocate a dynamic object, copy it and then detroy it in the first place, when you could simply create/modify the copy directly.

Here is an example of how to correctly initialize the member pointer:

class myClass{
    protected:
        A *const myPtrA = new A;
};

PS. While it is useful to learn how to do this, you should hardly ever manage memory manually in actual programs. Instead use RAII containers such as std::unique_ptr.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks a lot, it very clear! "it is completely pointless to allocate a dynamic object, copy it and then detroy it in the first place" I agree, but I wanted to make sure it's pointless ;) Thanks again –  May 30 '17 at 10:04