Question

I guess my question is quite simple but i can't figure out any solution:

Can I redefine memberfunctions of a class in a static library?

Example

My Library looks something like this:

Library.h

namespace mynamespace
{
     class A
     {
       public:   void randomfunction();
     }
}

Library.cpp

namespace mynamespace
{
    void A::randomfunction(){
       std::cout << "Random output!" << std::endl;
    }
}

My program using this library:

main.cpp

using namespace mynamespace;

void A::randomfunction(){
  std::cout << "Super fancy output!" << std::endl;
}

int main(int argc, char* argv[])
{
    A a;
    a.randomfunction();
    system("pause");
    return 0;
}

Including the library works fine and if I don't override the function my program compiles and works fine. The problem is I need the normal version for most of my programs but sometimes I need it to do other stuff and i can't use inheritance because I have other classes in this library that refer to this class and I also don't want to make the other classes generic because then i always have to use inheritance even if i don't want to override the function :/

Was it helpful?

Solution

If you define a function in a static library and again differently in one of the other source files, you violate the one definition rule.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top