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

R

Roka

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

Alf P. Steinbach

* 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.
 
R

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?

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?
 
A

Alf P. Steinbach

* Roka:
* Alf P. Steinbach:

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

Fei Liu

Roka said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top