 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Friedhelm Hoerner Guest
|
Posted: Wed Sep 15, 2004 9:38 am Post subject: private typedef |
|
|
Hello,
given the following c-snippet:
class Z
{
public:
class X {
private:
typedef vector<string> Vector;
Vector v_;
public:
Vector& get();
};
};
Z::X::Vector& Z::X::get() // (1)
{
return v_;
}
static void test()
{
Z::X x;
x.get(); // (2)
vector<string> v = x.get(); // (3)
Vector v = x.get(); // (4)
int size = x.get().size(); // (5)
}
With VC6 line (1) gets an Error: "cannot access private typedef...".
With VC7 and some UNIX compilers (gcc...) only line (4) gets an Error.
The explicit use of Vector (4) is the most obvious error, but (3) and
(5) also need the intrinsics of the private typedef.
What is the correct behaviour?
Thanks, Friedhelm
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Sep 15, 2004 7:35 pm Post subject: Re: private typedef |
|
|
Friedhelm Hoerner wrote:
| Quote: | Hello,
given the following c-snippet:
class Z
{
public:
class X {
private:
typedef vector<string> Vector;
Vector v_;
public:
Vector& get();
};
};
Z::X::Vector& Z::X::get() // (1)
{
return v_;
}
static void test()
{
Z::X x;
x.get(); // (2)
vector<string> v = x.get(); // (3)
Vector v = x.get(); // (4)
int size = x.get().size(); // (5)
}
With VC6 line (1) gets an Error: "cannot access private typedef...".
|
This is a bug in the compiler. Since it is a definition of a class
member, it should be able to use the private typedef.
| Quote: | With VC7 and some UNIX compilers (gcc...) only line (4) gets an Error.
The explicit use of Vector (4) is the most obvious error, but (3) and
(5) also need the intrinsics of the private typedef.
|
Accessibility checks apply to member lookup, after name resolution.
Lines (3) and (5) do not refer to Z::X::Vector by name, so its
accessibility to them is irrelevant. I don't know what you mean by
"intrinsics" though.
| Quote: | What is the correct behaviour?
|
VC++ 7 is correct here.
--
Ben Hutchings
Quantity is no substitute for quality, but it's the only one we've got.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Friedhelm Hoerner Guest
|
Posted: Fri Sep 17, 2004 5:39 pm Post subject: Re: private typedef |
|
|
Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote
| Quote: | Friedhelm Hoerner wrote:
Hello,
given the following c-snippet:
class Z
{
public:
class X {
private:
typedef vector<string> Vector;
Vector v_;
public:
Vector& get();
};
};
Z::X::Vector& Z::X::get() // (1)
{
return v_;
}
static void test()
{
Z::X x;
x.get(); // (2)
vector<string> v = x.get(); // (3)
Vector v = x.get(); // (4)
int size = x.get().size(); // (5)
}
With VC6 line (1) gets an Error: "cannot access private typedef...".
This is a bug in the compiler. Since it is a definition of a class
member, it should be able to use the private typedef.
With VC7 and some UNIX compilers (gcc...) only line (4) gets an Error.
The explicit use of Vector (4) is the most obvious error, but (3) and
(5) also need the intrinsics of the private typedef.
Accessibility checks apply to member lookup, after name resolution.
Lines (3) and (5) do not refer to Z::X::Vector by name, so its
accessibility to them is irrelevant. I don't know what you mean by
"intrinsics" though.
|
Well, you have to "know" that Vector is equivalent to vector<string>
which I thougt is a "private" knowledge.
In this case the design of making a typedef private and using that
name for the return value of a public function is probably
questionable;-).
The code was written under VC7 and then got an Error on VC6. For the
above reasons I agreed with VC6, but the "next Version should be
better than the previous" made me think about it...
Thanks, Friedhelm
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Mon Sep 27, 2004 8:22 pm Post subject: Re: private typedef |
|
|
Hi!
Friedhelm Hoerner wrote:
| Quote: | Well, you have to "know" that Vector is equivalent to vector<string
which I thougt is a "private" knowledge.
|
Although you specified "Vector" as the return type, the return
type really is "vector
know outside, you can call all members of it. Inside of X
"Vector" is just another name for "vector<string>".
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|