Click to See Complete Forum and Search --> : private typedef as return type of private function
Graham
September 19th, 2002, 10:38 AM
For information, really (I tried Comeau, and it doesn't complain), here's another VC++ non-standardism:
// A.h
#include <vector>
class A
{
typedef std::vector<int>::iterator iterator;
public:
A() : v(10, 1) {}
private:
iterator f();
std::vector<int> v;
};
// A.cpp
#include "A.h"
A::iterator A::f()
{
return v.begin();
}
VC++6.0 rejects this, saying that you can't access the private typedef A::iterator. Basically it seems to be failing to get the context of the (private) function before deciding whether private access is allowed. As I said, Comeau has no problem with it.
jwbarton
September 19th, 2002, 12:12 PM
I tried this example in my copy of Visual C++ 6.0 (SP5) and it compiled without any complaints. Are you missing something from this example?
Best regards,
John
AnthonyMai
September 19th, 2002, 12:53 PM
It compiles with no problem on my machine, too. My VC++ don't even have SP5 installed.
Microsoft bashing has become a prominent part of the developer culture. Whenver something doesn't work people jump right onto Microsoft and assume M$ to be guilty until proven innocent.
Graham
September 19th, 2002, 01:49 PM
John: possibly the SP5 part. Or it may be that the actual example (which involved nested classes and multiple files) is a bit more complex. I will admit to not trying that simple example - what I had was more along the lines of:
// A.h
class A
{
// blah, blah
private:
class B;
B* mem;
};
// AB.h
#include "A.h"
class A::B
{
typedef /*whatever*/ type
//blah
private:
type f();
};
//AB.cpp
A::B::type A::B::f()
{
}
Still, it may be the fact that my company has kept us at a lower SP that's the problem.
jwbarton
September 19th, 2002, 02:16 PM
I tried the following test, and get the error C2248 that you described:
class A
{
public:
A()
{
}
private:
class B;
B *pB;
};
class A::B
{
typedef B* ptrB;
public:
ptrB GetOne();
};
A::B::ptrB A::B::GetOne()
{
return new B;
}
Does this compile with the Comeau compiler? I am not sure whether this should compile, as I don't know what the rules are for typedefs being defined inside of a class.
Best regards,
John
Graham
September 19th, 2002, 02:29 PM
It looks like I may have unjustly maligned VC++. Comeau throws the nested class version out, too. I had assumed that nesting in this case wouldn't (or shouldn't) make a difference - if it works one level deep, why not two levels? Mea culpa - just goes to show where assumptions, however apparently reasonable, get you.
For the record, this is what I threw at it:
class A
{
public:
A() {}
private:
class B;
B* p;
};
class A::B
{
typedef B* pB;
pB f();
};
A::B::pB A::B::f()
{
return new B;
}
and the message reported is:
"ComeauTest.c", line 17: error: class "A::B" is inaccessible
A::B::pB A::B::f()
^
Which is not quite the same error, but in the same ball park
Anybody here able to work out what the standard says about this, and why there should be a difference in behaviour between outer-level and nested classes.
It all seems a bit odd, because you have to revert to the type that the typedef is an alias for in order to define the function.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.