类构造函数和成员 [英] class constructor and members

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

问题描述

关于ctors和班级成员的一些问题


以下是v私人吗?如果这是为什么把声明放在

顶部?把它放在私有部分有什么区别吗



对v()的引用定义了构造函数,它将调用v'的

构造函数,对吗?


class vec

{

std :: vector< celement v;

public:

vec():v(){}

如果你声明一个空的构造函数,如下所示是当它被遗漏的时候有什么不同?
的区别? C ++与其他

语言的不同之处在于,您不必明确地从编程中调用默认的ctor

。一个。


班级作品

{

int a;

double b;

public:

compos(){} //< - 但是里面什么都没有?


最后这个例子最后有与之前的结果相同

一个?


类组合

{

int a;

双b;

公开:

compos();


compos :: compos

{}

解决方案

im ***** @ hotmail.co.uk 写道:


关于ctors和班级成员的一些问题


以下是v私有吗?如果这是为什么把声明放在

顶部?把它放在私有的

部分有什么区别吗?

对v()的引用定义了构造函数,它将调用v'的

构造函数,对吗?


class vec

{

std :: vector< celement v;



这里''v''是私有的。


public:

vec():v(){}



这里由于某种原因省略了''vec''定义的结尾。


>


如果你声明一个空的构造函数,如下所示是否有任何

的差异。遗漏了?



No.


C ++与其他

语言的区别在于你不必在编程中明确地调用默认的ctor

。一。



我想我不明白这个问题。此时在C ++中你不能从b内调用默认的ctor。桌面上有一个建议

添加此功能,但它不会很快进入语言


< blockquote class =post_quotes>
>

class compos

{

int a;

双b;



''a''和'b''是私有的。


public:

compos(){} //< - 但是什么都没有?



是的,没什么。两个成员都没有被初始化。


>

最后这个最后一个例子的结果与之前的结果相同

一个?


班级作品

{

int a;

double b;

public:

compos();



缺少:


};


>

compos :: compos

{}



当然。你只是将定义移到了外面,你没有改变这个函数的作用以及它是如何做到的。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


感谢您的回答,只需要几件事。


Victor Bazarov写道:

im*****@hotmail.co.uk 写道:


public:

vec():v(){}



这里省略了结束出于某种原因,vec的定义。



你的意思是其余的定义(如果有的话)和结束

括号?对不起,我以为你会更喜欢简洁,你可以阅读更少的b $ b行;}


C ++与其他

语言的不同之处在于,您不必明确地从编程中调用默认的ctor

。一。



我想我不明白这个问题。此时在C ++中你不能从b内调用默认的ctor。桌面上有一个建议

添加此功能,但它不会很快进入语言




在其他一些语言中,如果你想覆盖默认的构造函数

你必须显式调用默认构造函数,否则你将

陷入困境......什么都不会被创造出来。所以在C ++中它有点奇怪看到空括号或只是一些变量集,通常

会有一个函数调用。这就是为什么我在询问

空括号时它们可能是空的但是仍然有一个有效的

构造函数。所以一个自然要问的问题是:是否在开始时调用默认的

构造函数?


类组合

{

int a;

double b;

public:

compos();

};


compos :: compos()

{

//之前调用它:

a = 1;

}


im ***** @ hotmail.co.uk 写道:


感谢您的回答,只需要几件事。


在其他一些语言中,如果要覆盖默认构造函数

,则必须显式调用默认构造函数,否则你会不会有麻烦......什么都不会创造出来。



构造函数不用C ++创建对象。

你不能打电话给他们。在每个对象创建

期间,

实现在适当的过程中为你调用构造函数。


你' '还滥用术语。 默认构造函数

是可以不带参数调用的。如果你没有定义

* ANY *构造函数,编译器会为你隐式生成一个空的

。如果你想定义自己的,你可以做其他的

,但是你可以用不同的方式添加你想要的东西。


所有的构造函数基类和非静态数据成员

(子对象)也是在你为类定义的构造函数的

主体被执行之前被调用的。 。

再一次,你无能为力改变这一点。你可以影响的唯一的事情就是选择哪个构造函数由(sub)对象创建的

参数。


Some questions about ctors and class members

Is v private in the following ? If it is why put the declaration at
the top ? Is there any difference to putting it in the private section
?
The reference to v() defines the constructor, it will call v''s
constructor, right ?

class vec
{
std::vector< celement v;
public:
vec() : v() { }
If you declare an empty constructor like In the following is there any
difference to when it is left out ? Does C++ differ from other
languages in that you do not have to explicitely call the default ctor
from within the "programed" one.

class compos
{
int a;
double b;
public:
compos() { } // <- but there is nothing in it ?

Finally does this last example have the same results as the previous
one ?

class compos
{
int a;
double b;
public:
compos();

compos::compos
{ }

解决方案

im*****@hotmail.co.uk wrote:

Some questions about ctors and class members

Is v private in the following ? If it is why put the declaration at
the top ? Is there any difference to putting it in the private
section ?
The reference to v() defines the constructor, it will call v''s
constructor, right ?

class vec
{
std::vector< celement v;

Here ''v'' is private.

public:
vec() : v() { }

And here you omit the end of the ''vec'' definition for some reason.

>

If you declare an empty constructor like In the following is there any
difference to when it is left out ?

No.

Does C++ differ from other
languages in that you do not have to explicitely call the default ctor
from within the "programed" one.

I guess I don''t understand the question. In C++ at this point you cannot
"call the default ctor from within". There is a proposal on the table to
add this functionality, but it''s not going to make it into the language
any time soon.

>
class compos
{
int a;
double b;

''a'' and ''b'' are private.

public:
compos() { } // <- but there is nothing in it ?

Yep, nothing. Both members are left uninitialised.

>
Finally does this last example have the same results as the previous
one ?

class compos
{
int a;
double b;
public:
compos();

Missing:

};

>
compos::compos
{ }

Sure. You just moved the definition outside, you didn''t change what
the function does and how it does it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Thanks for the answers, just a few things.

Victor Bazarov wrote:

im*****@hotmail.co.uk wrote:

public:
vec() : v() { }


And here you omit the end of the ''vec'' definition for some reason.

You mean the rest of the definitions (if there were any) and the end
bracket ? Sorry I thought you would prefer brevity, it is one less
line for you to read ;}

Does C++ differ from other
languages in that you do not have to explicitely call the default ctor
from within the "programed" one.


I guess I don''t understand the question. In C++ at this point you cannot
"call the default ctor from within". There is a proposal on the table to
add this functionality, but it''s not going to make it into the language
any time soon.

In some other languages if you want to override the default constructor
you have to explicitly call the default constructor otherwise you will
be in big trouble.. nothing will be created. So in C++ it is a bit
strange seeing empty brackets or just some variable set, where normally
there would be a function call. This was why I was asking about the
empty brackets they may be empty but there is still a valid
constructor. So a natural question to ask is: is the default
constructor called at the begining ?

class compos
{
int a;
double b;
public:
compos();
};

compos::compos()
{
// is it called before this:
a = 1;
}


im*****@hotmail.co.uk wrote:

Thanks for the answers, just a few things.

In some other languages if you want to override the default constructor
you have to explicitly call the default constructor otherwise you will
be in big trouble.. nothing will be created.

Constructors do NOT create object in C++.
You can''t call them. The constructor is called for you by the
implementation in the proper course during the creation of
each object.

You''re also abusing the terminology. The "default constructor"
is one that can be called with no arguments. If you do not define
*ANY* constructors, the compiler implicitly generates an empty one
for you. If you want to define your own, there''s NOTHING else
for you to do but add the stuff you want done differently.

Constructors for all the base classes and non-static data members
(subobjects) are called as a matter of course as well before the
body of the constructor you define for the class is executed.
Again there is nothing you can to do change this. The only thing
that you can affect is the selection of which constructor by what
arguments are given to the (sub)object creation.


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

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