提前致谢, 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 protectedfunctions in class A.When we can use the function GiveX(), why cannot we use the protectedconstructor?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! ]推荐答案 因为对象只允许访问A'的受保护成员,因为 实例与他们自己的类型相同。当涉及到c-tors时,只允许创建基类子对象,而不是一个独立的对象。 VBecause objects are only allowed to access A''s protected members for theinstances of the same type as their own. When c-tors are concerned, onlycreating of the base class subobject is allowed, not a stand-alone object.V ....所以你可以将它们作为_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 你不能打电话给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-polskiYou can''t call a ctor. Constructors are not called, they are invoked. Thatsbecause you can''t overide or redefine a base class constructor. The B classcan 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 visibleWhich raises a thorny issue (thats the question your post has me begging toask...). Since pointer p ceases to exist once the constructor has finishedconstructing, 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" Pilichowskihttp://bantu.fm.interia.pl/ M A R G O T --> http://www.margot.cad.pl/ automatyczny t3umacz (wczesna wersja rozwojowa) angielsko-polski 这篇关于受保护的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 13:32