Weird protected access problem.

  • Thread starter =?gb2312?B?wfXquw==?=
  • Start date
?

=?gb2312?B?wfXquw==?=

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

Alf P. Steinbach

* ??:
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
 
G

Guest

* ??:















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

Alf P. Steinbach

* (e-mail address removed):
Please don't quote signatures.

Please read the FAQ's section's about posting, before posting.

Cheers,

- Alf
 
A

anon

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

Guest

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top