c ++中的defatult构造函数 [英] defatult constructor in c++

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

问题描述

你好世界。我有个问题。我写下面的代码,我在类行中有一个错误,说我类点中没有任何默认构造函数,但我不知道在这段代码中调用默认构造函数。请告诉我为什么我需要默认的构造函数。

谢谢。

 class point 
{
int x;
int y;
public:
point(int _x,int _y):x(_x),y(_y){}
point& operator =(point& a)
{
x = a.x;
y = a.y;
返回* this;
}
};
class line
{
int m;
先指出;
指向最后;
line(point& f,point& l)
{
first = f;
last = l;
}
};
int _tmain(int argc,_TCHAR * argv [])
{
return 0;
}

解决方案

界面中,您声明了成员第一个最后类型(侧面提示) :采用一些命名约定,让你更容易区分类型和变量,例如让所有用户定义的类型以大写字母开头。通常,编译器将为您的类定义默认构造函数。但是,当您自己提供非默认值时(就像您在 point 类中所做的那样),编译器将从界面中省略其默认实现!这就是你没有默认构造函数的原因,但是为什么编译器要求你定义一个?



让我们来看看你的 line 构造函数如下:



 line(point& f,point& l) 
{
first = f;
last = l;
}





你应该知道在构造函数体的之前运行,类 - 成员已经已初始化。这意味着编译器将查找成员的默认构造函数。 C ++提供了一个名为成员初始化器的工具,它们在构造函数的主体之前处理。程序员未使用成员初始化程序(或类似的C ++ 11特性)显式初始化的所有成员将由编译器进行默认初始化。成员初始值设定项的语法如下:



 line(point& f,point& l)
:第一个(f),
最后(l)
{}





甚至可能更快,因为他们只会在代码中初始化一次而不是两次。



结论:如果您的成员不提供默认构造函数,则必须为成员初始值设定项提供适当的参数。



编辑:忘记包含一个非常有用的,仍然维护的在线资源(由我以前的C ++老师): http://www.icce.rug.nl/documents/cplusplus/ [ ^ ]。第7.3段处理这个问题: - )


最好解释为什么会出现编译错误:你定义了一个构造函数 point(int,int),因此在声明变量 first last 时必须使用它。当您在没有显式初始化的情况下声明它们时,编译器会假定您使用的是无参数构造函数。仅当您未定义其他构造函数时,才会将语法定义为采用默认构造函数。如果你没有用两个整数参数添加构造函数,编译器将使用默认构造函数,无参数构造函数,什么都不做;或者你可以明确定义这样的构造函数并初始化 x y



实际上,在这种情况下使用默认构造函数是一个非常糟糕的主意,因为你不希望保留未初始化的 x y ,所以你最好用两个参数初始化第一个最后一个,或者添加一个显式的无参数构造函数(然后你显然会有两个初始化类实例的选项)。



你可以阅读与构造函数相关的C ++语法和相应的自己的语义:

http://en.wikipedia.org/wiki/Default_constructor [< a href =http://en.wikipedia.org/wiki/Default_constructortarget =_ blanktitle =New Window> ^ ],

http://www.cplusplus.com/doc/tutorial/classes [ ^ ](在此页面上搜索默认构造函数)。



-SA


默认构造函数用于创建类的对象,..

构造函数用于初始化类的私有数据成员。

一个类可以有多个构造函数但它只有一个析构函数。



假设你有一个班级:



班级考试{
私人
int x;
int y;
public
Test() // 默认构造函数
{}
测试( int a, int b){ // 带参数的构造函数
x = a;
y = b;
}
// 。 。 。遍历数据的函数..

}
int main(){
// 现在你可以创建类的对象
测试testObject; // 创建一个全新的对象,没有默认构造函数,你不能
// 创建一个这样的对象。
// 注意:数据成员仍未初始化。
测试T( 2 5 ); // 带参数的构造函数,设置值x = 2和y = 5
return 0 ;
}


hello world. i have a question. i write the below code and i have an error in class line that say me "there isn't any default constructor in class point" but i don't know where in this code call the default constructor . please tell me why i need the default constructor.
thanks .

class point
{
	int x;
	int y;
public:
	point(int _x,int _y):x(_x),y(_y){}
	point& operator=(point&a)
	{
		x=a.x;
		y=a.y;
		return *this;
	}
};
class line
{
	int m;
	point first;
	point last;
	line(point &f,point &l)
	{
		first=f;
		last=l;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

解决方案

In your line interface, you declared the members first and last of type point (side-tip: adopt some naming convention which let's you distinguish more easily between types and variables, e.g. let all user defined types start with a capital). Normally, the compiler will define a default-constructor for your class. However, when you provide a non-default one yourself (like you did in the point class), the compiler will omit its default implementation from the interface! This is why you don't have a default constructor, but why does the compiler require you to define one?

Let's take a look at what your line constructor looks like:

line(point &f,point &l)
{
    first=f;
    last=l;
}



You should know that before the body of the constructor runs, the class-members have already been initialized. This means that the compiler will look for the default constructor of the line members. C++ offers a facility called member initializers, which are handled before the body of your constructor. All members that are not explicitly initialized by the programmer using member initializers (or similar C++11 features) will be default-initialized by the compiler. The syntax for member initializers is like so:

line(point &f,point &l)
:  first(f),
   last(l)
{}



It might even be faster, as they will only be initialized once instead of twice in your code.

Conclusion: if your members don't offer default constructors, you have to provide member initializers with the appropriate arguments.

EDIT: Forgot to include a very useful, still maintained, online resource (by my former C++ teacher): http://www.icce.rug.nl/documents/cplusplus/[^]. Paragraph 7.3 handles this issue :-)


It's better to explain why do you have a compilation error: you defined a constructor point(int, int), so you have to use it when declaring the variables first and last. As you declaring them without explicit initialization, the compiler assumes that you are using the parameterless constructor. The syntax is defined to assume the default constructor only if you don't define other constructors. If you did not add your constructor with two integer parameter, the compiler would use the default constructor, parameterless one, doing nothing; or you could explicitly define such constructor and initialize x and y.

Practically, using default constructor in this case would be a pretty bad idea, because you don't want leave uninitialized x and y, so you should better initialize first and last with two parameters or add an explicit parameterless constructor (and, then you would apparently have two options of initialization of the instances of the class).

And you could have read on C++ syntax related to constructors and corresponding semantics yourself:
http://en.wikipedia.org/wiki/Default_constructor[^],
http://www.cplusplus.com/doc/tutorial/classes[^] (search for "default constructor" on this page).

—SA


The Default Constructor is used to create an Object of Class, ..
Constructors are used to initialize the private data members of a Class.
A Class can have more then one Constructor BUT it only have one Destructor.

Suppose you have a class:

Class Test{
 private:
  int x;
  int y;
 public:
  Test() // Default constructor
  { }
  Test(int a, int b){ // constructor with parameters
  x = a;
  y = b;
 }
 // . . . functions to traverse the data .. 

}
int main(){
 // Ok now you create object of the class
 Test testObject; // A brand new object  is created, without default constructor you cannot
// create an object like this.
// NOTE: the data members are still un-initialized.
 Test T(2,5); // constructor with parameters, sets the value x = 2 and y = 5
 return 0;
}


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

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