Trick to generate compiler error if "wrong" function called

In C++ does anybody know a trick to ensure that a function is never called though its definition does exist? The compiler would generate an error if this function is called.

I have a hierarchy of classes that contain a parallel template inner classes hierarchy. These inner classes have a virtual function func(). I have a problem overriding this function in the derived specialized inner class.

This might be a separate issue but for now I would just be interested to know if a technique exists to catch a call to a "wrong" function in the compile time.
sparkythedogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
All I can think of is to make the function a private member of a class and make sure no other class member function calls it.  That will make it so the function exists, but nobody else can call it.
0
sparkythedogAuthor Commented:
Well, unfortunately the function cannot be made private. It is a public member function of a protected inner template class.

What happens is that I use a derived inner class (and it is derived from a specialized version of a base inner template class). I need to override the inner class public virtual function FUNC() for this specialization.

As for the base inner template class, I would not even need its version of FUNC() for this specific kind of specialization. It is required to be in place by the compiler but I would liketo have it prevented from accidental use.

Hope I clarified my problem a bit.
0
Infinity08Commented:
Why not just let the function do "nothing" ?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

DarrylshCommented:
you should make it pure virtual

it took a look bit of prodding but here's some sample code.

#include<iostream>

class base_outerclass
{
public:


      base_outerclass(){}
      void callsomefunction();

protected:
      class base_innerclass
      {
      public:
            base_innerclass(){}
            virtual void somefunction()=0;
      };

      base_innerclass* bi;

};

class derived_outerclass : public base_outerclass
{
public:
      derived_outerclass(){this->bi = &di;}
protected:
      class derived_innerclass :public base_outerclass::base_innerclass
      {
      public:
            virtual void somefunction();
      };
      derived_innerclass di;

};

void base_outerclass::callsomefunction()
{
      bi->somefunction();
}
      
      
void derived_outerclass::derived_innerclass::somefunction()
{
      std::cout << "hello world\n";
}

int main()
{
      base_outerclass* p_bo= new derived_outerclass();
      p_bo->callsomefunction();
      delete p_bo;

}
0
itsmeandnobodyelseCommented:
>>>> you should make it pure virtual
pure virtual functions that have an implementation nevertheless can be called if not private:

class Base
{

public:
      virtual void func() = 0
      {   // implementation }

};

class Derived : public Base
{
};

int main
{
     Derived d;
     d.Base::func();   // compiles

     return 0;
}:

sparkythedog, why can't you make it private?

You told it should not be called but as you said you can't make it private I assume it is called nevertheless. Right?

Regards, Alex



0
DarrylshCommented:
>>>>>>>> you should make it pure virtual
>>>>pure virtual functions that have an implementation nevertheless can be called if not private:

I am assuming that he would remove the implementation if he had a way to override it.  I am guessing that's what he was alluding to with the other problem of not being able to override.  My code may guide him, but because he's using templated classes there may be other problems I didn't foresee especially since template functions can't be virtual

Admittedly, without the code though, I am making a lot of assumptions.
0
itsmeandnobodyelseCommented:
>>>> does anybody know a trick to ensure that a function
>>>> is never called though its definition does exist?

You can make the function a template function by setting template <typename T> above the function code. If someone tries to call it, there is a compile error that the template type cannot be determined.

template <typename T>
void functionNeverCalled()
{
      // implementation
}

int main()
{
    functionNeverCalled();   // error: cannot determine template type
    return 0;
}

Regards, Alex
0
DarrylshCommented:
>>functionNeverCalled();   // error: cannot determine template type


functionNeverCalled<int>(); // hey, i can call it like this :-)
0
itsmeandnobodyelseCommented:
>>>> functionNeverCalled<int>(); // hey, i can call it like this :-)

put this line at top of the implementation of the template

      class X {    X() {} } x;// error ==> cannot call private constructor

or any other invalid statement.
0
itsmeandnobodyelseCommented:
>>>> functionNeverCalled<int>(); // hey, i can call it like this :-)

Another solution is to put the implementation to a cpp file which *is not* part of the interface, i. e. which cannot be included. then any call like the one above would generate a linker error: unresolved external ....

Generally, it is the question what kind of people should be prevented from calling? Developers who made this cause they didn't know that it is an interface they shouldn't call or criminals who try to use forbidden code that is dangerous for any reason. If the latter, you have to make the implementation unavailable cause else you can't prevent from copying the implementation and call it at derived level or from commenting the template line in a private copy of the header file thus making the code callable again.

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sparkythedogAuthor Commented:
Guys,

Thanks a lot for your replies. It does not seem I can use any of the recommendations since:

a) It cannot be private. My virtual function is part of a class public interface. To simplify the design, the hierarchy is class Abstract -> class A -> class B -> class C. If func() is declared as virtual public in A you cannot redecalre it as private in B. (or rather you can but I guess it will still be publicly accessible due to its virtuality.

b) It cannot do nothing. It is actually a factory method. If it becomes part of the interface you cannot guarantee that somebofy will not (mistakenly) use it.

c) I do not think I can make it pure virtual. It is declared pure virtual in Abstract, then it is defined in A, B and C. If I try to make it pure virtual in B I am not sure it will compile. At least in a simplified code that I tried to compile it did not work.

d) I am not comfortable either with the idea of making the function templated. The code as it is is overburdened with templates, template template parameters etc. I would like to avoid additional complexity. Besides even if it is templated I do not quite see how in the hierarchy Abstract->A->B->C it would be callable in A and C, but not in B.

e) I start to feel that no good answer means that probably the question itself is flawed. Maybe I am looking in the wrong direction. I will post an example of the original problem with overriding. I would appreciate it a lot if some of the experts could point out to me where the problem is.

Thanks again for your insights.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.