Good practice to call a static function inside class or struct - c++

I wonder what is the better practice to call a static function(and variables) inside a class or struct between A and B.
A: ClassName::functionName();
B: functionName();
Here's my simple example code:
In header file,
typedef struct _mystruct
{
static void myfunction();
} t_mystruct;
And in CPP file,
void t_mystruct::myfunction()
{
//do something
}
Now, in the same CPP file, what is the better practice to call this static function between A and B?
A: t_mystruct::myfunction();
B: myfunction();

Calling straight myfunction(); is only feasible if you are inside t_mystruct's implementation. In which case you can do according to your preferred coding style:
// .h
struct t_mystruct // You can declare it directly that way
{
static void myfunction();
void myOtherFunction();
};
// .cpp
void t_mystruct::myOtherFunction()
{
myfunction(); // That's fine!
t_mystruct::myfunction(); // That's fine too!
}
Otherwise, you have to explicitely use its fully qualified name:
void anywhereElse()
{
t_mystruct::myfunction(); // Mandatory
myfunction(); // Does not compile
}
This is true for your whole codebase, not only for the considered .cpp file.

Related

Standard way to control symbol visibility

Using C and plain functions in C++, I can prevent the functions' symbols to be exported by using static keyword:
static int foo(int a, int b) { /* ... */ }
But in a class, defining a function as static has a completely different meaning. Is there a way to ensure the compiler that my whole class will be used only within module, and there is no need to export any of its methods' symbols?
Use an anonymous namespace.
namespace
{
class C
{
C() {}
void f() {}
};
void f();
}
class ExportedClass
{
ExportedClass() {}
void f() {}
};
void exportedFunction() {}
As you can see, you should do this for normal functions, too. The usage of static for this is discouraged.
You can use the anonymous namespace. For example,
// file foo.cc
namespace {
int symbol1 = 0;
struct auxiliary { /* ... */ } symbol2;
}
void foo(int x)
{
// uses symbol1 and symbol2
}
when symbol1 and symbol2 are not 'visible'.
Depending on what you want and why you are doing it you could move the class into the .cpp file. This works for example if you are implementing a library. Have a look at the discussion at Putting class declaration in .cpp file
To be more specific your class should go in it's own separate translation unit away from main and away from classes outside of your module.

How to hide a class inside of a static library in Visual Studio

I want to only have 2 extern C functions, which are how to interact with the API. Then inside the static .lib I want to have my class that does all the work. But it shouldnt be visible to the outside.
I can do it with just pure C functions by declaring them static inside a compilation unit, but how do I do it with a class ?
If I understand your question well:
you want to create a static library presenting only two functions to the outside world
but the internals of this library should be based on a class you want to hide from outside world.
you know how to do hiding internals in classic c (i.e. using auxiliary static functions and static variables) but you don't see how to do with classes
If this is the case, the trick is simply to use an unamed namespace:
In your library source you would have something like this:
namespace { // anonymous
class U { // class visible only to the library
public:
int y;
U() :y(0) { cout << "U\n"; }
void mrun() { y++; }
};
}
void f() {
U x;
...
}
You may then use your library from outsilde world:
extern void f(); // declare the function (in a header)
f(); // invoke the function
Even if the auxiliary class would be declared in the outside world:
class U { public: int y; U(); void mrun(); };
It would not be able to used and linking errors would be generated if it would be tempted to use U. This is because unnamed namespaces are unique to each compilation unit.
If you use the same kind of solution but without the anonymous namespace, the auxiliary class would be visible and the link would succeed.
Perhaps you could mirror the class's API using C functions a bit like this:
class Cpp
{
int i = 0;
public:
int get_i() { return i; }
void set_i(int i) { this->i = i; }
};
// C code has a void* as a handle to access
// the correct instance of CPP
extern "C" void* new_Cpp()
{
return new Cpp;
}
extern "C" void delete_Cpp(void* cpp)
{
delete reinterpret_cast<Cpp*>(cpp);
}
extern "C" int Cpp_get_i(void* cpp)
{
return reinterpret_cast<Cpp*>(cpp)->get_i();
}
extern "C" void Cpp_set_i(void* cpp, int i)
{
return reinterpret_cast<Cpp*>(cpp)->set_i(i);
}

How to call static class method from a struct?

I've been always avoiding the following in C++ (which I believe is C++03 used in VS 2008) but now I'm curious if it's possible to do this? Let me explain it with the code.
//Definitions.h header file
//INFO: This header file is included before CMyClass definition because
// in contains struct definitions used in that class
struct MY_STRUCT{
void MyMethod()
{
//How can I call this static method?
int result = CMyClass::StaticMethod();
}
};
then:
//myclass.h header file
#include "Definitions.h"
class CMyClass
{
public:
static int StaticMethod();
private:
MY_STRUCT myStruct;
};
and:
//myclass.cpp implementation file
int CMyClass::StaticMethod()
{
//Do work
return 1;
}
In this case, you would need to move the implementation of MY_STRUCT::MyMethod outside the header file, and put it somewhere else. That way you can include Definitions.h without already having CMyClass declared.
So your Definitions.h would change to:
struct MY_STRUCT{
void MyMethod();
};
and then elsewhere:
void MY_STRUCT::MyMethod()
{
int result = CMyClass::StaticMethod();
}

Can a struct member(function) be defined before struct itself?

I'm new to C++, and I have a question: Can a member of struct that's a function, be defined before the struct itself?
Like this:
void foo_t::SayHello() {
printf("Hello,World!\n");
}
struct foo_t {
void SayHello();
};
Because by using this I split struct in a foo.h and SayHello() function in a C file.
EDIT::
And then include the .c in the top of .h file. Not in end of file.
I'm sorry for don't be more specific because I new to C++ and I don't know about C++'s terms.
No. (Point)
But you might do:
class foo_t;
void say_hello(const foo_t& foo) // defined in a source
struct foo_t {
void SayHello() const { say_hello(*this); }
};
But: "And then include the .c in the top of .h file. Not in end of file." makes me shiver.
No, but you can make a function proxy:
void foo_t_SayHello() {
printf("Hello,World!\n");
}
struct foo_t {
void SayHello() { foo_t_SayHello(); }
};
A decent compiler will inline this, resulting in no overhead.

Is pimpl compatible with anonymous namespaces?

I am trying to use the pimpl pattern and define the implementation class in an anonymous namespace. Is this possible in C++? My failed attempt is described below.
Is it possible to fix this without moving the implementation into a namespace with a name (or the global one)?
class MyCalculatorImplementation;
class MyCalculator
{
public:
MyCalculator();
int CalculateStuff(int);
private:
MyCalculatorImplementation* pimpl;
};
namespace // If i omit the namespace, everything is OK
{
class MyCalculatorImplementation
{
public:
int Calculate(int input)
{
// Insert some complicated calculation here
}
private:
int state[100];
};
}
// error C2872: 'MyCalculatorImplementation' : ambiguous symbol
MyCalculator::MyCalculator(): pimpl(new MyCalculatorImplementation)
{
}
int MyCalculator::CalculateStuff(int x)
{
return pimpl->Calculate(x);
}
No, the type must be at least declared before the pointer type can be used, and putting anonymous namespace in the header won't really work. But why would you want to do that, anyway? If you really really want to hide the implementation class, make it a private inner class, i.e.
// .hpp
struct Foo {
Foo();
// ...
private:
struct FooImpl;
boost::scoped_ptr<FooImpl> pimpl;
};
// .cpp
struct Foo::FooImpl {
FooImpl();
// ...
};
Foo::Foo() : pimpl(new FooImpl) { }
Yes. There is a work around for this. Declare the pointer in the header file as void*, then use a reinterpret cast inside your implementation file.
Note: Whether this is a desirable work-around is another question altogether. As is often said, I will leave that as an exercise for the reader.
See a sample implementation below:
class MyCalculator
{
public:
MyCalculator();
int CalculateStuff(int);
private:
void* pimpl;
};
namespace // If i omit the namespace, everything is OK
{
class MyCalculatorImplementation
{
public:
int Calculate(int input)
{
// Insert some complicated calculation here
}
private:
int state[100];
};
}
MyCalculator::MyCalculator(): pimpl(new MyCalculatorImplementation)
{
}
MyCalaculator::~MyCalaculator()
{
// don't forget to cast back for destruction!
delete reinterpret_cast<MyCalculatorImplementation*>(pimpl);
}
int MyCalculator::CalculateStuff(int x)
{
return reinterpret_cast<MyCalculatorImplementation*>(pimpl)->Calculate(x);
}
No, you can't do that. You have to forward-declare the Pimpl class:
class MyCalculatorImplementation;
and that declares the class. If you then put the definition into the unnamed namespace, you are creating another class (anonymous namespace)::MyCalculatorImplementation, which has nothing to do with ::MyCalculatorImplementation.
If this was any other namespace NS, you could amend the forward-declaration to include the namespace:
namespace NS {
class MyCalculatorImplementation;
}
but the unnamed namespace, being as magic as it is, will resolve to something else when that header is included into other translation units (you'd be declaring a new class whenever you include that header into another translation unit).
But use of the anonymous namespace is not needed here: the class declaration may be public, but the definition, being in the implementation file, is only visible to code in the implementation file.
If you actually want a forward declared class name in your header file and the implementation in an anonymous namespace in the module file, then make the declared class an interface:
// header
class MyCalculatorInterface;
class MyCalculator{
...
MyCalculatorInterface* pimpl;
};
//module
class MyCalculatorInterface{
public:
virtual int Calculate(int) = 0;
};
int MyCalculator::CalculateStuff(int x)
{
return pimpl->Calculate(x);
}
namespace {
class MyCalculatorImplementation: public MyCalculatorInterface {
...
};
}
// Only the ctor needs to know about MyCalculatorImplementation
// in order to make a new one.
MyCalculator::MyCalculator(): pimpl(new MyCalculatorImplementation)
{
}
markshiz and quamrana provided the inspiration for the solution below.
class Implementation, is intended to be declared in a global header file and serves as a void* for any pimpl application in your code base. It is not in an anonymous/unnamed namespace, but since it only has a destructor the namespace pollution remains acceptably limited.
class MyCalculatorImplementation derives from class Implementation. Because pimpl is declared as std::unique_ptr<Implementation> there is no need to mention MyCalculatorImplementation in any header file. So now MyCalculatorImplementation can be implemented in an anonymous/unnamed namespace.
The gain is that all member definitions in MyCalculatorImplementation are in the anonymous/unnamed namespace. The price you have to pay, is that you must convert Implementation to MyCalculatorImplementation. For that purpose a conversion function toImpl() is provided.
I was doubting whether to use a dynamic_cast or a static_cast for the conversion. I guess the dynamic_cast is the typical prescribed solution; but static_cast will work here as well and is possibly a little more performant.
#include <memory>
class Implementation
{
public:
virtual ~Implementation() = 0;
};
inline Implementation::~Implementation() = default;
class MyCalculator
{
public:
MyCalculator();
int CalculateStuff(int);
private:
std::unique_ptr<Implementation> pimpl;
};
namespace // Anonymous
{
class MyCalculatorImplementation
: public Implementation
{
public:
int Calculate(int input)
{
// Insert some complicated calculation here
}
private:
int state[100];
};
MyCalculatorImplementation& toImpl(Implementation& impl)
{
return dynamic_cast<MyCalculatorImplementation&>(impl);
}
}
// no error C2872 anymore
MyCalculator::MyCalculator() : pimpl(std::make_unique<MyCalculatorImplementation>() )
{
}
int MyCalculator::CalculateStuff(int x)
{
return toImpl(*pimpl).Calculate(x);
}