使用C ++构造函数 [英] Use of C++ Constructor

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

问题描述

我想看一个C ++程序,它显示了默认,perameterised和copy constuctor的使用。

解决方案

尽管这个问题很简单,但它是一个基础在一个地方从各个角度都没有很好地描述的原则。



我看过的一些页面对一个或多个问题有合理的描述: />
- http://www.cprogramming.com/tutorial/constructor_destructor_ordering.html [< a href =http://www.cprogramming.com/tutorial/constructor_destructor_ordering.html\"target =_ blanktitle =New Window> ^ ]

- http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15 [ ^ ]

- http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm [ ^ ]

- http://www.cprogramming.com/tutorial/initialization-lists-c++.html [ ^ ]

- http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 [<一个href =http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6target =_ blanktitle =新窗口> ^ ]



我找不到一个很好的链接来描述为什么类成员的顺序对初始化列表很重要。斯科特迈耶斯在他关于这个主题的书中有一个很好的项目,但它似乎没有在任何地方在网上描述。



[建议:要精通在C ++问题上,阅读Scott Meyer关于这一主题的书籍: http://www.aristeia.com/books.html [ ^ ]]


< blockquote>你究竟要求什么?



默认构造函数只是一个没有参数的构造函数,复制构造函数是唯一的参数是一个实例的构造函数相同的类(通常是const引用)。

例如

  class  MyClass 
{
public
// 默认构造函数
MyClass();
// 参数化构造函数
MyClass

int someVariable
);
// 复制构造函数
MyClass

const MyClass& other
);
// 其他类定义....
}

还有什么需要知道的?



不幸的是我现在还没有完成C ++,因为C#必须更容易和更快地生成好的代码;从我个人的C ++库中快速浏览一下,我发现定义所有三个的唯一具体例子是默认和复制构造函数被明确定义为私有而未实现的一个,因此该类只能通过一个特定的构造函数。这样的事情:

  class 文件
{
public
文件

const std :: string& inFilePath
);
virtual ~File();
// 其他公共方法
private
File(); // !<默认构造函数是private和NOT-IMPLEMENTED; class必须用路径名构造。
文件

const 文件&其他
); // !<复制构造函数是私有的,而不是实现的,因为暂时我们想要禁止复制这个类的对象。
File& operator =

const 文件&其他
); // !<赋值运算符是私有的而不是实现的,因为暂时我们想要禁止复制此类的对象。
private
// 会员变量

};





问候,

Ian。


#include< conio.h>

#include< iostream.h>



班级代码

{

public:

int id;

code()//默认构造函数

{

}

code(int a)//参数化构造函数

{

id = a;

}

代码(代码& x)//复制构造函数

{

id = x.id;

}

无效显示()

{

cout<< id;

}



};



int main()

{

clrscr();

代码A(100);

代码B(A); //复制Const。调用

代码C = A; //复制const。再次打电话给







cout<<\\'n id:A:;

A.display();

cout<<\\ n:B的ID:;

B.display();

cout<<\\ n:C的C:;

C.display();



getch();

}



-------------



O / P



id A:100

id of B:100

id of C:100


I want to see a C++ program that shows the use of default, perameterised and copy constuctor.

解决方案

Despite the simplicity of the question, it is a fundamental principle that is not well described from all angles in a single place.

Some of the pages I have looked at that have reasonable descriptions of one or more issues:
- http://www.cprogramming.com/tutorial/constructor_destructor_ordering.html[^]
- http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=15[^]
- http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm[^]
- http://www.cprogramming.com/tutorial/initialization-lists-c++.html[^]
- http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6[^]

I can''t find a good link that describes why order of class members is important to an initialization list. Scott Meyers has a good item in his book on the subject but it doesn''t seem to be described on the web anywhere.

[Recommendation: To become well versed in C++ issues, read Scott Meyer''s books on the subject: http://www.aristeia.com/books.html[^]]


What exactly are you asking for?

A default constructor is simply a constructor that has no parameters and a copy constructor is one whose only parameter is an instance of the same class (usually a const reference).
E.g.

class MyClass
{
    public:
        // Default constructor
        MyClass();
        // "Parameterised" constructor
        MyClass
            (
            int someVariable
            );
        // Copy constructor
        MyClass
            (
            const MyClass& other
            );
    // rest of class definition....
}

What else is there to know?

Unfortunately I''ve not done C++ for a while now, because C# is so must easier and faster to produce good code; from a very quick look in my personal C++ library, the only concrete example I''ve found that defines all three is one where the default and copy constructors are explicitly defined as private and not implemented, so that the class can only be constructed through a specific constructor. Something like this:

class File
{
    public:
                File
                    (
                    const std::string&      inFilePath
                    );
    virtual    ~File();
        // Other public methods
    private:
                File(); //!< Default constructor is private and NOT-IMPLEMENTED; class must be constructed with a pathname.
                File
                    (
                    const File&         other
                    ); //!< Copy constructor is private and NOT-IMPLEMENTED, because for the time-being we want to disallow copying objects of this class.
                File& operator=
                    (
                    const File&         other
                    ); //!< Assignment operator is private and NOT-IMPLEMENTED, because for the time-being we want to disallow copying objects of this class.
    private:
        // Member variables

};



Regards,
Ian.


#include <conio.h>
#include <iostream.h>

class code
{
public:
int id;
code() //Default Constructor
{
}
code (int a) //Parameterised Constructor
{
id=a;
}
code (code & x) //Copy Constructor
{
id=x.id;
}
void display()
{
cout <<id;
}

};

int main()
{
clrscr();
code A(100);
code B(A); // Copy Const. called
code C=A; //Copy const. called again



cout<<"\n id of A: ";
A.display();
cout<<"\n id of B: ";
B.display();
cout<<"\n id of C: ";
C.display();

getch();
}

-------------

O/P

id of A: 100
id of B: 100
id of C: 100


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

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