PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

EVC, namespaces and stuff

I'm having troubles compiling that kind of simple stuff ... both in EVC 3 and EVC 4 (funniest thing is that EVC 4 IDE crashes ...)

I'd be interested if you have already seen this problem (since my EVC 4 install is not very clean, it may be related to a buggy SP2 install, but I doubt it ...)

Code:

namespace first {
class dummy {
public:
void foo();
};
}

namespace second {
class dummy : public first::dummy {
public:
void foo();
};
}

void first::dummy::foo() {
}

void second::dummy::foo() {
first::dummy::foo();
}
I haven't tried compiling that, but first::foo would have to be a static function.

Dan East
That's what the compiler said, too, but I don't really understand what the restriction is (in my understanding the function should still be reachable since it isn't virtual, and the different namespaces shouldn't change this behaviour) ... besides, this works fine and behaves as expected when compiled with g++.

Damn, where is my C++ primer book when I need it :D
if you use:

Code:
void second::dummy::foo() {
reinterpret_cast<first::dummy*>(this)->foo();
}
instead of

Code:
void second::dummy::foo() {
first::dummy::foo();
}
then it works fine in visual c too...

bye,
DANIEL
I tried with a dynamic cast before - it compiled fine, but with a wrong runtime behaviour (read : infinite loop :D)

I'll recheck that with a reinterpret cast, thanks
works fine on my machine (i admit, i only tried under VC6, but should be the same with EVC)...

bye,
DANIEL
still a bad behaviour on my EVC 4 + SP2 install ... I'll do a clean one on another computer tomorrow and recheck I guess :?
after some thinking, it is probably behaving not properly because EVC 3 does not support RTTI, and I cannot enable it in EVC 4 ... OK, think it'll be good to reinstall everything and understand where my install went bad :evil:
i don't see why RTTI should be needed here. this is a reinterpreting, not a dynamic cast.

so no need for RTTI...

DANIEL
you're right, I'm trying a dynamic cast (as last hope :D) because the reinterpreting cast tweak doesn't work on EVC 4 (at least on my install, didn't try on another clean one, usual disclaimer) ... sorry if it wasn't clear ...

a simple standard C cast works fine with Visual 6 btw.
yes, i this case there should be no difference between a standard c-cast and a reinterpret cast...

DANIEL
drgoldie wrote:
yes, i this case there should be no difference between a standard c-cast and a reinterpret cast...


*ICKS* no! A reinterpret_case<> won't work... If it does then you are lucky.

Try static_cast<> instead.
the standard cast should work ... anyway it should even work with no casts at all ... unfortunately it doesn't :D
Arisme wrote:
the standard cast should work ... anyway it should even work with no casts at all ... unfortunately it doesn't :D


What's standard cast? The C-style one? Yes it should work... But a reinterpret_cast<> would be a bug.
why would a reinterpret_cast<> not work?

DANIEL
drgoldie wrote:
why would a reinterpret_cast<> not work?


Why should it?

The value of a [first::dummy*] can't be the same as the value of a [second::dummy*] for a given object. i.e. the two pointers for the same object will have a different result.

When you do a static_cast<> or a C-style cast on one of them, the compiler will automatically add/substract an offset to get the address of the pointer you are casting to. If you use reinterpret_cast<>, you are effectively bypassing this calculation.
thread conclusion : when you cannot compile that kind of code

Code:

namespace first {
class dummy {
public:
void foo();
};
}

namespace second {
class dummy : public first::dummy {
public:
void foo();
};
}

void first::dummy::foo() {
}

void second::dummy::foo() {
first::dummy::foo();
}


the proper way (i.e. working even if foo() is virtual) to fix this Visual Studio / Embedded Visual C bug is

Code:

typedef first::dummy first_dummy;

void second::dummy::foo() {
first_dummy::foo();
}
well, sometimes you've got to go strange ways to solve a problem. my absolute favourite is this one to solve that #pragma warning (disable: 4786) sometimes does not work:


http://66.102.11.104/search?q=cache:7JB-n9U2SSQJ:lists.boost.org/MailArchives/boost/msg25941.php+%22disable4786.h%22&hl;=en&ie;=UTF-8