1
是否可以使用构造函数的初始化列表初始化矢量成员。下面给出一些不正确的代码。使用构造函数的参数初始化矢量成员
#ifndef _CLASSA_H_
#define _CLASSA_H_
#include <iostream>
#include <vector>
#include <string>
class CA{
public:
CA();
~CA();
private:
std::vector<int> mCount;
std::vector<string> mTitle;
};
构造函数的.cpp文件
// I want to do it this way
#pragma once
#include "classa.h"
// Constructor
CA::CA(int pCount, std::string pTitle) :mCount(pCount), mTitle(pTitle)
{
}
// Destructor
CA::~CA()
{
}
在主文件
#include "classa.h"
int main()
{
CA A1(25, "abcd");
return 0;
}
谢谢@songyuanyao – user18441