Free function in a nested namesace

R

rwf_20

Consider the following:

// ns.cpp
namespace ns1 {
public ref class c1 {
};

namespace ns2 {
public ref class c2 {
};

void freeFunction() { }
}
}

I build this as an assembly with cl /clr /c ns.cpp && link /DLL /
out:ns.dll ns.obj

Using the following client code, it appears I am unable to call
ns1::ns2::freeFunction().

// nsTest.cpp
#using "ns.dll"

int main() {
ns1::c1 t; // Fine

ns1::ns2::c2 c; // Fine

ns1::ns2::freeFunction(); // error C2039: 'freeFunction' is not a
member of 'ns1::ns2'
}


What's the problem here? Is this a basic restriction of the managed
world that I'm unaware of?

Thanks in advance,
Ryan
 
T

Tamas Demjen

rwf_20 said:
Consider the following:

// ns.cpp
namespace ns1 {
public ref class c1 {
};

namespace ns2 {
public ref class c2 {
};

void freeFunction() { }
}
}

freeFunction was not published, so it's not accessible from the outside.
..NET doesn't have the concept of global functions, so if you want
freeFunction to be accessible from other .NET modules, you need to make
it a static member function of a public class:

public ref class c3
{
public: static void freeFunction() { }
};

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top