委托构造
#include "stdafx.h"
#include <iostream>
class Base
{
public:
int value1;
int value2;
Base()
{
value1 = 1;
}
Base(int value) : Base()
{
value2 = 2;
}
}
int main()
{
Base b(2);
std::cout << b.value1 << std::endl;
std::cout << b.value2 << std::endl;
}
很简单,一句话总结:一个构造函数调用了另一个构造函数
继承构造
#include "stdafx.h"
#include <iostream>
class Base
{
public:
int value1;
int value2;
Base()
{
value1 = 1;
}
Base(int value) : Base()
{
value2 = 2;
}
}
class subBase : public Base()
{
public:
using Base::Base;
}
int main()
{
subBase b(2);
std::cout << b.value1 << std::endl;
std::cout << b.value2 << std::endl;
}
|