C + + is not quite the same as in some places 2

Source: Internet
Author: User

1.The general global variables are placed inside the CPP file instead of . h inside. Otherwise easy to mess up, this chaos several times good, first remember such a coding specification.

Think that we are introduced to more trouble, but the implementation of only CPP inside. After all , the . h is the file to be included, which may cause problems when used.

Use the extern keyword when accessing.

Speaking of extern , just extend it a little bit. And in C,C+ + mixed programming, the C code in the extern , for example:

Extern{

/*c Language

The Code

*/

"Remember what I said beforeCthe language does not support overloading, butC + +is to support overloading. That place can see some clues, in order to implement the overload, in order to letC + +the compiler supportsClanguage,C + +still do a lot of efforts. For example, at the time of compiling,C + +The function adds a head and a tail. Just give me an example, for example, the original function is:void MyFunction (void) {}.thisC + +the function will become after compilingSdmyfunctionvprobably grown like this. So, at compile time, there will always be some different places, it is necessary, we put the current file or a part of the file according toCcompiled in the same way. "

So extern has two scenarios. 1,introduce the external variable,2, let the code snippet compile in C Way.

2. Initialization mode:

#include <iostream>using namespacestd;classmyclass{ Public: MyClass () {cout<<"no parameter"<<Endl; } MyClass (inta) {cout<<"int:"<<a<<Endl; }};intMain () {MyClass*myclass =NewMyClass ();//non-parametric constructionMyClass *myclass1 =NewMyClass (9);//with a reference structureMyClass myclasses[3] = {MyClass (1), MyClass (2), MyClass (3)};//an instantiation of an array of objects. cout <<"Hello world!"<<Endl; return 0;}

Single Case design mode:

The initialization of static variables is mainly:

Mysingle.h

#ifndef Mysingle_h#defineMysingle_hclassmysingle{Private: Mysingle (); StaticMysingle *mysingleptr; Public:    StaticMysingle *getmysingle (void); Static voidFreemysingle (void); voidFreecurrentsingle (void);};#endif //Mysingle_h

Main.cpp

#include <iostream>#include<mysingle.h>using namespacestd;intMain () {Mysingle*mysingle =Mysingle::getmysingle (); Mysingle*mysingle1 =Mysingle::getmysingle (); cout<<"Mysingle Address:"<<mySingle<<Endl; cout<<"MySingle1 Address:"<<mySingle1<<Endl;    Mysingle::freemysingle (); Mysingle-Freecurrentsingle (); MySingle1-Freecurrentsingle (); cout<<"Hello world!"<<Endl; return 0;}

Mysingle.cpp

#include"mysingle.h"#include<iostream>Mysingle*mysingle::mysingleptr=NULL;//blinded, this has to look like this: Mysingle * is the type, mysingle::mysingleptr=null;//The value of this property is Mysingle inside the mysingleptr and then empty. Mysingle::mysingle () {}mysingle*Mysingle::getmysingle () {if(mysingleptr==NULL) {Mysingleptr=NewMysingle (); }    returnmysingleptr;}/** * @brief Mysingle::freemysingle * released by class*/voidMysingle::freemysingle () {if(Mysingleptr!=null) {//if it is not empty,        DeleteMysingleptr;//releasing the corresponding memoryMysingleptr = NULL;//then point to empty    }}/** * @brief Mysingle::freecurrentsingle * Released by the object. */voidMysingle::freecurrentsingle () {if(mysingleptr!=NULL) {        Deletemysingleptr; Mysingleptr=NULL; }}

Some shortcut keys:

Http://www.tuicool.com/articles/IjuaQz

C + + differs from C in some places 2

Related Article

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.