c++ and using public static members in a class
-
How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.
-
How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.
this worked for me:
class A
{
public:
static void g(int)
{
int i = 0;
}
};typedef void (*fp)(int);
int _tmain(int argc, _TCHAR* argv[])
{
fp p = A::g;
p(1);
return 0;
} -
How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.
// MyClass.h
class MyClass
{
static INT m_i;
// ...
};
// if you want to keep it in definition part
__declspec(selectany) INT MyClass::m_i; // default initialized to 0or
// MyClass.cpp
// ...
// if you want to have it in implementation part
INT MyClass::m_i; // default initialized to 0
// ...cheers, AR
When the wise person points at the moon the fool looks at the finger (Chinese proverb)
-
How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.
Static member functions usage: MyClass.h:
class MyClass
{
public:
static int fcn( int param );
static int inlineFcn( int param) ) // may be inlined, but not guaranteed
{
// just a little stuff
};
};MyClass.cpp:
#include "Myclass.h"
int MyClass::fcn( int param )
{
// stuff
}Client.cpp:
#include "Myclass.h"
void clientFcn( int a, int b )
{
// stuffint c = MyClass::fcn( a ); int d = MyClass::inlineFcn( b ); // more stuff
}
It sounds to me that another approach might be more suitable for your purposes. I'll post that next.
Please do not read this signature.
-
How do you do a extern type declaration for this. At link time I get unresolved external symbol "public: static void__cdecl etc. etc. I'm trying to expand my use of C++ to envelop static functions inside of classes as a way to help organize the code a bit better.
Alan Kurlansky wrote:
a way to help organize the code a bit better
It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:
namespace Group
{
int fcn( int param );
inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
{
// just a little stuff
};
}Group.cpp:
#include "Group.h"
namespace Group
{
int fcn( int param )
{
// stuff
}
}Client.cpp:
#include "Group.h"
void clientFcn( int a, int b )
{
// stuffint c = Group::fcn( a ); int d = Group::inlineFcn( b ); // more stuff
}
AlternateClient.cpp:
#include "Group.h"
using namespace Group;
void altClientFcn( int a, int b )
{
// stuffint c = fcn( a ); int d = inlineFcn( b ); // more stuff
}
Please do not read this signature.
-
Alan Kurlansky wrote:
a way to help organize the code a bit better
It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:
namespace Group
{
int fcn( int param );
inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
{
// just a little stuff
};
}Group.cpp:
#include "Group.h"
namespace Group
{
int fcn( int param )
{
// stuff
}
}Client.cpp:
#include "Group.h"
void clientFcn( int a, int b )
{
// stuffint c = Group::fcn( a ); int d = Group::inlineFcn( b ); // more stuff
}
AlternateClient.cpp:
#include "Group.h"
using namespace Group;
void altClientFcn( int a, int b )
{
// stuffint c = fcn( a ); int d = inlineFcn( b ); // more stuff
}
Please do not read this signature.
Yes, organization is what I'm looking to do in this case. This looks good. I'll give this a try. Thanks
-
Alan Kurlansky wrote:
a way to help organize the code a bit better
It sounds to me like you want the organization, but what you are doing is not truly about classes and object orientation. There is another facility in C++, namespaces, that may be more appropriate for your current use. It organizes things in groups and helps avoid naming conflicts, but does not raise the "object" expectations of someone reading your code. In some ways using namespaces looks a lot like what I posted for static member functions. This is how you might use namespaces: Group.h:
namespace Group
{
int fcn( int param );
inline int inlineFcn( int param) ) // may be inlined, but not guaranteed
{
// just a little stuff
};
}Group.cpp:
#include "Group.h"
namespace Group
{
int fcn( int param )
{
// stuff
}
}Client.cpp:
#include "Group.h"
void clientFcn( int a, int b )
{
// stuffint c = Group::fcn( a ); int d = Group::inlineFcn( b ); // more stuff
}
AlternateClient.cpp:
#include "Group.h"
using namespace Group;
void altClientFcn( int a, int b )
{
// stuffint c = fcn( a ); int d = inlineFcn( b ); // more stuff
}
Please do not read this signature.
Thanks. This looks good. I'll give it a try.
-
Yes, organization is what I'm looking to do in this case. This looks good. I'll give this a try. Thanks
Good luck. I hope it works well for you. I will add a note about style. My example showed indenting a level inside the namespace definitions, just like you would do for any other pair of {}'s. There are style guidelines that recommend not doing so for namespaces. I'm undecided about this myself.
Please do not read this signature.