Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why cann't I access protected var from a inherited class?

Reply
Thread Tools

why cann't I access protected var from a inherited class?

 
 
Roka
Guest
Posts: n/a
 
      03-31-2006
Hi all
why cann't I access protected var from a inherited class?
Code example:

class Base{
protected:
int color;
public:
virtual void show() = 0;
};

class Testublic Base{
public:
Test(int c = 1):color(c){ }; //WHY COMPILE ERROR ??
void show(){
cout << color << endl; // WHY THIS IS OK ??
}
};

int main(void){
Test* test = new Test(10);
test->show();

return 0;
}


THANKS;

 
Reply With Quote
 
 
 
 
Roka
Guest
Posts: n/a
 
      03-31-2006
Sorry. My compiler is g++ on linux.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-31-2006
* Roka:
>
> class Base{
> protected:
> int color;
> public:
> virtual void show() = 0;
> };
>
> class Testublic Base{
> public:
> Test(int c = 1):color(c){ }; //WHY COMPILE ERROR ??
> void show(){
> cout << color << endl; // WHY THIS IS OK ??
> }
> };


You get a compilation error because you're trying to /construct/ a base
class member. Not using it, but constructing (initializing) it. That's
the base class' responsibility.

What you can do is to delegate the job to the base class:

class Base {
protected:
int color;
public:
Base( int aColor ): color( aColor ) {}
virtual void show() const = 0;
};

class Test: public Base {
public:
Test( int aColor = 1 ): Base( aColor ) {}
void show() const { std::cout << color << std::endl; }
};

This way Base gives a guarantee that its 'color' member is initialized
whereever you have access to a Base object, which is the point of
constructors.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Roka
Guest
Posts: n/a
 
      03-31-2006
>Alf P. Steinbach 写道:
> You get a compilation error because you're trying to /construct/ a base
> class member. Not using it, but constructing (initializing) it. That's
> the base class' responsibility.
>
> What you can do is to delegate the job to the base class:
>
> class Base {
> protected:
> int color;
> public:
> Base( int aColor ): color( aColor ) {}
> virtual void show() const = 0;
> };
>
> class Test: public Base {
> public:
> Test( int aColor = 1 ): Base( aColor ) {}
> void show() const { std::cout << color << std::endl; }
> };
>
> This way Base gives a guarantee that its 'color' member is initialized
> whereever you have access to a Base object, which is the point of
> constructors.


>

Thank you very much.
So, if I use protected then I can access it but I cannot initialize it
.. Is that right?

what about create a set_color() function in the Base class ?
like:
class Base {
protected:
int color;
public:
virtual void show() const = 0;
void set_color(int c){ // NEW
color = c;
}
};

And use set_color(int) in inherited class instead of initialize color
in constractor of Bass class.

Is that way not good?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-31-2006
* Roka:
>* Alf P. Steinbach:
>> You get a compilation error because you're trying to /construct/ a base
>> class member. Not using it, but constructing (initializing) it. That's
>> the base class' responsibility.
>>
>> What you can do is to delegate the job to the base class:
>>
>> class Base {
>> protected:
>> int color;
>> public:
>> Base( int aColor ): color( aColor ) {}
>> virtual void show() const = 0;
>> };
>>
>> class Test: public Base {
>> public:
>> Test( int aColor = 1 ): Base( aColor ) {}
>> void show() const { std::cout << color << std::endl; }
>> };
>>
>> This way Base gives a guarantee that its 'color' member is initialized
>> whereever you have access to a Base object, which is the point of
>> constructors.

>
> Thank you very much.
> So, if I use protected then I can access it but I cannot initialize it
> . Is that right?


Not quite. 'protected' has nothing to do with. You couldn't initialize
a base class member, using a constructor initializer list in a derived
class, even if that member were 'public'.


> what about create a set_color() function in the Base class ?
> like:
> class Base {
> protected:
> int color;
> public:
> virtual void show() const = 0;
> void set_color(int c){ // NEW
> color = c;
> }
> };
>
> And use set_color(int) in inherited class instead of initialize color
> in constractor of Bass class.
>
> Is that way not good?


It's generally ungood to leave things uninitialized. Having some
set_color function /in addition/ isn't necessarily evil. But leaving
things uninitialized (you have no constructor) is, in general.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Roka
Guest
Posts: n/a
 
      03-31-2006

>>Alf P. Steinbach 写道:

Thank you very much;
 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      04-02-2006

"Roka" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all
> why cann't I access protected var from a inherited class?
> Code example:
>
> class Base{
> protected:
> int color;
> public:
> virtual void show() = 0;
> };
>
> class Testublic Base{
> public:
> Test(int c = 1):color(c){ }; //WHY COMPILE ERROR ??
> void show(){
> cout << color << endl; // WHY THIS IS OK ??
> }
> };
>
> int main(void){
> Test* test = new Test(10);
> test->show();
>
> return 0;
> }
>
>
> THANKS;
>


The following code has 2 errors, understanding them will help you to learn
some of the access rules involved in protected member in base class.

#include <iostream>
using std::cout; using std::endl;

class Base{
protected:
int color;
public:
Base(int c=1): color(c){}
virtual void show() = 0;
};

class Testublic Base{
public:
Test(int c = 1):color(c){ }; //WHY COMPILE ERROR ??
void show(){
cout << color << endl; // WHY THIS IS OK ??
}
void display(Base * b){
cout << b->color << endl; // ERROR
}
};

int main(void){
Test* test = new Test(10);
test->show();
Base * t2 = test;
t2->show();
test->display(t2);

return 0;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 0 06-20-2005 10:54 AM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM



Advertisments