Solved

Trick to generate compiler error if "wrong" function called

Posted on 2007-03-28
11
268 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 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

17 Experts available now in Live!

Get 1:1 Help Now