|
-
September 19th, 2002, 10:38 AM
#1
private typedef as return type of private function
For information, really (I tried Comeau, and it doesn't complain), here's another VC++ non-standardism:
Code:
// 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 19th, 2002, 12:12 PM
#2
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
-
September 19th, 2002, 12:53 PM
#3
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.
-
September 19th, 2002, 01:49 PM
#4
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:
Code:
// 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 19th, 2002, 02:16 PM
#5
I tried the following test, and get the error C2248 that you described:
Code:
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
-
September 19th, 2002, 02:29 PM
#6
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:
Code:
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:
Code:
"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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|