Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   why cann't I access protected var from a inherited class? (http://www.velocityreviews.com/forums/t452954-why-cannt-i-access-protected-var-from-a-inherited-class.html)

Roka 03-31-2006 01:50 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;


Roka 03-31-2006 01:54 AM

Re: why cann't I access protected var from a inherited class?
 
Sorry. My compiler is g++ on linux.


Alf P. Steinbach 03-31-2006 02:01 AM

Re: why cann't I access protected var from a inherited class?
 
* Roka:
>
> 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 ??
> }
> };


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?

Roka 03-31-2006 02:32 AM

Re: why cann't I access protected var from a inherited class?
 
>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?


Alf P. Steinbach 03-31-2006 02:46 AM

Re: why cann't I access protected var from a inherited class?
 
* 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?

Roka 03-31-2006 03:14 AM

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

>>Alf P. Steinbach 写道:

Thank you very much;

Fei Liu 04-02-2006 03:31 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...
> 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;
>


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;
}




All times are GMT. The time now is 05:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.