Solved

Trick to generate compiler error if "wrong" function called

Posted on 2007-03-28
11
267 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:sparkythedog
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 15

Expert Comment

by:efn
ID: 18813647
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
 

Author Comment

by:sparkythedog
ID: 18813737
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18814169
Why not just let the function do "nothing" ?
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18814174
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18814222
>>>> 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:Darrylsh
ID: 18814348
>>>>>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18814939
>>>> 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
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18816849
>>functionNeverCalled();   // error: cannot determine template type


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

Expert Comment

by:itsmeandnobodyelse
ID: 18817114
>>>> 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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 18817228
>>>> 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
 

Author Comment

by:sparkythedog
ID: 18819746
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now