vector test(4)为什么会调用type的默认构造
2010-05-21 23:59
447 查看
/*作者:帅得不敢出门
*出处:C++爱好者灌水天堂 群号3503799
*/
问题出自群里,vector<type> test(4)为什么会调用type的默认构造,调用4次挎贝构造不就结了?
分析如下
调试过程 从1开始 看//注释后面标号
vector<type> test(4)为什么会调用type的默认构造
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
class A{
public:
A(const A& a){printf("A(A&)/n");}
A(){printf("A()/n");} //3. b--------- f10 跳到a
};
int main(void)
{
vector<A> test(4); //首先1. f5 f11 跑到a
return 0;
}
A() //问题在这里 为什么会调用到默认构造
A(A&)
A(A&)
A(A&)
A(A&)
过程分析
默认构造生成一个对象
然后4个A类元素都拷贝自它
explicit vector(size_type _Count)
: _Mybase()
{ // construct from _Count * _Ty()
/*a-----*/ _Construct_n(_Count, _Ty());//2. f10 跑到b 4.从3重新跑到这里再f11跑到c
}
相应值情况: 名称 值 类型
- _Alval {...} std::allocator<A>
std::_Allocator_base<A> {...} std::_Allocator_base<A>
_Count 4 unsigned int
this [0]() std::vector<A,std::allocator<A> > * const
--------------------------------------------------------
5.
/*c----------------*/
此时
参数情况:
_Val {...} const A & 这个是由默认构造产生的
void _Construct_n(size_type _Count, const _Ty& _Val)
{ // construct from _Count * _Val
if (_Buy(_Count))
{ // nonzero, fill it
_TRY_BEGIN
_Mylast = _Ufill(_Myfirst, _Count, _Val); // copy initializing _Count * _Val, using
allocator这里调用了_Count次拷贝构造函数
_CATCH_ALL
_Tidy();
_RERAISE;
_CATCH_END
}
}
环境:vs2005
*出处:C++爱好者灌水天堂 群号3503799
*/
问题出自群里,vector<type> test(4)为什么会调用type的默认构造,调用4次挎贝构造不就结了?
分析如下
调试过程 从1开始 看//注释后面标号
vector<type> test(4)为什么会调用type的默认构造
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
class A{
public:
A(const A& a){printf("A(A&)/n");}
A(){printf("A()/n");} //3. b--------- f10 跳到a
};
int main(void)
{
vector<A> test(4); //首先1. f5 f11 跑到a
return 0;
}
A() //问题在这里 为什么会调用到默认构造
A(A&)
A(A&)
A(A&)
A(A&)
过程分析
默认构造生成一个对象
然后4个A类元素都拷贝自它
explicit vector(size_type _Count)
: _Mybase()
{ // construct from _Count * _Ty()
/*a-----*/ _Construct_n(_Count, _Ty());//2. f10 跑到b 4.从3重新跑到这里再f11跑到c
}
相应值情况: 名称 值 类型
- _Alval {...} std::allocator<A>
std::_Allocator_base<A> {...} std::_Allocator_base<A>
_Count 4 unsigned int
this [0]() std::vector<A,std::allocator<A> > * const
--------------------------------------------------------
5.
/*c----------------*/
此时
参数情况:
_Val {...} const A & 这个是由默认构造产生的
void _Construct_n(size_type _Count, const _Ty& _Val)
{ // construct from _Count * _Val
if (_Buy(_Count))
{ // nonzero, fill it
_TRY_BEGIN
_Mylast = _Ufill(_Myfirst, _Count, _Val); // copy initializing _Count * _Val, using
allocator这里调用了_Count次拷贝构造函数
_CATCH_ALL
_Tidy();
_RERAISE;
_CATCH_END
}
}
环境:vs2005
相关文章推荐
- vector的push_back对于拷贝构造和赋值操作的调用
- 调用CachedRowSetImpl类时,为什么会出现这种错误 Access restriction: The type CachedRowSetImpl is not accessible due
- vector用erase删除元素时,为什么被删元素的析构函数会被调用更多次?
- 为什么子类的构造方法一定会调用父类的某个构造方法
- 讨论一下java中的构造方法中为什么不要尝试调用其本类方法!!!
- vector用erase删除元素时,为什么被删元素的析构函数会被调用更多次?
- 我们在创建对象的时候,为什么要调用构造方法并且要给构造方法传入实参???
- 验证对象在创建时就会先调用(默认)构造方法
- 声明一个父类A,它只有一个非默认构造方法;声明A的子类B,B具有默认构造方法及非默认方法,并在B的构造方法中调用基类A的构造方法。
- C++默认编写并调用了那些函数(构造,析构,赋值)!!!
- 调用Thread类的方法:public final String getName() 为什么得到的线程对象的名称默认是:Thread-0、Thread-1、Thread-2、...呢?
- [vs 2005]vector的默认构造流程
- C++ 类的三种构造函数(默认构造,拷贝构造,赋值构造),在什么情况下会被调用
- 为什么java的子类中必须调用父类的构造方法
- void prinT(Test *pT)//这个函数 这里为什么要用 我完全可以用t3.print()这样来调用类里面函数方法就可以,为何多此一举来这样一步???
- new 一个没有定义构造方法的子类对象,会调用父类的默认构造方法
- java继承时,实例化子类,是否会默认调用父类构造方法
- 关于默认构造的问题——vector的resize函数
- 对调用了TypeMock.net的代码进行调试遇到的怪问题--在vs.net中启动resharper自带的unit test的过程和方式是什么样的?
- Java继承中默认调用父类的无参构造方法的作用