By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;

Mar 31 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sorry. My compiler is g++ on linux.

Mar 31 '06 #2

P: n/a
* 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?
Mar 31 '06 #3

P: n/a
>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?

Mar 31 '06 #4

P: n/a
* 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?
Mar 31 '06 #5

P: n/a
Alf P. Steinbach 写道:

Thank you very much;
Mar 31 '06 #6

P: n/a

"Roka" <Ro*****@gmail.com> wrote in message
news:11**********************@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;
}
Apr 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.