
March 31st, 2006, 02:05 AM
| | | why cann't I access protected var from a inherited class?
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 Test:public 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; | 
March 31st, 2006, 02:05 AM
| | | Re: why cann't I access protected var from a inherited class?
Sorry. My compiler is g++ on linux. | 
March 31st, 2006, 02:15 AM
| | | Re: why cann't I access protected var from a inherited class?
* Roka:[color=blue]
>
> class Base{
> protected:
> int color;
> public:
> virtual void show() = 0;
> };
>
> class Test:public Base{
> public:
> Test(int c = 1):color(c){ }; //WHY COMPILE ERROR ??
> void show(){
> cout << color << endl; // WHY THIS IS OK ??
> }
> };[/color]
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? | 
March 31st, 2006, 02:45 AM
| | | Re: why cann't I access protected var from a inherited class?
>Alf P. Steinbach 写道:[color=blue]
> 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.[/color]
[color=blue]
>[/color]
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? | 
March 31st, 2006, 02:55 AM
| | | Re: why cann't I access protected var from a inherited class?
* Roka:[color=blue]
>* Alf P. Steinbach:[color=green]
>> 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.[/color]
>
> Thank you very much.
> So, if I use protected then I can access it but I cannot initialize it
> . Is that right?[/color]
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'.
[color=blue]
> 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?[/color]
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? | 
March 31st, 2006, 03:25 AM
| | | Re: why cann't I access protected var from a inherited class?
[color=blue][color=green]
>>Alf P. Steinbach 写道:[/color][/color]
Thank you very much; | 
April 2nd, 2006, 04:45 AM
| | | Re: why cann't I access protected var from a inherited class?
"Roka" <Roka100@gmail.com> wrote in message
news:1143769822.887253.107550@i40g2000cwc.googlegr oups.com...[color=blue]
> 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 Test:public 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;
>[/color]
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 Test:public 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;
} | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,338 network members.
|