• Advertisement
Sign in to follow this  

Namespaces & statics

This topic is 3106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, i've just came across some "wired" behavior tho im not sure if it is desired or not. As a simple example i did something along this:
namespace blah
{
	static const int foo = 66;
}

namespace blub
{
	static const int foo = 99;
}
And after i compiled it successfully, i checked the values of both variables, and i was quite puzzled to learn the value was 99 for both blah::foo and blub::foo. So obviously the compiler made use of just one memory block and filled it with the latest value. But is this desired behavior, i though if im using namespaces i'm also getting my very own memory block for each variable used within. Am i totaly mistaken here? Thanks in advance! Imp

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Impz0r
As a simple example i did something along this:


namespace blah
{
static const int foo = 66;
}

namespace blub
{
static const int foo = 99;
}


[...]i was quite puzzled to learn the value was 99 for both blah::foo and blub::foo. So obviously the compiler made use of just one memory block and filled it with the latest value.


So a complete example would be:

#include <iostream>

namespace blah
{
static const int foo = 66;
}

namespace blub
{
static const int foo = 99;
}

int main()
{
std::cout << blah::foo << '\n';
std::cout << blub::foo << '\n';
return 0;
}


Visual C++ 2005:

P:\guff>cl /nologo /EHsc ns.cpp /Fens.exe
ns.cpp

P:\guff>ns
66
99


MinGW 3.4.5:

P:\guff>g++ ns.cpp -o ns

P:\guff>ns
66
99


Borland 5.5.1:

P:\guff>bcc32 ns.cpp -ens.exe
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
ns.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

P:\guff>ns
66
99


Digital Mars 8.5.0:

P:\guff>dmc ns.cpp -Ae
link ns,,,user32+kernel32/noi;


P:\guff>ns
66
99


Which compiler are you using?

Share this post


Link to post
Share on other sites
Hmm wired, thoes results i would have expected.. im using Microsoft Visual C++ 2008 btw.


Imp

Share this post


Link to post
Share on other sites
Visual C++ 2008:

P:\guff>cl
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

P:\guff>cl /nologo /EHsc ns.cpp /Fens.exe
ns.cpp

P:\guff>ns
66
99


Does that not work for you?

Share this post


Link to post
Share on other sites
Wired stuff... i just tested it again and it does work now.. dont ask why.. sorry for this senseless post...

But thanks for your help the_edd, appreciate it.


Imp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement