CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2005
    Posts
    8

    Thumbs up Function pointer assignment where the problem lies ?

    Could you tell me how I can deal with this problem when I currently need to make an assignment of some function pointers as shown below ?

    Code:
    class Account{
    private:
    	string 	name;
    	long 	code;
    	int 	hoursOfWork;
    	double 	baseSalary;
    public:
    	//Some functions to handle these private members
    };
    
    
    void (*funci)()throw Account; //this function does the throwing job
    void (*funco)();
    void (*funcp)();
    void funcCompatre{
    	funcp=funco;
    	funco=funci;
    	//some other assignments
    }
    If I take out --thow Account--, things are just fine, no compilers' errors show up, but still I don't know if there is any another way for me to work around that...Some advice from you is really needed...

    Thank you.
    ......

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by Cungtrang
    Could you tell me how I can deal with this problem when I currently need to make an assignment of some function pointers as shown below ?
    Overall, what are you really trying to accomplish (please don't mention function pointers -- what are you really trying to do)? It seems you are using the most obscure parts of the C++ language to accomplish something that doesn't need all of this esoteric coding.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Function pointer assignment where the problem lies ?

    void (*funci)()throw Account; //this function does the throwing job
    void (*funco)();
    void (*funcp)();
    What are these global variable of type pointer to function?
    I hope that they are initialized somewhere.
    Could you post more code?
    And, what is the exact nature of your problem?
    funcp=funco;
    funco=funci;
    What is the purpose of these assignments?
    Code:
    void funcCompatre{
    The compiler should have emitted an error here.
    There are missings parenthesis to enclose function parameters.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Oct 2005
    Posts
    8

    Thumbs up Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by Paul McKenzie
    Overall, what are you really trying to accomplish (please don't mention function pointers -- what are you really trying to do)? It seems you are using the most obscure parts of the C++ language to accomplish something that doesn't need all of this esoteric coding.

    Regards,

    Paul McKenzie
    I truly would just like to understand what is correct and what is incorrect...

    SuperKoko, I mistook again {} with (), but it won't be the main problem at all right ?
    I am doing a job as an accountant for a small company in my area, and that is a small program I will do to generate all employees' salaries during a week...
    Also, i am learning basic function pointers but I can't find any good sources available out there after many times I have tried again and again to search, which is why I come to post some questions here in the hope of being able to have several of my doubts in mind partially or completely cleared up by gurus' help...


    Those function pointers I created actually have their own bodies and return pointers also, but I wonder if they are able to be in such a comparison anyway...

    Do you still have any other advice for me anyway ? Thank you so much, :-D
    ......

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by Cungtrang
    I truly would just like to understand what is correct and what is incorrect...
    Then post compilable code. The reason why I (and others) ask what you are trying to do is that you will get answers and specifically how to solve your problem using proper C++ methods, not necessarily methods that you've convinced yourself will "solve" your issue.
    I am doing a job as an accountant for a small company in my area, and that is a small program I will do to generate all employees' salaries during a week...
    That is not really what I was asking. This part of the program you are working on -- what high-level construct are you trying to do? For example, are you trying to call a function based on some criteria, and have it work on derived objects of a based class?
    Also, i am learning basic function pointers
    Why do you believe you need to use function pointers? In C++, you rarely use function pointers. What's wrong with virtual functions and proper use of polymorphism? That's what I'm trying to relay to your original post -- you've decided for some reason that "function pointers" will solve your problem, we just would like to know what that reason is. When you ask experienced persons a question (it doesn't matter what topic), expect questions in return if there is something amiss about the way you are going about your problem. Don't expect just robotic answers.
    but I can't find any good sources available out there after many times I have tried again and again to search, which is why I come to post some questions here in the hope of being able to have several of my doubts in mind partially or completely cleared up by gurus' help...
    If you posted why you need function pointers, and code that can actually be broken down and understood, then you will get more help. Function pointers are an obscure part of C++, and that is why a) you need to post a better, more compilable example and b) why you decided to use function pointers, as opposed to virtual functions/polymorphism.

    There are many threads here concerning function pointers. Just do a search. There are also many threads on virtual functions. There are even threads where the poster starts out using function pointers, then one of the CodeGuru experts explains that the way to really do this with C++ is with virtual functions (or maybe templates), and posts a much cleaner solution without function pointers, with the original poster a now happy camper. Maybe your problem will fall into the latter category.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Function pointer assignment where the problem lies ?

    First, the code that you have given does not compile which is quite obvious as pointed by SuperKoko and Paul already dues to errors. Now, if you ask me to ignore them for a moment and help you with something looking at whatever you have posted, I can try but that would not be dependable unless what I am thinking is what you are actually wanting to achieve. However, I shall not refrain from giving some comments and here they are:

    1. I assume here that you are trying to create three different function pointers here:
    Code:
    void (*funci)()throw Account; //this function does the throwing job
    void (*funco)();
    void (*funcp)();
    Now, any code with function pointers defined like this beats the purpose of mere existence of their concept. The actual thing should have had been that you had one function pointer for all functions taking no arguments and returning nothing. For example like this:
    Code:
    typedef void (*Fptr)();	//define a typedef for easier looking syntax
    Fptr var_Fptr=NULL;	//declare a variable of type Fptr and initialize it
    I also assume that funco, funci and funcp are 3 different functions. Making 3 different function pointers for the same return type-argument type functions beats their purpose and I assume that is not what you are trying to do here.

    2. It is not at all clear what you are trying to do in funcCompatre() function because if my assumption of funco, funci and funcp being 3 different functions is correct then probably you cannot do what you are doing there since they could not be assigned any values to.

    3. A function pointer to a function having same return type and argument list (type and number) as another but throwing a some or different exception can all be handled with the same type of function pointer. You don't need to typedef another one for that purpose. My example below will show you that.

    4. There are better alternative to function pointers in C++. They are called function objects which are actually classes (their objects, in fact) with the '()' operator overloaded. You can read up about them here: Function Objects.

    5. A small comment about the exception handling. You should have custom exception classes for the purpose of throwing and catching exceptions. you should refrain using ints, char's, or as in you sample code, exceptions like ordinary objects "Account" from your code. You should derived your custom exception classes from the base std::exception. That is the preferred way to go, IMO.

    Here is the sample code that I refered to, above:
    Code:
    #include <iostream>
    #include <string>
    using std::string;
    class Account{
    	private:
    		string name;
    		long code;
    		int hoursOfWork;
    		double baseSalary;
    	public:
    		//Some functions to handle these private members
    };
    
    void funci() throw(Account){
    	std::cout << "funci called\n";
    	throw Account();
    }
    
    void funco(){
    	std::cout << "funco called\n";
    }
    
    void funcp(){
    	std::cout << "funcp called\n";
    }
    
    int main(){
    	try{
    		typedef void (*Fptr)();	//define a typedef for easier looking syntax
    		Fptr var_Fptr=NULL;		//declare a variable of type Fptr and initialize it
    		if (true /*funci is to be called*/)  //depending upon the conditions.
    			var_Fptr = funci;
    		else if (true /*funco is to be called*/)  //depending upon the conditions.
    				var_Fptr = funco;
    		else if (true /*funcp is to be called*/)  //depending upon the conditions.
    				var_Fptr = funcp;
    		//make a call to whatever function that was to be called based on
    		//above design making if-else constructs
    		var_Fptr();		//yeah the function called - the one that is stored in this variable.
    	}catch(const Account&){
    		std::cout << "Account exception caught.\n";
    	}
    	return 0;
    }
    Use of virtual functions as suggested by Paul might solve your purpose but since you do not expose any meaningful code hence it would be tough to conclude upon that. For better and more specific comments/explanations/solutions please provide a meaningful code that you think should have had worked but is not working right now. Hope this helps. Regards.
    Last edited by exterminator; October 24th, 2005 at 02:42 AM. Reason: formatting the code...and typos...

  7. #7
    Join Date
    Jul 2005
    Posts
    103

    Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by Cungtrang
    Could you tell me how I can deal with this problem when I currently need to make an assignment of some function pointers as shown below ?

    Code:
    void (*funci)()throw Account; //this function does the throwing job
    void (*funco)();
    void (*funcp)();
    void funcCompatre{
    	funcp=funco;
    	funco=funci;
    	//some other assignments
    }
    funci is a different type (the exception specification is part of the type) to funco and funcp and simpy put, you can't mix pointer (and reference) assignments to different function types where the source is less restrictive. The type expression for the function that funci can point to is
    Code:
    void () throw Account;
    and for the others
    Code:
    void ();
    Last edited by freddyflintstone; October 24th, 2005 at 09:13 AM.

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by freddyflintstone
    funci is a different type (the exception specification is part of the type) to funco and funcp and simpy put, you can't mix pointer (and reference) assignments to different function types.
    I don't think so. Even Comeau accepts it. You could try this code with it at Online Comeau Compiler where I do this:
    Code:
    #include <iostream>
    class Account{
    };
    void funci() throw(Account){
    	std::cout << "funci called\n";
    	throw Account();
    }
    int main(){
    	try{
    		typedef void (*Fptr)();	
    		Fptr var_Fptr=NULL;
    		var_Fptr = funci;
    		var_Fptr();
    	}catch(const Account&){
    		std::cout << "Account exception caught.\n";
    	}
    	return 0;
    }

  9. #9
    Join Date
    Jul 2005
    Posts
    103

    Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by exterminator
    I don't think so. Even Comeau accepts it. You could try this code with it at
    Check your example, again.
    Code:
    		typedef void (*Fptr)();	
    		Fptr var_Fptr=NULL;
    		var_Fptr = funci;
    You are assigning the address of funci to Fptr which is less restrictive so this works.

    The post, however, is effectively
    Code:
    void funco() {}
    typedef void (*Fptr)() throw(account);	
    Fptr var_Fptr=&funco;
    which fails because you are assigning a pointer to a function with no exception specification to a pointer to a function that has one.

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by freddyflintstone
    Check your example, again.
    Okay. So, I checked my code again with what you suggested and now VC++ 6.0 let's me do that as well without any warnings/errors and Comeau fails to compile it. But for another reason, not what you are saying. It fails with an error saying that the exception specification is not even allowed!! Here is the code that I tested on both compilers:
    Code:
    #include <iostream>
    class Account{
    };
    
    void funci(){
    	std::cout << "funci called\n";
    }
    
    int main(){
    	typedef void (*Fptr)() throw(Account);	
    	Fptr var_Fptr=&funci;
    	var_Fptr();
    	return 0;
    }
    Now I shall be asking you to please direct me to a link/article/book whatever that suggests something what you are saying or post a sample code that justifies your words because I am unable to prove you right . Best regards.

  11. #11
    Join Date
    Jul 2005
    Posts
    103

    Re: Function pointer assignment where the problem lies ?

    You can look it up in Stroustrup - Checking Exception Specifications.

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Function pointer assignment where the problem lies ?

    Quote Originally Posted by freddyflintstone
    You can look it up in Stroustrup - Checking Exception Specifications.
    Okay...I got it. Actually, the typedef doesn't work with exception specifications basically and I thought, since you also used typedef to mention the error, that way. Once I removed the typedef and used the exception specifications, it did allow those restrictions that means functions with different exception specifications must be handled using different function pointer. But I wonder why the typedef failed to recognize the ES. Here is the code that proves you partially correct ..thanks for pointing this out. It is something new that I have learnt today. Thanks freddyflintstone .
    Code:
    #include <iostream>
    class Account{
    };
    
    class  AnotherAccount{
    
    };
    
    void funci() throw (Account){
    	std::cout << "funci called\n";
    }
    
    void funco() throw (AnotherAccount){
    	std::cout << "funco called\n";
    }
    
    int main(){
    	void (*Fptr)() throw(Account);	
    	Fptr=&funci;
    	Fptr();
    	Fptr=&funco;  //ERROR here: incompatible exception specifications
    	Fptr();
    	return 0;
    }
    I got this from here - 11.4.1 Exception Specifications and Pointers to Functions in case the OP might be interested. Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured