Sign in to follow this  
Impz0r

Namespaces & statics

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this