c++ self 在初始化列表中
我有这个代码片段
class Osoba{
Osoba(char* imie,int wiek){
this->imie=new char[strlen(imie)+1];
strcpy(this->imie,imie);
this->wiek=wiek;
cout<<"Utworzono Osobe "<<this->imie<<endl;
}
Osoba(Osoba& x){
Osoba(x.imie,x.wiek);
}
[...]
,当我调用复制构造函数时,它不起作用(创建和销毁对象)。
编辑: 如果我使用
Osoba(Osoba& x): Osoba(x.imie,x.wiek){
我得到类型'class Osoba'不是'Osoba'的直接基础
这是如何完成的?
i have this code snippet
class Osoba{
Osoba(char* imie,int wiek){
this->imie=new char[strlen(imie)+1];
strcpy(this->imie,imie);
this->wiek=wiek;
cout<<"Utworzono Osobe "<<this->imie<<endl;
}
Osoba(Osoba& x){
Osoba(x.imie,x.wiek);
}
[...]
and when i call the copy constructor it doesnt work (creates and destroyes object).
Edit:
if i use
Osoba(Osoba& x): Osoba(x.imie,x.wiek){
i get type 'class Osoba' is not a direct base of 'Osoba'
how is this done ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能这样调用构造函数。好吧,你可以,但是会发生一个无名的临时对象被创建。编写复制构造函数而不引用其他构造函数。另外,如果您使用 std::string 而不是 char *,则不需要复制构造函数。如果你坚持使用 char *,你还需要一个析构函数和一个赋值运算符。
You can't call constructors like that. Well you can, but what happens is a nameless temporary object gets created. Write your copy constructor without reference to the other constructor. Also, if you use std::string instead of char *, you won't need a copy constructor. If you persist in using char *, you will also need a destructor and an assignment operator.
您必须初始化
Osoba
的成员,就像在其他构造函数中所做的那样。您只能在 C++2011 中重用构造函数(使用其他语法)。
You have to initialize the members of
Osoba
, the same way as you are doing in the other constructor.You can only reuse constructors (with other syntax) in C++2011.
除了创建另一个不同的对象之外,您不能调用构造函数。
如果您需要在构造函数之间有一些共同的代码,您可以将其放在单独的方法中并调用该方法。请注意,在构造函数中,您可以调用对象的方法,但虚拟方法不会分派派生类。
换句话说,如果
在
Bar
的构造函数期间调用的doit
的实现将是Foo
中定义的实现,因为在派生对象的基础部分,该对象只是一个“基础”对象。仅在构造函数的末尾,它在执行“派生”构造函数中的任何最终存在的代码之前才成为“派生”对象。请注意其他面向对象语言使用不同的方法...
有关 C++ 中到底发生的情况的解释 请参阅这篇文章。
如果您喜欢法律术语描述,则 C++ 标准 12.7.4 中是这样规定的:
You cannot call a constructor except for creating another different object.
If you need to have some code in common between constructors you can place it in a separate method and call that method. Note that in the constructor you can call methods of the object but
virtual
methods are not going to dispatch do derived classes.In other words if you have
during the constructor of
Bar
the implementation ofdoit
called will be the one defined inFoo
because during the constructor of the base part of the derived object, the object is only a "base" object. Only at the end of the constructor it becomes a "derived" object right before executing any eventually present code in the "derived" constructor.Be careful that other object oriented languages use a different approach...
For an explanation of what happens exactly in C++ see this article.
If you like instead a legalese description this is what is stated in the C++ standard at 12.7.4:
首先,构造函数不仅仅是任何方法,你不能只使用 at 作为修改方法。
也就是说,
我认为你们的班级有两个领域,imie 和 wiek,对吗?
只需再次执行初始化代码,这次使用 x.imie 和 x.wiek 作为输入
First of all, the constructor is not just any method, you can't just use at as a mutator method.
that said,
Am I right in thinking that your class had two fields, imie and wiek?
just do the init code again, this time with x.imie and x.wiek as input