How to initialize a class member reference to the empty string in the default constructor

Hunsu

How can I initialize a class member reference to the empty string ("") in this default constructor.

class Document {
    string& title;
    string* summary;

    Document() : title(/*what to put here*/), summary(NULL) {}
};
Christian Hackl

There is no sensible way to do this. A reference must refer to an existing object (an ever better way to put it would be that a reference is the existing object), and the std::string that would be constructed in the initialiser list from "" would be destroyed after the constructor has finished, leaving you with undefined behaviour on every attempt to use your member variable.

Now, I said "no sensible way". There are hacks to achieve what you want, of course, e.g.:

// bad hack:
std::string &EmptyStringThatLivesForever()
{
    static std::string string;
    return string;
}

class Document {
    string& title;
    string* summary;

    Document() : title(EmptyStringThatLivesForever()), summary(NULL) {}
};

But I doubt that such a trick would survive any serious code review.

The real solution is to get rid of the reference:

class Document {
    string title;
    string* summary;

    Document() : title(""), summary(NULL) {}
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Default constructor for a class with a reference data member?

From Dev

How to initialize a reference to an empty STL container in a constructor?

From Dev

Class Composition Constructor d Must Explicitly initialize the reference member

From Dev

Error: constructor must explicitly initialize reference member

From Dev

Error: constructor must explicitly initialize reference member

From Dev

cannot find default constructor to initialize member in cpp

From Dev

Class Member Not a Reference but Constructor with Argument Passed by Reference

From Dev

Empty constructor in String class

From Dev

Empty constructor in String class

From Dev

Initialize tuple member in constructor of variadic template class

From Dev

Passing class member to base class constructor (by reference)

From Dev

Initialize a const class member with a default value

From Dev

Initialize parent class with empty constructor in initializer list?

From Dev

Initialize parent class with empty constructor in initializer list?

From Dev

Instantiating an object as class member without default constructor

From Dev

Initialize an array member of class when class's copy constructor is deleted

From Dev

Does default constructor zero-initialize member array variable?

From Dev

Explicitly initialize member which does not have a default constructor

From Dev

How to create a smartpointer to a class and initialize the class constructor

From Dev

How do I initialize an array in a default constructor?

From Dev

How does default constructor used to instantiate object if it's super class no argument constructor has empty body?

From Dev

How to initialize static member in child class?

From Dev

Default constructor is getting called on a const reference member despite non default constructor arguments

From Dev

How to initialize a new field of an inner class with constructor?

From Dev

How to initialize dictionary inside class constructor

From Dev

How to initialize a new field of an inner class with constructor?

From Dev

Defining class member via a reference passed through constructor

From Dev

C++ Undefined reference when using class member in constructor

From Dev

How to initialize template member array from constructor parameter?

Related Related

  1. 1

    Default constructor for a class with a reference data member?

  2. 2

    How to initialize a reference to an empty STL container in a constructor?

  3. 3

    Class Composition Constructor d Must Explicitly initialize the reference member

  4. 4

    Error: constructor must explicitly initialize reference member

  5. 5

    Error: constructor must explicitly initialize reference member

  6. 6

    cannot find default constructor to initialize member in cpp

  7. 7

    Class Member Not a Reference but Constructor with Argument Passed by Reference

  8. 8

    Empty constructor in String class

  9. 9

    Empty constructor in String class

  10. 10

    Initialize tuple member in constructor of variadic template class

  11. 11

    Passing class member to base class constructor (by reference)

  12. 12

    Initialize a const class member with a default value

  13. 13

    Initialize parent class with empty constructor in initializer list?

  14. 14

    Initialize parent class with empty constructor in initializer list?

  15. 15

    Instantiating an object as class member without default constructor

  16. 16

    Initialize an array member of class when class's copy constructor is deleted

  17. 17

    Does default constructor zero-initialize member array variable?

  18. 18

    Explicitly initialize member which does not have a default constructor

  19. 19

    How to create a smartpointer to a class and initialize the class constructor

  20. 20

    How do I initialize an array in a default constructor?

  21. 21

    How does default constructor used to instantiate object if it's super class no argument constructor has empty body?

  22. 22

    How to initialize static member in child class?

  23. 23

    Default constructor is getting called on a const reference member despite non default constructor arguments

  24. 24

    How to initialize a new field of an inner class with constructor?

  25. 25

    How to initialize dictionary inside class constructor

  26. 26

    How to initialize a new field of an inner class with constructor?

  27. 27

    Defining class member via a reference passed through constructor

  28. 28

    C++ Undefined reference when using class member in constructor

  29. 29

    How to initialize template member array from constructor parameter?

HotTag

Archive