受保护的构造函数 [英] Protected Constructor

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

问题描述

大家好,


我不明白为什么以下代码无法编译...

#include< iostream>


使用命名空间std;


class A

{

protected:

A()

{

cout<<""我是A;" ;;

x = 5;

}


int GiveX()

{

返回x;

}

int x;


};


B级:公共A

{

公开:

B()

{

A * p =新A();

cout<<"我是B,x ="<<< GiveX()< ;< endl;

}

};

int main(int argc,char * argv [])

{

B b;

返回0;

}

编译器将显示以下错误:包含A * p =

的新行A();

Hi All,

I don''t understand why the following code does not compile...
#include <iostream>

using namespace std;

class A
{
protected:
A()
{
cout<<" I am A;";
x = 5;
}

int GiveX()
{
return x;
}
int x ;

};

class B : public A
{
public:
B()
{
A* p = new A();
cout<<"I am B with x = "<<GiveX()<<endl;
}
};
int main(int argc, char* argv[])
{
B b;
return 0;
}
The compiler will show the following error at line containg A* p =
new A();



''A :: A'':无法访问在类'A'中声明的受保护成员

<<

但不能仅使用GiveX( )函数。


B类公开形式A. A()和GiveX()都受到保护

在A类中的功能。


当w e可以使用GiveX()函数,为什么我们不能使用受保护的

构造函数?


任何人都可以解释这种行为吗?

提前致谢,

Chetan Raj

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated 。第一次海报:做到这一点! ]


''A::A'' : cannot access protected member declared in class ''A''
<<
but not when I use only GiveX() function.

class B derives publicly form A. Both A() and GiveX() are protected
functions in class A.

When we can use the function GiveX(), why cannot we use the protected
constructor?

Can anyone explain this behaviour?

Thanks in advance,
Chetan Raj
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

Chetan Raj写道:
Chetan Raj wrote:
我不明白为什么以下代码没有编译...

#include< iostream>

使用命名空间std;

A类
{
protected :
A()
{
cout<<<"我是A;" ;;
x = 5;
}

int GiveX()
{
返回x;
}
int x;

};

B班:公众A
{
公开:
B() {
A * p =新A();
cout<<"我是B,x ="<<< GiveX()<< endl;
}

};
[...]
当我们可以使用GiveX()函数时,为什么我们不能使用受保护的
构造函数?
I don''t understand why the following code does not compile...
#include <iostream>

using namespace std;

class A
{
protected:
A()
{
cout<<" I am A;";
x = 5;
}

int GiveX()
{
return x;
}
int x ;

};

class B : public A
{
public:
B()
{
A* p = new A();
cout<<"I am B with x = "<<GiveX()<<endl;
}
};
[...]
When we can use the function GiveX(), why cannot we use the protected
constructor?




因为对象只允许访问A'的受保护成员,因为

实例与他们自己的类型相同。当涉及到c-tors时,只允许创建基类子对象,而不是一个独立的对象。


V



Because objects are only allowed to access A''s protected members for the
instances of the same type as their own. When c-tors are concerned, only
creating of the base class subobject is allowed, not a stand-alone object.

V


2005年4月26日05:22:36 -0400,Chetan Raj < hi ***** @ gmail.com>

写道:
On 26 Apr 2005 05:22:36 -0400, "Chetan Raj" <hi*****@gmail.com>
wrote:
B类公开形成A. A()和GiveX()在A类中受保护
函数。
class B derives publicly form A. Both A() and GiveX() are protected
functions in class A.




....所以你可以将它们作为_current_ object / of B /!的一部分使用。


B :: B():A()//为此对象调用一个c-tor,可见 - >允许

{

A * p =新A(); //为一个物体打电话给c-tor,不可见

}


有美好的一天,再见

-

Maciej" MACiAS" Pilichowski http://bantu.fm.interia.pl/


MARGOT - > http://www.margot.cad.pl/

automatyczny t3umacz(wczesna wersja rozwojowa)angielsko-polski



....so you can use both as part of _current_ object /of class B/!

B::B() : A() // call A c-tor for THIS object, visible -> allowed
{
A* p = new A(); // call A c-tor for an object, not visible
}

have a nice day, bye
--
Maciej "MACiAS" Pilichowski http://bantu.fm.interia.pl/

M A R G O T --> http://www.margot.cad.pl/
automatyczny t3umacz (wczesna wersja rozwojowa) angielsko-polski




" Maciej Pilichowski" < BA *** @ SKASUJTOpoczta.FM>在消息中写道

news:92 ******************************** @ 4ax.com ...

"Maciej Pilichowski" <ba***@SKASUJTOpoczta.FM> wrote in message
news:92********************************@4ax.com...
2005年4月26日05:22:36 -0400,Chetan Raj < hi ***** @ gmail.com>
写道:
On 26 Apr 2005 05:22:36 -0400, "Chetan Raj" <hi*****@gmail.com>
wrote:
B类公开形式A. A()和GiveX()都受到保护 ...所以你可以同时使用它们作为_current_ object / B / B的一部分!

B :: B():A()//为此对象调用一个c-tor,可见 - >允许
class B derives publicly form A. Both A() and GiveX() are protected
functions in class A.
...so you can use both as part of _current_ object /of class B/!

B::B() : A() // call A c-tor for THIS object, visible -> allowed




你不能打电话给ctor。不调用构造函数,调用它们。那是

,因为你不能覆盖或重新定义基类构造函数。 B类

可以调用,重载或覆盖GiveX(),但它只能调用A'的ctor。

{
A * p = new A (); //为一个对象打电话给c-tor,不可见


这引发了一个棘手的问题(这就是你的帖子让我乞求的问题

问。 ..)。一旦构造函数完成构建后,指针p就不再存在了,那么如何在p处解除分配A的实例呢?


答案是你可以不要删除p。这是一个保密的内存泄漏。

}

祝你有愉快的一天,再见
-
Maciej" MACiAS" Pilichowski
http://bantu.fm.interia.pl/
MARGOT - > http://www.margot.cad.pl/
automatyczny t3umacz(wczesna wersja rozwojowa)angielsko-polski



You can''t call a ctor. Constructors are not called, they are invoked. Thats
because you can''t overide or redefine a base class constructor. The B class
can call, overload or overide GiveX() but it can only invoke A''s ctor.
{
A* p = new A(); // call A c-tor for an object, not visible
Which raises a thorny issue (thats the question your post has me begging to
ask...). Since pointer p ceases to exist once the constructor has finished
constructing, how do you then deallocate the instance of A at p?

The answer is you can''t delete p. Thats a guarenteed memory leak.
}

have a nice day, bye
--
Maciej "MACiAS" Pilichowski http://bantu.fm.interia.pl/
M A R G O T --> http://www.margot.cad.pl/
automatyczny t3umacz (wczesna wersja rozwojowa) angielsko-polski



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

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