typedef question

I am trying to understand how to define a typedef in general and how to specifically apply it below:

 
bool(*n_ptr)(int)(int)


I know we start with:
 
typedef bool(*n_ptr)(int)(int)


Do I condense and rename this?
 
(*n_ptr)(int)(int)
Last edited on
¿what do you want to do?
I want to make a typedef for ( I just don't get it)

bool (*validateFcn)(int, int);
Last edited on
given that you have no idea how to code what you want to do, perhaps you should consider to describe it with words.
typedef bool (*validateFcn)(int, int);
Now the example says instead of doing this:

bool validate(int x, int y, bool (*fcnPtr)(int, int));

You can do this (make less ugly):

bool validate(int nX, int nY, validateFcn pfcn)

Its the nomenclature. Where did validateFcn come from, and pfcn? What happened to (int,int)?
Last edited on
> Where did validateFcn come from
typedef bool (*validateFcn)(int, int);
`validateFcn' is the "type" of the functions that return a bool and have two ints as parameter

> and pfcn?
same as nX or nY, it's just the name of the parameter.

> What happened to (int,int)?
that was the idea, to make it disappear.
Last edited on
pfcn is not in the original typedef - it seems 'new'. You say its the name of the parameter but what does it represent?(pfcn). Is this user created?

Sorry probably making this more complicated than it should be.
Last edited on
It is simply a parameter.
bool validate(int nX, int nY, validateFcn pfcn)


eg;
1
2
3
4
bool validate(int nX, int nY, validateFcn pfcn)
{
     if(pfcn(nX,nY)) printf("Valid");
}
Consider how you define a parameter to a function: you specify the type, and you specify the name of the parameter. So, for the int nX parameter, you're specifying that the first argument to the function is of type int, and within the function it will have the name nX. nX hasn't "come from" anywhere - you, as the writer of the function, have decided that that will be the name of the parameter that you are defining.

Now look at validateFcn pfcn. That's another parameter of the function. And it's exactly the same - it has a type, and a name. The type is validateFcn - which, in this case, is a type that you've defined in your typedef statement, rather than a built-in type. And the name is pfcn - again, that hasn't "come from" anywhere - that's the name you're giving that parameter in the definition of function.
Last edited on
On using typedefs in general, if you're using a C++11 compiler (and if you're not, question it), you should make use of the 'using' syntax instead of typedefs: https://ideone.com/1eVSut
How would you call this function from main.


1
2
3
4
bool validate(int nX, int nY, validateFcn pfcn)
{
     if(pfcn(nX,nY)) printf("Valid");
}

Last edited on
Like this: https://ideone.com/oOCtjq

Not sure why you'd have a validate method that calls into some other validate method for a simple case, though.
As you should be able to see from MrHutch's example, the name of the function can be used as a pointer to the function.

I'm surprised the textbook you're using to learn this stuff didn't explain that.
Hi MB and Mr.Hutch

I agree about the complexity of the example. Could I possibly get you to modify your code for a more simple example? In the meantime I will go over the text.

If we have:
 
bool validate(int x, int y, bool (*fcnPtr)(int, int));


are int x & y in this case, "substitutions" for (int,int).
No.

What you have there is a method signature.

It says:
I will return a bool
I am named validate
I take three parameters:
- an integer named x
- an integer named y
- a pointer to some other function, who will return a bool and takes two integers, named fcnPtr

The x and y in that signature are unrelated to the two ints declared in the function pointer.

I think you're finding this confusing because of the way the example is set up. Here's a super-simple example (I'll use 'typedef' over 'using' since that's what you're asking for and I'm moving away from the validate example completely):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

// I'm a method that takes an int, returns an int
int Double(int a)
{
	return a * 2;	
}

int main(int argc, const char* argv[]) 
{
	// I'm a typedef. You can use me via typename 'MyFunctionPointer'
	// Whatever I point to must return and int and take one int
	typedef int (*MyFunctionPointer)(int); 
	
	// Declare an instance of the above type, pointing to Double
	// This is legal - Double meets the return int/take int requirement
	MyFunctionPointer myPtr = Double;
	
	// I can now call that method via myPtr, rather than call it explicitly
	std::cout << myPtr(10) << std::endl;
	
	return 0;
}


If that doesn't make sense, I can probably muster up some validation example.

Edit - Said simple validation example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

typedef bool (*ValidationMethod)(int);

bool PerformValidate(int numberToValidate, ValidationMethod)
{
	return ValidationMethod(numberToValidate);	
}

bool IsPositive(int numberToValidate)
{
	return numberToValidate > 0;
}

bool IsNonZero(int numberToValidate)
{
	return numberToValidate != 0;
}

int main(int argc, const char* argv[]) 
{
	int myNumber = 10;
	
	bool isPositive = PerformValidate(myNumber, IsPositive);
	bool isNonZero = PerformValidate(myNumber, IsNonZero);
	
	std::string isPositiveString = isPositive ? "is" : "is not";
	std::string isNonZeroString = isNonZero ? "is" : "is not";
	
	std::cout << "My number " << isPositiveString << " positive and " << isNonZeroString << " non zero\n";
	
	return 0;
}

Last edited on
I definitely understand the first example through and through. It was the setup that threw me.

Im now going to study the second example.

I also have a simple question: what is a typedef in its most literal sense, for e.g.:

typedef bool (*ValidationMethod)(int);

I know this bool fx is a pointer for pointing to a function. What does typedef "do" in this line for example.

I didn't want to bring this question to light b/c it is so basic.
Last edited on
A typedef in its most literal sense is an alias. It's a way of saying 'when I use this type, I actually mean this other type'.

I think in your examples are confusing you a bit because you're typedef'ing function pointers, which might look a little different.

Basically, typedef syntax is:
typedef <some_existing_type> <my_aliased_type>;

A concrete example might be something like:
typedef std::vector<MyNamespace::MyObject> TMyObjectVector;

This might help to make your code a little more readable. Say you declare an iterator for that vector type. Let's look at typedef'd vs non-typedef'd:
1
2
3
4
5
// No typedef
std::vector<MyNamespace::MyObject>::iterator iter;

// Typedef
TMyObjectVector::iterator iter;


You can freely use your typedef'd alias wherever you want.
1
2
3
4
5
// This...
void DoSomethingToThisVector(std::vector<MyNamespace::MyObject>& myVector);

// Could be written like this...
void DoSomethingToThisVector(TMyObjectVector& myVector);


As for function pointers, the syntax is:
<return type> (*<identifier>)(parameter types, ...)

So a function pointer to a function returning a std::string and taking a couple of integers would be:
std::string (*myFunctionPointer)(int, int)

When you typedef a function pointer, the identifier portion of the above syntax is what actually because the alias:
1
2
3
4
5
6
7
typedef std::string(*TMyFunctionPointer)(int, int);

// This...
TMyFunctionPointer somePointer = SomeFunction;

// Is the same as writing this...
std::string (*somePointer) (int, int) = SomeFunction;


So, say you've typedef'd your function pointer, you can now use your freely, just like the vector example above.
1
2
3
4
5
// This...
void SomeFunctionThatTakesAFunctionPointer(std::string(*myFunctionPointer)(int a, int b));

// Can be written like this...
void SomeFunctionThatTakesAFunctionPointer(TMyFunctionPointer myFunctionPointer);


A full example: https://ideone.com/tNRIL3

Hope this helps.

Edit: To answer the case you've specified in your post, this declares a typedef'd alias to a function pointer that points to a function that returns a bool and takes an integer. The name of this alias is ValidationMethod.
Last edited on
there's a little bit of confusion going here between functions and pointers to them:
typedef bool (*ValidationMethod)(int);
this declares a typedef'd alias to a function

that's an alias to a pointer to a function, not to a function itself, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
        bool f(int); // f is a function taking int and returning bool
typedef bool f_t(int); // f_t is the type of a function taking int and returning bool
using f_t2 = bool(int); // same, using C++11

        bool (*fp)(int);   // fp is a pointer to function taking int and returning bool
typedef bool (*fp_t)(int); // fp_t is the type "pointer to function taking int and returning bool"

        f_t* fp2;     // pointer to function taking int and returning bool (same as fp)
typedef f_t* fp_t2;   // type "pointer to function taking int and returning bool" (same as fp_t)

        bool (&fr)(int) = f; // reference to function taking int and returning bool
typedef bool (&fr_t)(int);   // type "reference to function taking int and returning bool" 


demo http://coliru.stacked-crooked.com/a/b5d4050997b34c16
Last edited on
Cubbi wrote:
that's an alias to a pointer to a function


Good catch. Edited.
Last edited on
Topic archived. No new replies allowed.