C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Protected Constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Chetan Raj
Guest





PostPosted: Tue Apr 26, 2005 9:22 am    Post subject: Protected Constructor Reply with 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();
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





PostPosted: Tue Apr 26, 2005 1:35 pm    Post subject: Re: Protected Constructor Reply with quote



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





PostPosted: Tue Apr 26, 2005 1:42 pm    Post subject: Re: Protected Constructor Reply with quote



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





PostPosted: Tue Apr 26, 2005 4:27 pm    Post subject: Re: Protected Constructor Reply with quote


"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.

Quote:
}

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
Maciej Pilichowski
Guest





PostPosted: Wed Apr 27, 2005 7:32 am    Post subject: Re: Protected Constructor Reply with quote

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





PostPosted: Wed Apr 27, 2005 8:14 am    Post subject: Re: Protected Constructor Reply with quote

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





PostPosted: Wed Apr 27, 2005 8:26 am    Post subject: Re: Protected Constructor Reply with quote

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





PostPosted: Wed Apr 27, 2005 8:36 am    Post subject: Re: Protected Constructor Reply with quote

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





PostPosted: Wed Apr 27, 2005 8:43 am    Post subject: Re: Protected Constructor Reply with quote


"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





PostPosted: Wed Apr 27, 2005 8:50 am    Post subject: Re: Protected Constructor Reply with quote


"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





PostPosted: Wed May 04, 2005 11:53 pm    Post subject: Re: Protected Constructor Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.