美文网首页
构造函数

构造函数

作者: Dample_MAN | 来源:发表于2017-03-21 13:56 被阅读0次

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

  • 当一个类没有任何构造函数时,编译器会自动生成一个默认构造函数,此时可以classtype variable;,但类有其他构造函数时,编译器不会生成默认构造函数,因此如果没有自定义,以上实例化是错误的
  • 两个构造函数需要执行同一段代码时,可以考虑用overlapping constructor 和delegating constructor;
  • 当数据成员有const成员时,只能用成员列表赋值,在函数体采用copy initialization是错误的
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文件?????


静态成员函数

  • 静态成员函数只能调用静态数据,不能调用非静态数据
  • 静态成员函数没有this指针
  • 可以通过类名加::进行调用
  • 在类里面定义为内联函数,在类外定义则不是

静态成员数据

  • 静态成员数据在 类里声明,在类外定义初始化
  • 当static const int i = 1;为静态const数据时在内里面初始化(不过这种数据应该没什么用处吧)
  • 在没有实例化的情况下,也可以通过::进行访问

相关文章

  • Flutter 6种构造函数详解

    Flutter有生成构造函数、默认构造函数、命名构造函数、重定向构造函数、常量构造函数、工厂构造函数 一.生成构造...

  • C# 构造函数总结

    构造函数 构造函数分为:实例构造函数,静态构造函数,私有构造函数。 实例构造函数 1、构造函数的名字与类名相同。 ...

  • Javascript 基础之原型链

    构造函数、构造函数实例、构造函数原型 function Student (name) { } - 这是构造函数va...

  • Swift基础语法-类的构造函数

    本节知识点 构造函数的介绍 构造函数的基本使用 自定义构造函数 属性与构造函数 1. 构造函数的介绍 构造函数类似...

  • C++:面向对象基础

    构造函数 C++中有三种构造函数:默认构造函数,有参构造函数,拷贝构造函数 类对象的初始化 括号法//默认构造函数...

  • 构造函数

    构造函数分为: 1.实例构造函数 2.私有构造函数 3.静态构造函数 私有构造函数 私有构造函数是一种特殊的实例构...

  • Dart整理

    Dart构造函数 普通构造函数和命名构造函数 默认构造函数一个类没有声明构造函数,默认会有一个无参构造函数,声明了...

  • Kotlin面向对象 (3)✔️构造函数

    主构造函数次构造函数默认构造函数 kotlin中的构造函数有主次之分,主构造函数只能有一个,此构造函数可以有多个。...

  • [C++之旅] 10 构造函数

    [C++之旅] 10 构造函数 构造函数的特点 构造函数在对象实例化时被自动调用 构造函数与类同名 构造函数没有返...

  • Java基础-3 构造函数

    构造函数: 构造函数的作用: 给对应的对象进行初始化。 构造函数的格式: 构造函数要注意的细节:1. 构造函数是...

网友评论

      本文标题:构造函数

      本文链接:https://www.haomeiwen.com/subject/tfpwnttx.html