| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"=?gb2312?B?wfXquw==?=" |
| Date: |
23 Aug 2007 09:40:04 PM |
| Object: |
Weird protected access problem. |
Hi, folks,
My g++ is of version 3.4.2, here is the code snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is B\n";
}
};
template<typename T>
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived<T> D() {
Base b;
return Derived<T>(b);
}
};
int main() {
Derived<int> obj;
obj.D();
return 1;
}
When I tried to build this program, an error arraised:
test.cpp:7: error: `Base::Base()' is protected
test.cpp:18: error: within this context
Any informative tips will be appreciated, thank you.
Best regards.
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Weird protected access problem. |
23 Aug 2007 09:49:41 PM |
|
|
* ??:
Hi, folks,
My g++ is of version 3.4.2, here is the code snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is B\n";
}
};
template<typename T>
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived<T> D() {
Base b;
return Derived<T>(b);
}
};
int main() {
Derived<int> obj;
obj.D();
return 1;
}
When I tried to build this program, an error arraised:
test.cpp:7: error: `Base::Base()' is protected
test.cpp:18: error: within this context
Any informative tips will be appreciated, thank you.
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
Cheers, & hth.,
- Alf
--
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?
.
|
|
|
| User: "" |
|
| Title: Re: Weird protected access problem. |
23 Aug 2007 10:07:23 PM |
|
|
On 8 24 , 10 49 , "Alf P. Steinbach" <al...@start.no> wrote:
* ??:
Hi, folks,
My g++ is of version 3.4.2, here is the code snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is B\n";
}
};
template<typename T>
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived<T> D() {
Base b;
return Derived<T>(b);
}
};
int main() {
Derived<int> obj;
obj.D();
return 1;
}
When I tried to build this program, an error arraised:
test.cpp:7: error: `Base::Base()' is protected
test.cpp:18: error: within this context
Any informative tips will be appreciated, thank you.
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
Cheers, & hth.,
- Alf
--
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?- -
- -
I am sorry I didn't make myself clear, here is the revised code
snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is Base\n";
}
Base(const Base& other) {
cout << "This is Base's copy ctor\n";
}
Base B() {
return Base();
}
};
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived D() {
return Derived(B());
}
};
int main() {
Derived obj;
obj.D();
return 1;
}
VC8.0: succeed,
g++3.3.1: succeed,
g++3.4.2: failed,
Could you explain more, thank you.
.
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Weird protected access problem. |
23 Aug 2007 10:16:33 PM |
|
|
* leomayleomay@gmail.com:
On 8 24 , 10 49 , "Alf P. Steinbach" <al...@start.no> wrote:
Cheers, & hth.,
- Alf
--
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?- -
Please don't quote signatures.
Please read the FAQ's section's about posting, before posting.
Cheers,
- Alf
--
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?
.
|
|
|
|
| User: "anon" |
|
| Title: Re: Weird protected access problem. |
24 Aug 2007 04:40:30 AM |
|
|
wrote:
On 8 24 , 10 49 , "Alf P. Steinbach" <al...@start.no> wrote:
* ??:
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
I am sorry I didn't make myself clear, here is the revised code
snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is Base\n";
}
Base(const Base& other) {
cout << "This is Base's copy ctor\n";
}
Base B() {
return Base();
}
};
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived D() {
return Derived(B());
}
};
int main() {
Derived obj;
obj.D();
return 1;
}
VC8.0: succeed,
g++3.3.1: succeed,
g++3.4.2: failed,
Could you explain more, thank you.
He already answered your question.
Therefore, VC8.0 and g++3.3.1 are broken regarding this thing.
Using g++ 4.1 I got:
sss.cpp: In member function ‘Derived Derived::D()’:
sss.cpp:11: error: ‘Base::Base(const Base&)’ is protected
sss.cpp:27: error: within this context
These messages clearly explain whats the problem.
.
|
|
|
| User: "" |
|
| Title: Re: Weird protected access problem. |
24 Aug 2007 05:08:12 AM |
|
|
On 8 24 , 5 40 , anon <a...@no.no> wrote:
leomayleo...@gmail.com wrote:
On 8 24 , 10 49 , "Alf P. Steinbach" <al...@start.no> wrote:
* ??:
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
I am sorry I didn't make myself clear, here is the revised code
snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is Base\n";
}
Base(const Base& other) {
cout << "This is Base's copy ctor\n";
}
Base B() {
return Base();
}
};
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived D() {
return Derived(B());
}
};
int main() {
Derived obj;
obj.D();
return 1;
}
VC8.0: succeed,
g++3.3.1: succeed,
g++3.4.2: failed,
Could you explain more, thank you.
He already answered your question.
Therefore, VC8.0 and g++3.3.1 are broken regarding this thing.
So, can I circumvent this problem if I indeed want to let Derived
object access the protected method defined in the Base object with g++
of version 3.4.2?
Thanks a lot.
Best regards
.
|
|
|
| User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?=" |
|
| Title: Re: Weird protected access problem. |
24 Aug 2007 05:55:12 AM |
|
|
On 2007-08-24 12:08, wrote:
On 8 24 , 5 40 , anon <a...@no.no> wrote:
leomayleo...@gmail.com wrote:
On 8 24 , 10 49 , "Alf P. Steinbach" <al...@start.no> wrote:
* ??:
If C++ allowed you to freely access protected features of a class in a
derived class, then you could circumvent any "protected:" access
protection simply by deriving a class; in particular you could access
Base "protected:" features on instances of SomeoneElsesDerivedClass.
Therefore you can't.
In class Derived you can only access protected Base features on
instances of Derived, or on instances of classes derived from Derived,
and just for completeness, this has nothing to do with templating.
I am sorry I didn't make myself clear, here is the revised code
snippet:
#include <iostream>
using namespace std;
class Base {
protected:
Base() {
cout << "This is Base\n";
}
Base(const Base& other) {
cout << "This is Base's copy ctor\n";
}
Base B() {
return Base();
}
};
class Derived: public Base {
public:
Derived(){}
Derived(const Base& b): Base(b) {}
Derived D() {
return Derived(B());
}
};
int main() {
Derived obj;
obj.D();
return 1;
}
VC8.0: succeed,
g++3.3.1: succeed,
g++3.4.2: failed,
Could you explain more, thank you.
He already answered your question.
Therefore, VC8.0 and g++3.3.1 are broken regarding this thing.
So, can I circumvent this problem if I indeed want to let Derived
object access the protected method defined in the Base object with g++
of version 3.4.2?
You can make them public.
--
Erik Wikström
.
|
|
|
|
|
|
|
|

|
Related Articles |
|
|