For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2005 > Nested class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Nested class
Hovhannes Asatryan

2005-02-14, 9:11 pm

Hi guys.
I have the following code
class A
{
public:
struct B
{
char *k;
void ff()
{
}
};

void f()
{
A::B::k = new char [10];
A::B::k[0] = '1';
A::B::k[1] = '2';
A::B::k[2] = '3';
A::B::k[3] = '4';
A::B::k[4] = '5';
}

protected:
private:
};
int main(int argc, char* argv[])
{
A m;
m.f();
cout << m.B::k << endl;
delete m.B::k;

return 0;
}

which works in windows and don't works in Linux.
Can anyone tell me why it works in windows??????????


Ulrich Eckhardt

2005-02-14, 9:11 pm

Hovhannes Asatryan wrote:
> class A
> {
> public:
> struct B
> {
> char *k;
> void ff()
> {
> }
> };
>
> void f()
> {
> A::B::k = new char [10];
> A::B::k[0] = '1';
> A::B::k[1] = '2';
> A::B::k[2] = '3';
> A::B::k[3] = '4';
> A::B::k[4] = '5';
> }
>
> protected:
> private:
> };
> int main(int argc, char* argv[])
> {
> A m;
> m.f();
> cout << m.B::k << endl;
> delete m.B::k;
>
> return 0;
> }


Your code is ill-formed in several aspects and I don't believe that this is
really the code you tried. Post the real code, after removing everything
not necessary. One more hint: there is no object of type A::B, so you can't
access any of its members.

> which works in windows and don't works in Linux.
> Can anyone tell me why it works in windows??????????


What do you mean with "don't works", does it not compile, link, run or give
the wrong results? Anyhow, nothing to do with the OS, only with the quality
(or lack thereof) of the compiler. I'd suggest you take that to
comp.lang.learn.c-c++, too.

Uli


Hovhannes Asatryan

2005-02-14, 9:11 pm

The is the real what I wrote.
In VC 6.0 SP5 it compiles runs and the result is correct (mean the same what
assigned in code)

In Linux it does not compile.

"Ulrich Eckhardt" <doomster@knuut.de> wrote in message
news:35ph1mF4o8v36U1@individual.net...
> Hovhannes Asatryan wrote:
>
> Your code is ill-formed in several aspects and I don't believe that this
> is
> really the code you tried. Post the real code, after removing everything
> not necessary. One more hint: there is no object of type A::B, so you
> can't
> access any of its members.
>
>
> What do you mean with "don't works", does it not compile, link, run or
> give
> the wrong results? Anyhow, nothing to do with the OS, only with the
> quality
> (or lack thereof) of the compiler. I'd suggest you take that to
> comp.lang.learn.c-c++, too.
>
> Uli
>
>



Stephen Howe

2005-02-14, 9:11 pm

> don't works in Linux.

NEVER EVER SAY THAT !!!

Anybody that says it "don't works" does not deserve help.
"don't works" tells us NOTHING !!!!!

Does not compile? If so, why not post the first few messages the compiler
emits, word verbatim?
Does not link? If so, why not post the messages the linker emits, word
verbatim?
Does not run? If so, why not tell us what the symptoms are.
And why not tell us what version compilers you are using while you are at
it?
And and any other environmental information (OS, minor versions, memory if
relevant, other tools if relevant, command line switches if relevant)

Remember, the quality of help you get is directly proportional to your
initial post.

Stephen Howe


Stephen Howe

2005-02-14, 9:11 pm

> The is the real what I wrote.
> In VC 6.0 SP5 it compiles runs and the result is correct (mean the same

what
> assigned in code)
>
> In Linux it does not compile.


Right, so could you post the error messages are you seeing in Linux?
And are you using g++ and if so, what version?

Stephen Howe





Jeff F

2005-02-14, 9:11 pm

Stephen Howe wrote:
>
> NEVER EVER SAY THAT !!!
>
> Anybody that says it "don't works" does not deserve help.
> "don't works" tells us NOTHING !!!!!
>
> Does not compile? If so, why not post the first few messages the
> compiler emits, word verbatim?


The bigger question is why does this code compile, link and run under vc6.5
and vc7.1? As Ulrich stated, it is ill formed in several ways. So I think
the OP's "don't works" == does not compile, in this case.

Jeff


Hovhannes Asatryan

2005-02-14, 9:11 pm

Hello Stephen.
In my second letetr I wrote it does not compile. in Linux.

//// LINUX part ////
OS Red Hat Linux 7.3

gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110).

Environment Visual SlickEdit 7

compilation error is:
--------- Build Project: 'btest.vpj' - 'Debug' ----------
VSLICKERRORPATH=/home/MyProjects/btest
btest.cpp
btest.cpp: In method `void A::f ()':
btest.cpp:18: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:19: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:19: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:19: ambiguous conversion for array subscript
btest.cpp:20: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:20: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:20: ambiguous conversion for array subscript
btest.cpp:21: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:21: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:21: ambiguous conversion for array subscript
btest.cpp:22: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:22: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:22: ambiguous conversion for array subscript
btest.cpp:23: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:23: cannot convert a pointer of type `A' to a pointer of type
`A::B'
btest.cpp:23: ambiguous conversion for array subscript
btest.cpp: In function `int main (int, char **)':
btest.cpp:33: type `A::B' is not a base type for type `A'
btest.cpp:34: type `A::B' is not a base type for type `A'
*** Errors occurred during this build ***

////Windows part ////

OS Windows XP professional.
Environment VC++ 6.0 SP 5.

"Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom> wrote in message
news:elnKuj6AFHA.612@TK2MSFTNGP09.phx.gbl...
>
> NEVER EVER SAY THAT !!!
>
> Anybody that says it "don't works" does not deserve help.
> "don't works" tells us NOTHING !!!!!
>
> Does not compile? If so, why not post the first few messages the compiler
> emits, word verbatim?
> Does not link? If so, why not post the messages the linker emits, word
> verbatim?
> Does not run? If so, why not tell us what the symptoms are.
> And why not tell us what version compilers you are using while you are at
> it?
> And and any other environmental information (OS, minor versions, memory if
> relevant, other tools if relevant, command line switches if relevant)
>
> Remember, the quality of help you get is directly proportional to your
> initial post.
>
> Stephen Howe
>
>



Carl Daniel [VC++ MVP]

2005-02-14, 9:11 pm

Hovhannes Asatryan wrote:
> which works in windows and don't works in Linux.
> Can anyone tell me why it works in windows??????????


It's a bug. A rather heinous one at that, and it's still present in VC7.1
and VC8 beta.

For the purpose of computing member offsets, the compiler is acting as if A
contains an instance of A::B as its first member variable. Of course, A
contains no such member, so the accesses to k are actually overwirting
memory past the real size of A.

gcc is correct to reject this code (which by the way isn't what you really
compiled, despite your assertion otherwise - at least '#include <iostream>'
and 'using namespace std;' must be added to get this to compile.

I've opened a bug report against VC8 beta for this, if you think this is an
important bug (I do), please add your vote at

http://lab.msdn.microsoft.com/Produ...ackId=FDBK20688

-cd


alarix

2005-02-14, 9:11 pm

voted;)
really stupid bug.

Hov jan, gj:)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com