 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chetan Raj Guest
|
Posted: Tue Apr 26, 2005 9:22 am Post subject: Protected Constructor |
|
|
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 = "<
}
};
int main(int argc, char* argv[])
{
B b;
return 0;
}
The compiler will show the following error at line containg A* p =
new A();
| Quote: |
'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! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Apr 26, 2005 1:35 pm Post subject: Re: Protected Constructor |
|
|
Chetan Raj wrote:
| Quote: | I don't understand why the following code does not compile...
#include
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 = "<
}
};
[...]
When we can use the function GiveX(), why cannot we use the protected
constructor?
|
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
|
|
| Back to top |
|
 |
Maciej Pilichowski Guest
|
Posted: Tue Apr 26, 2005 1:42 pm Post subject: Re: Protected Constructor |
|
|
On 26 Apr 2005 05:22:36 -0400, "Chetan Raj" <hichetu (AT) gmail (DOT) com>
wrote:
| Quote: | 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
{
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 t³umacz (wczesna wersja rozwojowa) angielsko-polski
|
|
| Back to top |
|
 |
codigo Guest
|
Posted: Tue Apr 26, 2005 4:27 pm Post subject: Re: Protected Constructor |
|
|
"Maciej Pilichowski" <bantu (AT) SKASUJTOpoczta (DOT) FM> wrote
| Quote: | On 26 Apr 2005 05:22:36 -0400, "Chetan Raj" <hichetu (AT) gmail (DOT) com
wrote:
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
|
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.
| Quote: | {
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.
|
|
| Back to top |
|
 |
Maciej Pilichowski Guest
|
Posted: Wed Apr 27, 2005 7:32 am Post subject: Re: Protected Constructor |
|
|
On Tue, 26 Apr 2005 12:27:08 -0400, "codigo" <codigo (AT) codigo (DOT) trap.com>
wrote:
| Quote: | You can't call a ctor. Constructors are not called, they are invoked.
|
Ok, language /English/ issues -- my mistake :-)
| Quote: | Thats
because you can't overide or redefine a base class constructor.
|
Maybe it is the same as the above, but yes, I can...
| Quote: | The B class
can call, overload or overide GiveX() but it can only invoke A's ctor.
|
....sure, but as well it doesn't have to, right? So
A::A()
{
something();
}
B::B()
{
something_completely_different();
};
| Quote: | 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.
|
a) this is just an example of different matter
b) you can't free any memory because this code won't compile -- that's
why
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 t³umacz (wczesna wersja rozwojowa) angielsko-polski
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Apr 27, 2005 8:14 am Post subject: Re: Protected Constructor |
|
|
Chetan Raj wrote:
not when I use only GiveX() function.
| Quote: |
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?
|
Because A()'s constructor isn't accessible there. A protected member
can only be accessed by derived instances of the same object. Here
you're creating a different object in B's constructor.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
radu.chindris@gmail.com Guest
|
Posted: Wed Apr 27, 2005 8:26 am Post subject: Re: Protected Constructor |
|
|
The problem is that B doesn't inherit the constructor from A (
constructors, destructor, the = operator and friends are not
inherited).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Apr 27, 2005 8:36 am Post subject: Re: Protected Constructor |
|
|
Chetan Raj wrote: (edited for brevity)
| Quote: | #include <iostream
using namespace std;
class A {
protected:
A() : x(5) {}
int GiveX() { return x; }
int x ;
};
class B : public A {
public:
B() {
// This line gets error "cannot access protected member"
A* p = new A();
// But this line does not:
cout<<"I am B with x = "<
}
};
|
I was certain this was one of Marshal Cline's FAQs, but I couldn't
find it when I searched.
Chetan, this confused me too when I first encountered it. The answer
is that just because B inherits from A does not mean that it has
friend-type access to all of the protected members. It only has
access to the protected members of the base object -- the A that's
part of the B.
Here's another example of what does not work:
class C : public A {
public:
C() {
A*p = this; // Okay
cout << GiveX(); // Okay
cout << this->GiveX(); // Also okay
cout << p->GiveX(); // No.
}
};
You cannot access protected members of A through a pointer to A.
You can only access protected members of A through a pointer to C
(or the implied this pointer). This is intentional.
You can solve your problem by making B a friend of A -- but I
suspect you already knew that.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Wed Apr 27, 2005 8:43 am Post subject: Re: Protected Constructor |
|
|
"Chetan Raj" <hichetu (AT) gmail (DOT) com> skrev i meddelandet
news:1114430232.178343.79190 (AT) o13g2000cwo (DOT) googlegroups.com...
| Quote: | 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 = "<
}
};
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' : cannot access protected member declared in class 'A'
but not when I use only GiveX() function.
|
The GiveX function is a part of the B object, it actually calls
this->GiveX(). It has access to this function, because it inherits it
from its base class A.
When it tries to create a new A object, that's a totally different
object to which it does not have access.
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
codigo Guest
|
Posted: Wed Apr 27, 2005 8:50 am Post subject: Re: Protected Constructor |
|
|
"Chetan Raj" <hichetu (AT) gmail (DOT) com> wrote
| Quote: | Hi All,
I don't understand why the following code does not compile...
class B : public A
{
public:
B()
{
A* p = new A();
|
the variable *p is *not* a member of the B class. Neither can B inherit from
an instance. One does not inherit from a variable. Since the A ctor is
protected, the above doesn't compile. The class B() ctor should look like
this:
B() : A() { cout << "B ctor invokedn"; }
The A class found in B's initialization list *is* an inherited class (not an
instance). Instantiation of a class B variable therefore *invokes* the base
class ctor this way.
So...
B *ptr = new B();
invokes both: A's protected ctor and invokes B's ctor.
A class is not a variable (an instance). You can't inherit from an instance.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed May 04, 2005 11:53 pm Post subject: Re: Protected Constructor |
|
|
Allan W wrote:
| Quote: | Chetan Raj wrote: (edited for brevity)
#include <iostream
using namespace std;
class A {
protected:
A() : x(5) {}
int GiveX() { return x; }
int x ;
};
class B : public A {
public:
B() {
// This line gets error "cannot access protected member"
A* p = new A();
// But this line does not:
cout<<"I am B with x = "<
}
};
I was certain this was one of Marshal Cline's FAQs, but I
couldn't find it when I searched.
Chetan, this confused me too when I first encountered it. The
answer is that just because B inherits from A does not mean
that it has friend-type access to all of the protected
members. It only has access to the protected members of the
base object -- the A that's part of the B.
|
Almost. You need to replace the 'the's with 'a's in the last
sentence. A derived class B can only access the protected
members of a base A in objects whose actual type is a B (or
derived from B). In his example, for example :
B* pB ;
pB->GiveX() ; // Legal.
A* pA ;
pA->GiveX() ; // Illegal.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|