C++ Cookbook by Turkanis Jonathan Cogswell Jeff Diggins Christopher Stephens D. Ryan

C++ Cookbook by Turkanis Jonathan Cogswell Jeff Diggins Christopher Stephens D. Ryan

Author:Turkanis, Jonathan, Cogswell, Jeff, Diggins, Christopher, Stephens, D. Ryan [D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, and Jeff Cogswell]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C++
ISBN: 9781449366568
Publisher: O'Reilly Media
Published: 2009-06-29T16:00:00+00:00


8.1. Initializing Class Member Variables

Problem

You need to initialize member variables that are native types, pointers, or references.

Solution

Use an initializer list to set the initial values for member variables. Example 8-1 shows how you can do this for native types, pointers, and references.

Example 8-1. Initializing class members

#include <string> using namespace std; class Foo { public: Foo() : counter_(0), str_(NULL) {} Foo(int c, string* p) : counter_(c), str_(p) {} private: int counter_; string* str_; }; int main() { string s = "bar"; Foo(2, &s); }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.