构造函数

  1. C++中const修饰符的作用
    <1> 常量
    <2> 常量引用指对常量的引用(reference to const)
    <3> 常量指针(const pointer)与指向常量的指针(pointer to const)
    <4> 常量成员函数

深入理解构造函数

构造函数的定义:A constructor is a special kind of class member function that is automatically called when an object of that class is instantiated.
构造函数的任务:Constructors are typically used to initialize member variables of the class to appropriate default or user-provided values, or to do any setup steps necessary for the class to be used (e.g. open a file or database).
Notes:
<1> 构造函数不可以是常量成员函数
<2> 构造函数仅仅用于初始化,且不可以被显示调用

一. 当类的数据成员皆为public属性时,构造方法有如下两种

直接就 “初始化列表” 或 “一致性初始化”

class Foo {
public:
    int m_x;
    int m_y;
};

int main() {
    Foo foo1 = { 4, 5 }; // initialization list
    Foo foo2 { 6, 7 }; // uniform initialization (C++11)
    return 0;
}

二. 当类的数据成员为private属性时,该如何构造呢?

1. 默认构造函数(无参 or 有默认值的参)

A constructor that takes no parameters (or has parameters that all have default values) is called a default constructor. The default constructor is called if no user-provided initialization values are provided.

#include <iostream>
class Fraction {
private:
    int m_numerator;
    int m_denominator;
public:
    Fraction() // default constructor
    {
         m_numerator = 0;
         m_denominator = 1;
    }
    int getNumerator() { return m_numerator; }
    int getDenominator() { return m_denominator; }
    double getValue() { return static_cast<double>(m_numerator) / m_denominator; }
};

int main() {
    Fraction frac; // Since no arguments, calls Fraction() default constructor
    std::cout << frac.getNumerator() << "/" << frac.getDenominator() << '\n';
    return 0;
}

2. Direct and uniform initialization using constructors with parameters(有参)

// Constructor with two parameters, one parameter having a default value
Fraction(int numerator, int denominator=1)
{   
    assert(denominator != 0);
    m_numerator = numerator;
    m_denominator = denominator;
}

//调用实例
//小括号的直接初始化,没有"="号
int x(5); // Direct initialize an integer
Fraction fiveThirds(5, 3); // Direct initialize a Fraction, calls Fraction(int, int) constructor
//花括号的一致性初始化,没有"="号
int x { 5 }; // Uniform initialization of an integer
Fraction fiveThirds {5, 3}; // Uniform initialization of a Fraction, calls Fraction(int, int) constructor

3. Copy initialization using equals with classes

//Much like with fundamental variables, it’s also possible to initialize classes using copy initialization
int x = 6; // Copy initialize an integer
Fraction six = Fraction(6); // Copy initialize a Fraction, will call Fraction(6, 1)
Fraction seven = 7; // Copy initialize a Fraction.  The compiler will try to find a way to convert 7 to a Fraction, which will invoke the Fraction(7, 1) constructor.

不建议这么做,效率低。对于基本类型,直接初始化、一致性初始化、拷贝初始化是一样的,但对于类,拷贝初始化是不同的。
建议:Do not copy initialize your classes

4. Classes without default constructors

If your class has no other constructors, C++ will automatically create an empty default constructor(空的默认构造函数)for you. Because the empty default constructor does not initialize any of the class’s member variables, if you use it to allocate an object of your class, the member variables of the class will not be initialized (similar to what happens when you declare an int, double, or other basic data type).

class Date {
private:
    int m_year;
    int m_month;
    int m_day;
// No default constructor provided, so C++ creates an empty one for us
// Because no other constructors exist, this provided constructor will be public
};

int main() {
    Date date; // calls default constructor that does nothing
    // date's member variables are uninitialized
    // Who knows what date we'll get?
    // They will hold garbage values.
    return 0;
}

建议:Provide at least one constructor for your class, even if it’s an empty default constructor.

三. 当一个类包含了另一个类,构造的顺序是?

class A {
public:
    A() { std::cout << "A\n"; }
};

class B {
private:
    A m_a; // B contains A as a member variable
public:
    B() { std::cout << "B\n"; }
};

int main() {
    B b;
    system("pause");
    return 0;
}

运行结果:
A
B

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值