Class static variable Initialization

Source: Internet
Author: User

The definition of static member variables consists of two parts.

Like a function, you must first declare it in the class, and then find another place to implement it. Static members must first declare it in the class, and then "IMPLEMENT" outside the class.

This initialization method is determined by the public nature of static members. It cannot be put in the constructor and initialized by every object. It must be implemented outside the class.

// Trace. h

# Pragma once
# Include <string>
Using std: string;
Class CTrace
{
Public:
CTrace (void );
CTrace (const string & name );
~ CTrace (void );
Void debug (const string & msg );
Static bool traceIsActive;

Private:
String theFunctionName;
};

 

// Trace. cpp

# Include <iostream>
# Include "Trace. h"

Bool CTrace: traceIsActive = false; // This sentence must be added; otherwise, the link is incorrect.

CTrace: CTrace (void)
{
}

Inline ctrace: ctrace (const string & name): thefunctionname (name)
{
If (traceisactive)
{
STD: cout <"Enter function" <name <STD: Endl;
}
}
Ctrace ::~ Ctrace (void)
{
If (ctrace: traceisactive)
{
STD: cout <"exit function" <thefunctionname <STD: Endl;
}
}

Inline void ctrace: Debug (const string & MSG)
{
If (ctrace: traceisactive)
{
STD: cout <MSG <STD: Endl;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.