 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tom_usenet Guest
|
Posted: Thu Jun 26, 2003 7:11 pm Post subject: Re: Cannot access private typedef |
|
|
On 25 Jun 2003 21:07:10 -0400, "Matthias Hofmann"
<hofmann (AT) anvil-soft (DOT) com> wrote:
| Quote: | Hello,
I have a little problem here. Consider the following class, which has
an
embedded class and a private typedef:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f();
};
};
The problem is: When I define COuterClass::CInnerClass::f() outside of
the
class definition, like so
// Note, SOMETYPE is a private typedef within COuterClass..
COuterClass::SOMETYPE COuterClass::CInnerClass::f()
{
return 0;
}
I get an error that says that I "cannot access private typedef" for the
return value of the function. However, an inline definition of the
function
works:
[SNIP] |
Neither should work - CInnerClass gets no special access privileges to
COuterClass. You need:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
//tell the compiler that CInnerClass is in this scope
class CInnerClass;
//so that this friend declaration knows that it
//is at this scope, and not the enclosing scope
friend class CInnerClass;
//and finally define the class
class CInnerClass
{
public:
SOMETYPE f();
};
};
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chandra Shekhar Kumar Guest
|
Posted: Thu Jun 26, 2003 7:13 pm Post subject: Re: Cannot access private typedef |
|
|
| Quote: |
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f() { return 0; } // Note, now inline.
};
};
Now I wonder: Is this compliant with the standard?
|
no, u r using old compiler perhaps....this is not a legal code...same
error
shud come as earlier...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Thu Jun 26, 2003 7:18 pm Post subject: Re: Cannot access private typedef |
|
|
Hi,
| Quote: | I have a little problem here. Consider the following class, which has
an
embedded class and a private typedef:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f();
};
};
The problem is: When I define COuterClass::CInnerClass::f() outside of
the
class definition, like so
// Note, SOMETYPE is a private typedef within COuterClass..
COuterClass::SOMETYPE COuterClass::CInnerClass::f()
{
return 0;
}
I get an error that says that I "cannot access private typedef" for
the
return value of the function.
|
This is IMHO a compiler deficency. A subclass of a class should be
considered
part of this class and hence should have access to private members of
the class, including the typedef. For reference, g++ 2.95.xx had the
same problem that has been fixed in the meantime.
Greetings,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
MiniDisc_2k2 Guest
|
Posted: Thu Jun 26, 2003 7:40 pm Post subject: Re: Cannot access private typedef |
|
|
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote
| Quote: | Hello,
I have a little problem here. Consider the following class, which has
an
embedded class and a private typedef:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f();
};
};
The problem is: When I define COuterClass::CInnerClass::f() outside of
the
class definition, like so
// Note, SOMETYPE is a private typedef within COuterClass..
COuterClass::SOMETYPE COuterClass::CInnerClass::f()
{
return 0;
}
I get an error that says that I "cannot access private typedef" for
the
return value of the function. However, an inline definition of the
function
works:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f() { return 0; } // Note, now inline.
};
};
Now I wonder: Is this compliant with the standard? And if yes, is
there
anything alse I can do about it except defining the function inline?
|
AFAIK it is compliant with the standard (who knows why), and if your
compiler won't let you, I don't see a way to work around it.
| Quote: |
I don't want to make the typedef public because it is not need in
global
functions, only by other classes also declared with COuterClass.
|
But, unlike public functions/members, making a typedef public wouldn't
hurt
the class at all. I see no reason to make the typedef private. Besides,
I'd
love to see you make a practical typedef shorter than int. I'd recommend
making the typedef public.
-- MiniDisc_2k2
to reply, replace nospam.com with cox dot net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Fri Jun 27, 2003 12:34 am Post subject: Re: Cannot access private typedef |
|
|
----- Original Message -----
From: MiniDisc_2k2 <MattDelB (AT) sdc (DOT) cox.net>
Newsgroups: comp.lang.c++.moderated
Sent: Thursday, June 26, 2003 9:40 PM
Subject: Re: Cannot access private typedef
| Quote: | Besides,
I'd
love to see you make a practical typedef shorter than int. I'd recommend
making the typedef public.
|
What exactly do you mean? Should I choose a typedef with fewer letters? This
wouldn't make sense in my case, the classes are used for an audio
compression algorithm and I define sample types like
typedef unsigned char SAMPLE8BIT;
typedef signed short SAMPLE16BIT;
Regards,
Matthias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vadim Guest
|
Posted: Fri Jun 27, 2003 6:09 pm Post subject: Re: Cannot access private typedef |
|
|
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> schrieb im Newsbeitrag
news:3efb79ae (AT) news (DOT) nefonline.de...
| Quote: | Now this is 2 people telling me it is compliant with the standard and 2
that
say it is not - which is true now ???
|
Nested class cannot access members of the enclosing class, no matter typedef
or not:
11.8/1 Nested classes
The members of a nested class have no special access to members of an
enclosing class,
nor to classes or functions that have granted friendship to an enclosing
class; the usual access rules (clause 11) shall be
obeyed. The members of an enclosing class have no special access to members
of a nested class; the usual
access rules (clause 11) shall be obeyed.
However you can derive your nested class from another nested class declared
private in the same enclosing class (base-clause)
You may declare your enclosing class as friend as shown by tom_usenet, or
make your typedef public
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Sat Jun 28, 2003 4:30 am Post subject: Re: Cannot access private typedef |
|
|
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> skrev i meddelandet
news:3efb79ae (AT) news (DOT) nefonline.de...
| Quote: |
Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> schrieb in im Newsbeitrag:
[email]3efa2c30 (AT) news (DOT) nefonline.de[/email]...
Hello,
I have a little problem here. Consider the following class, which
has an
embedded class and a private typedef:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f();
};
};
The problem is: When I define COuterClass::CInnerClass::f()
outside of the
class definition, like so
// Note, SOMETYPE is a private typedef within COuterClass..
COuterClass::SOMETYPE COuterClass::CInnerClass::f()
{
return 0;
}
I get an error that says that I "cannot access private typedef"
for the
return value of the function. However, an inline definition of the
function
works:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f() { return 0; } // Note, now inline.
};
};
Now I wonder: Is this compliant with the standard? And if yes, is
there
anything alse I can do about it except defining the function
inline?
I don't want to make the typedef public because it is not need in
global
functions, only by other classes also declared with COuterClass.
Best regards,
Matthias Hofmann
Now this is 2 people telling me it is compliant with the standard
and 2 that
say it is not - which is true now ???
|
I belive is actually *is* this confusing because the standard document
is not very clear at this point. There is an update on the way, with a
new wordning on the access rules for private 'siblings' in an outer
class:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#45
I have seen some compilers that implement the suggested change, and
some that implement the letter of the standard.
Bo Persson
[email]bop2 (AT) telia (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Sat Jun 28, 2003 12:35 pm Post subject: Re: Cannot access private typedef |
|
|
On 26 Jun 2003 15:18:17 -0400, Thomas Richter
<thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote:
| Quote: | This is IMHO a compiler deficency. A subclass of a class should be
considered
part of this class and hence should have access to private members of
the class, including the typedef.
|
According to the standard, it shouldn't. You can always make it a
friend if you want, but you couldn't unmake it a friend if it were
implicitly a friend, as you suggest.
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Jun 28, 2003 11:29 pm Post subject: Re: Cannot access private typedef |
|
|
In message <3efc8c69.5321781 (AT) news (DOT) easynet.co.uk>, tom_usenet
<tom_usenet (AT) hotmail (DOT) com> writes
| Quote: | On 26 Jun 2003 15:18:17 -0400, Thomas Richter
[email]thor (AT) cleopatra (DOT) math.tu-berlin.de[/email]> wrote:
This is IMHO a compiler deficency. A subclass of a class should be
considered
part of this class and hence should have access to private members of
the class, including the typedef.
According to the standard, it shouldn't. You can always make it a
friend if you want, but you couldn't unmake it a friend if it were
implicitly a friend, as you suggest.
|
You are right in that that is what we originally wrote but Thomas is
also right in that that makes more sense and it will be that way in the
future (BTW according to the standard you cannot make a nested class a
friend)
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Jun 28, 2003 11:33 pm Post subject: Re: Cannot access private typedef |
|
|
In message <bdh2q7$cfj$04$1 (AT) news (DOT) t-online.com>, Vadim
<spam (AT) ferderer (DOT) net> writes
| Quote: | "Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> schrieb im Newsbeitrag
news:3efb79ae (AT) news (DOT) nefonline.de...
Now this is 2 people telling me it is compliant with the standard
and 2
that
say it is not - which is true now ???
Nested class cannot access members of the enclosing class, no matter
typedef
or not:
|
Try checking the defect reports. The section you quoted directly
conflicts with the constraints on friend which mean that an enclosing
class cannot make a nested class a friend (yes I know that every working
compiler ignores that) We chose to fix 11.8/1 rather than further mess
with the problems surrounding friend declarations.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Jul 01, 2003 6:12 pm Post subject: Re: Cannot access private typedef |
|
|
Matthias Hofmann wrote:
| Quote: | I have a little problem here. Consider the following class, which has an
embedded class and a private typedef:
class COuterClass
{
public:
COuterClass();
private:
typedef int SOMETYPE;
class CInnerClass
{
public:
SOMETYPE f();
};
};
[snip]
I get an error that says that I "cannot access private typedef" for
the return value of the function.
|
Apart from what was already said by others in this thread, you might be
able to turn the typedef around:
class Outer
{
struct Inner
{
typedef int inner_type;
};
typedef Inner::inner_type SOMETYPE;
};
cheers
Ulrich Eckhardt
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ 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
|
|