0

I understand that a member initialzation list can be used to initialize objects and variables that have a known size.

However, how does a member initialization list actually work with pointers, in particular const char* pointers, as in MyClass below? Will it simply point to the memory location of the const char* which I pass in?

class MyClass
{
  public:
    MyClass(const char* str): _str(str)
    {
    }

  private:
    const char* _str;
};
Evg
  • 25,259
  • 5
  • 41
  • 83
Engineer999
  • 3,683
  • 6
  • 33
  • 71

2 Answers2

1

Yes, it will simply initialize _str with the value of str - ie, make _str point to the same memory address that str is pointing at.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I understand that a member initialzation list can be used to initialize objects and variables that have a known size.

Member initialization lists can be used to initialize members and base sub objects. Those always have a known size.

However, how does a member initialization list actually work with pointers

All pointers have a known size.

Will it simply point to the memory location of the const char* which I pass in?

Yes. That's what a pointer value is.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
eerorika
  • 232,697
  • 12
  • 197
  • 326