构造函数

2017-03-21  本文已影响0人  Dample_MAN

关于一个类的构造函数要注意以下几点:

double value2(2.2); // direct initialization
char value3 {'c'} // uniform initialization

关于assert()函数:

#include <stdio.h>      /* printf */
#include <assert.h>     /* assert */

void print_number(int* myInt) {
    assert(("this is not to be null",myInt != NULL));
//或者assert(myInt != NULl && "this is not to be null"),这两个语句将输出字面语
//或者不显示语句assert(myInt != NULL)
    printf("%d\n", *myInt);
}

int main()
{
    int a = 10;
    int * b = NULL;
    int * c = NULL;

    b = &a;

    print_number(b);
    print_number(c);

    return 0;
}

析构函数

析构函数可以调用其他成员函数,没什么好说的;;;


scope , duration, linkage.

关于linkage,一个文件的非const全局变量默认为extern的
global.cpp

int g_x; // non-const globals have external linkage by default
int g_y(2); // non-const globals have external linkage by default
// in this file, g_x and g_y can be used anywhere beyond this point```
main.cpp

extern int g_x; // forward declaration for g_x -- g_x can be used beyond this point in this file

int main()
{
extern int g_y; // forward declaration for g_y -- g_y can be used beyond this point in main()

g_x = 5;
std::cout << g_y; // should print 2

return 0;

}```
一个文件const全局变量默认static的constants.cpp:

namespace Constants
{
    // actual global variables
    extern const double pi(3.14159);
    extern const double avogadro(6.0221413e23);
    extern const double my_gravity(9.2); // m/s^2 -- gravity is light on this planet
}

constants.h:

#ifndef CONSTANTS_H
#define CONSTANTS_H
 
namespace Constants
{
    // forward declarations only
    extern const double pi;
    extern const double avogadro;
    extern const double my_gravity;
}
 
#endif

Use in the code file stays the same:

#include "constants.h"
double circumference = 2 * radius * Constants::pi;

这个实现cpp不需要包含.h文件?????


静态成员函数

静态成员数据


上一篇下一篇

猜你喜欢

热点阅读