构造函数 [英] constructor

查看:58
本文介绍了构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下


类序列{

public:

sequence(const sequence& mysequence,const int newjob ){

job_sequence(mysequence.job_sequence)

job_sequence.push_back(newjob);

...

}


vector< int> job_sequence;

}


我想要的是:如果我有一个序列对象,我希望能够创建一个

复制job_sequence并在其中添加另一个作业的新对象

job_sequence向量。


问题:我假设当我调用构造函数时-class,

首先是vector< int>的构造函数 job_sequence"将被称为

(这是错误的吗?我假设当我构造一个对象时,构造函数将会调用所有数据成员的
)。下一个:可以打电话给

" job_sequence(mysequence.job_sequence)"在序列的构造函数中?

这最后一个语句表示vector< int>的构造函数应该叫

吧?我可以用其他向量作为

参数调用构造函数,即使已经构造了向量对象(使用

默认构造函数,我假设它是一个空向量?


tx

i have the following

class sequence {
public:
sequence (const sequence& mysequence, const int newjob) {
job_sequence(mysequence.job_sequence)
job_sequence.push_back(newjob);
...
}

vector<int> job_sequence;
}

what i want is: if i have a sequence object, i want to be able to create a
new object that copies the job_sequence within it and adds another job to
the job_sequence vector.

question: i assume that when i call the constructor for sequence-class, that
first of all the constructor for "vector<int> job_sequence" will be called
( is this wrong? i assume that when i construct an object, the constructors
of all data-members will be called). next: is it ok to call
"job_sequence(mysequence.job_sequence)" in the constructor of sequence?
this last statement says that the constructor of vector<int> should be
called, right? can i call the constructor with the other vector as an
argument, even though the vector object was already constructed (with
default constructor, which i assume, is an empty vector?

tx

推荐答案

slurper写道:
slurper wrote:
我有以下

类序列{
public:
sequence(const sequence& mysequence,const int newjob){
job_sequence(mysequence.job_sequence) )
job_sequence.push_back(newjob);
...


向量< int> job_sequence;
}


;

我想要的是:如果我有一个序列对象,我希望能够创建一个
新对象,复制其中的job_sequence并添加另一个作业
job_sequence向量。

问题:我假设当我为序列类调用构造函数时,
首先是vector< int>的构造函数 job_sequence"将被调用(这是错误的吗?我假设当我构造一个对象时,将调用所有数据成员的
构造函数)。


这是正确的。

next:可以调用job_sequence(mysequence.job_sequence)吗?在序列的构造函数中?


No.

这最后一个语句说vector< int>的构造函数应该被叫好吧?


不,它说该类的运算符()被调用。它没有任何与建筑有关的事情。由于AFAIK向量没有定义

运算符(),这应该会产生错误。

我可以用另一个向量调用构造函数作为
参数,即使已经构造了矢量对象(使用
默认构造函数,我假设它是一个空向量?
i have the following

class sequence {
public:
sequence (const sequence& mysequence, const int newjob) {
job_sequence(mysequence.job_sequence)
job_sequence.push_back(newjob);
...
}

vector<int> job_sequence;
}
;
what i want is: if i have a sequence object, i want to be able to create a
new object that copies the job_sequence within it and adds another job to
the job_sequence vector.

question: i assume that when i call the constructor for sequence-class,
that first of all the constructor for "vector<int> job_sequence" will be
called ( is this wrong? i assume that when i construct an object, the
constructors of all data-members will be called).
That''s correct.
next: is it ok to call "job_sequence(mysequence.job_sequence)" in the
constructor of sequence?
No.
this last statement says that the constructor of vector<int> should be
called, right?
No, it says that the class''s operator() gets called. It doesn''t have
anything to do with construction. Since AFAIK vector doesn''t have
operator() defined, this should produce an error.
can i call the constructor with the other vector as an
argument, even though the vector object was already constructed (with
default constructor, which i assume, is an empty vector?




你不能自己调用​​构造函数。每当你创建一个对象时,它们会被自动调用

。你可以做的是 - 对于成员变量 -

指定通过初始化列表调用哪个构造函数。如果你这样做/>
没有指定任何,默认构造函数被调用。所以在上面的

示例中,job_sequence在你输入之前已经是默认构造的

你的序列构造函数。

因为你想要复制构造函数,你必须写:


sequence(const sequence& mysequence,const int newjob)

:job_sequence(mysequence.job_sequence)

{

job_sequence.push_back(newjob);

...

}


slurper写道:
slurper wrote:
我有以下

班级序列{
公开:
序列(const序列& mysequence,const int newjob){
job_sequence(mysequence.job_sequence)
job_sequence.push_back(newjob);
...


vector< INT> job_sequence;
}


所有这些:

当你构建一个对象时,会调用构造函数,因此名称为

构造函数。它的工作是将对象设置为初始状态。

构造函数被隐式调用,你不应该明确地调用它们。

他们不应该被误认为是改变对象状态的一种手段。

甚至不可能,我甚至不确定明确的ctor电话是否合法
$ b $标准C ++中的b。


顺便说一句,类名应始终以大写字母开头。在你打电话之后你就错过了一个分号了。

问题:我假设当我为序列类调用构造函数时,首先是
vector< int>的所有构造函数job_sequence"将被调用(这是错误的吗?我假设当我构造一个对象时,将调用所有数据成员的
构造函数)。


据我所知,构造函数调用的顺序是未定义的。或者至少

编译器依赖。但是当然会在

序列之前调用vector'的ctor。其他一切都没有意义。

next:可以调用
" job_sequence(mysequence.job_sequence)"在序列的构造函数?


不,使用init列表。

这最后一个语句说vector< int>的构造函数应该被调用,对吗?
i have the following

class sequence {
public:
sequence (const sequence& mysequence, const int newjob) {
job_sequence(mysequence.job_sequence)
job_sequence.push_back(newjob);
...
}

vector<int> job_sequence;
}
Firts of all:
Constructors are called when you -construct- an object, therefore the name
constructor. It''s job is to set the object to an initial state.
Constructors are called implicitly, you shouldn''t call them explicitly.
They shouldn''t be misued as a means of changing the state of your object.
And maybe even can''t, I''m not even sure if an explicit ctor call is legal
in standard C++.

Class names should always start with a capital letter by the way. And you''re
missing a semicolon after the ctor call.
question: i assume that when i call the constructor for sequence-class,
that first of all the constructor for "vector<int> job_sequence" will be
called ( is this wrong? i assume that when i construct an object, the
constructors of all data-members will be called).
As far as I know, the order of constructor calls is undefined. Or at least
compiler dependent. But of course vector''s ctor will be called before
sequence. Everything else wouldn''t make sense.
next: is it ok to call
"job_sequence(mysequence.job_sequence)" in the constructor of sequence?
No, use the init list.
this last statement says that the constructor of vector<int> should be
called, right?




不,你只是说序列有一个int向量。当然,在建设时间的某些时候,它的ctor将被调用。


问候,

马蒂亚斯



No, you''re just saying that sequence has a vector of int. Of course, at some
time of construction, its ctor will be called.

Regards,
Matthias


* Matthias:
* Matthias:

据我所知,构造函数调用的顺序是未定义的。或者至少取决于编译器。

As far as I know, the order of constructor calls is undefined. Or at least
compiler dependent.




不,它通常定义明确。


For成员变量它是声明的顺序。


我不太确定多重继承,但可能

即便如此(如果或者当它重要时我会查找它。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么并在电子邮件中?



No, it''s generally well-defined.

For member variables it''s the order of declaration.

I''m not quite sure with regard to multiple inheritance, but probably
even then (if or when it matters I''ll look it up).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆