Initialize const member variables

dooglius

I have C++ code that boils down to something like the following:

class Foo{
    bool bar;
    bool baz;
    Foo(const void*);
};
Foo::Foo(const void* ptr){
    const struct my_struct* s = complex_method(ptr);
    bar = calculate_bar(s);
    baz = calculate_baz(s);
}

Semantically, the bar and baz member variables should be const, since they should not change after initialization. However, it seems that in order to make them so, I would need to initialize them in an initialization list rather than assign them. To be clear, I understand why I need to do this. The problem is, I can't seem to find any way to convert the code into an initialization list without doing one of the following undesirable things:

  • Call complex_method twice (would be bad for performance)
  • Add the pointer to the Foo class (would make the class size needlessly large)

Is there any way to make the variables const while avoiding these undesirable situations?

Andy Prowl

If you can afford a C++11 compiler, consider delegating constructors:

class Foo
{
    // ...
    bool const bar;
    bool const baz;
    Foo(void const*);
    // ...
    Foo(my_struct const* s); // Possibly private
};

Foo::Foo(void const* ptr)
    : Foo{complex_method(ptr)}
{
}

// ...

Foo::Foo(my_struct const* s)
    : bar{calculate_bar(s)}
    , baz{calculate_baz(s)}
{
}

As a general advice, be careful declaring your data members as const, because this makes your class impossible to copy-assign and move-assign. If your class is supposed to be used with value semantics, those operations become desirable. If that's not the case, you can disregard this note.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Initialize member variables in the beginning of class definition or in constructor?

From

How to initialize const member variable in a class?

From

How can I initialize C++ object member variables in the constructor?

From Dev

Proper way to initialize a const member of dynamically allocated struct

From Dev

Where to initialize member variables of Class in C++

From Dev

How to initialize const member requiring computations to be performed?

From Dev

Cannot initialize a member subobject of type 'const signed char *' with an lvalue of type 'const char [X]'

From Dev

Do constructors have to initialize member variables in C++?

From Dev

Is there a way to initialize a `const` `struct` using `const` variables?

From Dev

How to initialize multiple constant member variables that shares complex initialization code?

From Dev

How to initialize a const member variable of a class with a const variable?

From Dev

C++ proper way to inline initialize member variables

From Dev

In what sense const allows only atomic changes to mutable member variables?

From Dev

Type of member variables in a const member function

From Dev

How to initialize member variables of a custom javafx controller?

From Dev

Const Member Variables in C++11

From Dev

Is it safe to initialize global const variables, defines?

From Dev

Initialize a static const member with a template argument

From Dev

Initialize static const member of a class, where the member is of a private type?

From Dev

Where do I have to initialize class member variables JavaFX

From Dev

Initialize const member and use parent constructor

From Dev

C++ 11 standards - initialize member variables in header

From Dev

Why can I update member variables in a const member function?

From Dev

Initialize a vector of classes that have member pointers to another class' member variables

From Dev

How to initialize member variables before inherited classes

From Dev

How to initialize a const reference member to another member (std vector) in C++ initializer list

From Dev

Why default capture is not consistently const for both local variables and member variables?

From Dev

Is it possible to initialize a data member as const based on a bool passed in as argument in the constructor?

From Dev

How to initialize non-const member variable with const value

Related Related

  1. 1

    Initialize member variables in the beginning of class definition or in constructor?

  2. 2

    How to initialize const member variable in a class?

  3. 3

    How can I initialize C++ object member variables in the constructor?

  4. 4

    Proper way to initialize a const member of dynamically allocated struct

  5. 5

    Where to initialize member variables of Class in C++

  6. 6

    How to initialize const member requiring computations to be performed?

  7. 7

    Cannot initialize a member subobject of type 'const signed char *' with an lvalue of type 'const char [X]'

  8. 8

    Do constructors have to initialize member variables in C++?

  9. 9

    Is there a way to initialize a `const` `struct` using `const` variables?

  10. 10

    How to initialize multiple constant member variables that shares complex initialization code?

  11. 11

    How to initialize a const member variable of a class with a const variable?

  12. 12

    C++ proper way to inline initialize member variables

  13. 13

    In what sense const allows only atomic changes to mutable member variables?

  14. 14

    Type of member variables in a const member function

  15. 15

    How to initialize member variables of a custom javafx controller?

  16. 16

    Const Member Variables in C++11

  17. 17

    Is it safe to initialize global const variables, defines?

  18. 18

    Initialize a static const member with a template argument

  19. 19

    Initialize static const member of a class, where the member is of a private type?

  20. 20

    Where do I have to initialize class member variables JavaFX

  21. 21

    Initialize const member and use parent constructor

  22. 22

    C++ 11 standards - initialize member variables in header

  23. 23

    Why can I update member variables in a const member function?

  24. 24

    Initialize a vector of classes that have member pointers to another class' member variables

  25. 25

    How to initialize member variables before inherited classes

  26. 26

    How to initialize a const reference member to another member (std vector) in C++ initializer list

  27. 27

    Why default capture is not consistently const for both local variables and member variables?

  28. 28

    Is it possible to initialize a data member as const based on a bool passed in as argument in the constructor?

  29. 29

    How to initialize non-const member variable with const value

HotTag

Archive